tags:

views:

179

answers:

4

First of i am not a UI developer and this is probably a very simple problem.

What i have is a external service that I subscribe to an event, when ever that event fires a service picks this up, manipulates the data in some way then gives the UI the data to display

What i am unsure of is how to archetect this and keep the dependancy between the service which will tell the UI to update and the UI as loose as possible.

Can anyone suggest a stratagy for this or post me some links on examples or an open source project to actually look at some working code.

I am using c# and ether wpf or winforms for this.

Cheers

Colin G

+1  A: 

My solution to this problem is to create a timer in your ui, and have your ui subscribe to the 'onTick' method. Then, at every timer tick, have the UI look at the service and figure out what data to display.

Mark P Neyer
You don't need timer in this situation.
P.K
A: 

There's a lot of ways to skin this cat, but without knowing a little more about your requirements and your existing infrastructure, let me suggest you use an EventBroker / Mediator for this. This is an easy way to implement a kind of Publisher / Subscriber type of relationship without worrying about too much of the plumbing.

If you are using Prism, I'd suggest using the EventAggregator.

If not, you might consider using the "Messenger" implementation of an EventBroker available with the MVVMFoundation stuff that John Smith wrote. It's not really dependent on you using MVVM or WPF and does what you are looking for: http://mvvmfoundation.codeplex.com/

Hope this helps.

Anderson Imes
+1  A: 

How simple is this application?

The simplest solution is to have the data access/manipulation in one object, and have the UI passed as an interface into that object. With the UI interface methods, you can give data to the UI but let the UI handle displaying the data in a GUI thread-safe manner.

If it's a more complex application, I'd say it would make more sense to look into something like MVC or MVP. Or MVVM for WPF, maybe look at Bea Costa's blog for databinding examples.

genki
the Model* patterns don't natively provide a solution for this problem. Passing your UI via an interface is known as the "Listener" pattern and is more easily done using events, but you wind up causing memory leaks this way because the service that is static and unchanging has a reference to the UI in a way that keeps it around forever (assuming you aren't doing something complicated, like using WeakReference, etc).
Anderson Imes
A: 

then gives the UI the data to display...

I would suggest you to have a service agent layer which will raise an event and pass a DTO. This event should be subscribed by the layer which contains objects bound to the UI. Once this layer receives the DTO, update the UI.

P.K