Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an app that works with a list of objects so has one central keyed list. The main window has a tree containing these object with some information, which is easy enough to populate as a one off event after loading.

Now, the complicated part, any part of the application can add, remove, and more importantly change the details of those objects at any time (all in the same process) and I'd like the tree to update to suit.

I have a few options including passing events back down from the object to the list to the form which seems to be the most flexible way. I can also do it lazily and repopulate the tree each time or periodically (very hackish).

Does anyone have any better thoughts on how to structure this?

This is being done in C# 2.0 but the concepts apply to any environment.


share|improve this question
up vote 2 down vote accepted

This is just one application not a server client setup correct? If so I would use a Observer pattern with the base objects as the subject and the display objects as the observers.

share|improve this answer
How does this differ from events from the object? They just seem to be generic events. – Deanna Mar 2 '11 at 14:53
It doesn't the Observer pattern is a pattern that describes the use of events to keep two objects in sync. – Erin Mar 2 '11 at 15:18
Ok, so evants to say "this has changed", possibly passed back through the list object (to handle new ones, etc)? – Deanna Mar 3 '11 at 12:22

I solved this by adding a Presenter. Essentially, the presenter is a middle-man. All interactions between your UI and the "back end", as you call it, go through the presenter.

Now assuming when you say that "any part of the application can add, remove, and more importantly change the details of those objects at any time", I assume what you mean is that the parts that can initiate some kind of change are always starting from the UI level (unless of course you have some kind of asynchronous task running in the background...)

Since all actions are initiated from the UI, then the presenter knows when anything happens that could change the state. The presenter is responsible for notifying relevant portions of the UI after something happens that could affect its state. The naive, but completely workable solution in many cases, it to always notify all of the displayed parts of the UI after every UI initiated action. (This is workable because in most cases the UI is a flattened representation of a small portion of your "back end", so there usually isn't that much information to synchronize -- the amount of data is limited by your physical screen real-estate.) The notification can be any mechanism (in mine, I just notify the top UI object of the tree, and it passes the message down to all child UI objects). Each UI object is then responsible for looking at the current state, determining if anything changed, and then refreshing what it's displaying if necessary.

Another alternative: a message bus. It's a bit cleaner than events (with events you have to hook up and unhook events to/from objects, and if you forget, you get the equivalent of memory leaks). With a message bus, any object can send a message: "something changed!" and anyone can subscribe to those messages. Check out the messaging infrastructure in MVVM Light for example. It's nice in that it uses soft references, so it doesn't have the potential memory leak issue that events have.

share|improve this answer
+1. The best way to link the model and the view is with a controller. – Matthew Read Mar 2 '11 at 16:12
Would the backend list object in this case count as the controller? – Deanna Mar 3 '11 at 12:21
@Deanna - no, the backend list object is part of your model. The controller/presenter is something else. It's a separate concern. – Scott Whitlock Mar 4 '11 at 17:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.