0

I have a scenario that I've tried to solve with TPL. The result is decent but I'm wondering if my design is flawed and if there is room for any improvement or radical changes.

Scenario: An user can "subscribe" to X number of items and can set a given interval for updates of that item. The user will then get notifications if the item has changed its data. Since time is a vital factor I want to show an item as updated straight away instead of waiting for all items to be updated and then notify the user about all updated items in a batch, or is this a bad idea?

My approach:

  1. A user subscribes to an event called ItemUpdated.
  2. A method, called Process is called each time with the given interval. The method is called in a fire and forget way by creating running it on a BackgroundWorker. The Process method works in the following way:
    2.1 Retrieve JSON strings and post them to a BufferBlock which is linked to a TransformBlock.
    2.2 The TransformBlock parses each JSON string into a domain object called Item. The TransformBlock is linked to an ActionBlock.
    2.3 The ActionBlock invokes the event ItemUpdated for each Item it receives.

My question is basically: Is this an optimal solution or should i re-think my strategy? My biggest concern is that I notify the user about updated items with an event. Should I use an async callback method that will be given a list of all updated items instead or is there an alternative solution?

  • What do you need the BackgroundWorker for when you are using TPL and DataFlow? It doesn't provide anything more than an ActionBlock or Task.Run does. In this case, a [System.Threading.Timer](http://msdn.microsoft.com/en-us/library/system.threading.timer(v=vs.110).aspx) would be more appropriate - it's a real timer and it runs on a ThreadPool thread – Panagiotis Kanavos Oct 30 '14 at 11:12
  • Basically I just wanted to call Process and then forget about it until I get notifications about updated items. Though System.Threading.Timer does look like a good way to do it. On another note: Would it be better to skip the whole invoking of events as the last step. Maybe the last step should be putting the Item objects in an ActionBlock and then let the caller decide what to do when items are being added to the ActionBlock? – rosaflodhest Oct 30 '14 at 11:22
  • You should look at reactive extensions. – i3arnon Oct 30 '14 at 11:43
  • When would one use Reactive Extensions over, for example, TPL and Dataflow? – rosaflodhest Oct 30 '14 at 12:27
  • @rosaflodhest 1. You should tag people to notify them of a comment. 2. TPL is the underline framework of Rx, TPL Dataflow, async-await and so forth. TPL Dataflow is for actor based design. When you want to "subscribe" to some kind of stream Rx is a better fit. – i3arnon Oct 31 '14 at 09:50

0 Answers0