Archive for the ‘Design Pattern’ Category

h1

MVVM described by Fowler (as Presentation Model)

April 14, 2009

A great design-pattern description by Martin Fowler entitled “Presentation Model”.

In WPF/Silverlight terms, this would be MVVM – and is a great statement of what’s going on.

http://www.martinfowler.com/eaaDev/PresentationModel.html

h1

Thread Safety in a (Property)Changing World

March 8, 2009

 

I’m really loving the power and simplicity of MVVM (Model View View-Model).  The View-Model communicates changes to the binding system via the ‘PropertyChanged‘ event of INotifyPropertyChanged.  I also like to communicate changes from the Model to the View-Model via this event.

But there is a potential rub here.  Whenever you do some work on a background thread, you need to make sure you pass execution back to the UI thread if the resulting activity ends up changing the UI (a foregone conclusion really when firing PropertyChanged).  It’s a real drag having to invoke the Dispather whereever you know (or suspect) firing the event will cause a threading exception because an attempt is made to update the UI on a background thread.

Well, after poking through Nikhil’s excellent framework/sample – I found a great way to remove this drag from my life (and your life) forever!  Below is a base class you can use to derive model’s and view-models on that will always fire the PropertyChanged event over on the UI thread.  Dispatcher arbitrage is now a gone-burger.

 

NotifyPropertyChangedBase

 

And for good measure, here’s an asynchronous unit test that verifies this behavior:

 

Thread Safe NotifyPropertyChanged - UnitTest