views:

110

answers:

2

Would it be incorrect to make all my View Models / Presentation Models static classes so that if any other View Presenter wanted to change a view model other than its own it could easily aquire a reference to it?

If this is the wrong approach how would you achieve it?

A: 

If you want to make it static you'd be better off implementing a proper Singleton pattern. You will find it extremely difficult to test all the classes that consume your static models as you won't be able to use any Dependency Inversion techniques.

Also, be aware of concurrency if you only have 1 class - you'll need to lock pretty much everything. Not good.

Finally, you might want to consider using a Factory pattern. Easily accessible but will create a new model object for each class that wants to use it. Concurrency issue (mostly) solved.

Chris Arnold
Thanks for your response. So I could use singletons for the models which would allow me to make use of something like Spring Ioc / Dependency Injection?
David
Yep. Best approach is the 'Supercede Instance' pattern.
Chris Arnold
Can you point me in the right direction on the 'Supercede Instance' pattern??
David
http://goodcoffeegoodcode.blogspot.com/2010/01/supercede-instance-pattern.html
Chris Arnold
+1  A: 

Static ViewModels sounds like an awful idea (I consider static evil as a general principle). This would mean that you can't have more than one instance of a ViewModel. I can think of many examples of UI where there are several instances of the same View type, but that wouldn't be possible with static ViewModels.

If you want to enable communication across Views, Publish/Subscribe (events) are a much better option.

Remember that when we talk about ViewModels/Presentaion Models they usually expose underlying Domain Objects. If you have sevaral Views that display parts of the same Domain Object, you can have the Domain Object raise events when it changes state, and any ViewModel that display data from that Domain Object can subscribe to those events and update their controls accordingly.

Mark Seemann
Thanks once again. Your answer makes complete sense to me but its the later part of your answer that I seem to have difficulty getting my head around. Say I have a customer view (customer id=1), this view is based upon a CustomerViewModel that is assembled from the customer domain object with id=1. Say I then create another customer view (customer id=1), this view again is based upon a new instance of CustomerViewModel that is again assembled from the customer domain object with id=1. These CustomerViewModels although reflecting ultimately the same customer domain object have been .. continued
David
.. assembled from 2 different instances of the Customer domain object. When I change the CustomerViewModel belonging to 1 view and if it was to update a property on the underlying domain object which would raise an event to signal this the other CustomerViewModel would not receive an event from its underlying domain object because its a different instance?
David
In such cases (and they can happen), you need some kind of Mediator to synchronize Domain Events, but the fundamental concept remains.
Mark Seemann
Ok, so would this mediator mediate between the 2 ViewModels? Is this where an event aggregator would be of use?
David
Yes, it vould mediate between all ViewModel instances. Event Aggregator is IMO just another name for the same thing :)
Mark Seemann