views:

105

answers:

3

I have a collection of "active objects". That is, objects that need to preiodically update themselves. In turn, these objects should be used to update a WPF-based GUI.

In the past I would just have each object include it's own thread, but that only makes sense when working with a finite number of objects with well-defined life-cycles. Now I'm using objects that only exist when needed by a form so the life cycle is unpredicable. Also, I can have dozens of objects all making database and web service calls.

Under normal circumstances the update interval is 1 second, but it can take up to 30 seconds due to timeouts.

So, what design would you recommend?

A: 

I am not an expert, but I would just have the objects fire an event indicating when they've changed. The GUI can then refresh the necessary parts of itself (easy when using data binding and INotifyPropertyChanged) whenever it receives an event.

emddudley
That's easy. The hard part is telling the object when to update itself in the first place without some user hitting a refresh button.
Jonathan Allen
Maybe I don't understand your question. Doesn't each object handle its own refreshes? Each object can have its own timer and timeout. Then whenever an object detects an event worth reporting, it notifies the GUI.
emddudley
A: 

I'd probably try to generalize out some sort of data bus, if possible, and when objects are 'active' have them add themselves to a list of objects to be updated. I'd especially be tempted to use this pattern if the objects are backed by a database, as that way you can aggregate multiple queries, instead of having to do a single query per each object.

If there end up being no listeners for a specific object, no big deal, the data just goes nowhere.

The core updater code can then use a single timer (or multiple, or whatever is appropriate) to determine when to get updates. Doing this as more of a dataflow, and less of a 'state update' will probably save a lot of sanity in the end.

kyoryu
That breaks the encapsulation of the data objects, but so far it sounds like my best bet.
Jonathan Allen
I don't particularly like encapsulating 'data' (typically domain objects) with persistence code anyway, as it tends to create high degrees of coupling that can be very difficult to work around in the long run. But, there's valid reasons to have that coupling in place.
kyoryu
+1  A: 

Hello, Jonathan.

You may use one dispatcher (scheduler) for all or group of active objects. Dispatcher can process high priority tasks at the first place then other ones. You can see this article about the long-running active objects with code to find out how to do it. In additional I recommend to look at Half Sync/ Half Async pattern. If you have questions - welcome.

igor
What is the Half Sync/ Half Async pattern?
Jonathan Allen
I think, it is better to read this document www.cs.wustl.edu/~schmidt/PDF/PLoP-95.pdf. Shortly, we have active objects that execute their own tasks (synchronous tasks or user's processes) and some external tasks that execute asynchonously (low events or calls from the other subsystems).
igor