C# Disciples

my life in Avalon ….

Article revisited: MVVM + M (implementing a Fly Weight TreeView)

Ages ago I wrote an article to introduce an idea that I had for the Mediator pattern to work with MVVM (at that time MVVM was still in the being of its age so I didn’t even call it View Model I called it Controller).

Today I was preparing for a Demo that I will be doing in Poland and I thought to revisit this article and refactor it with the new methods that I discovered over the last year.

What’s new in the new and exciting in the new version.

>> Well for starters I am using a new and improved version of the Mediator. The new version of the Mediator API is much more user friendly. It support Generics and it also eliminates the need to have interfaces implemented for you ViewModel. Instead of implementing an interface and have a switch for the message type now you simple register a message to an Action delegate. The Action delegate can be of a generic type so that you don’t even have to cast the argument. You can read more on this over here.

>> This version also uses Service Locator and the concept of ViewModel Loaders that I introduced in this article.

>> It also uses attached behaviours for the TreeView instead of a ViewModel registering to routed events of the treeview through the EventManager. This is much better! Event Manager registration of event inside a ViewModel can be very dangerous and also makes the ViewModel dirty with references to UI elements. Yet at that time it was an approach that I thought was best, as I said those days were just when View Model concept was emerging…

It’s amazing what I have learnt in the last year and how much MVVM evolved!

Download the source code and have a look for yourself…. Compare the 2 versions and see how much better we WPF developers can today use MVVM :)

September 13, 2009 - Posted by marlongrech | WPF | | 8 Comments

8 Comments »

  1. Doesn’t it feel great to see improvements in your coding techniques and thought processes? :) Great job!

    Comment by Josh Smith | September 13, 2009 | Reply

  2. It does and that’s why I posted this :)

    Comment by Marlon Grech | September 13, 2009 | Reply

  3. hi, the .zip file is missing the MVVMHelper :)

    Comment by Frank | September 14, 2009 | Reply

  4. Thanks for pointing it out… I updated the link this should include MMVM Helper… Thanks again

    Comment by marlongrech | September 14, 2009 | Reply

  5. Things have indeed moved forward rapidly. Great job Marlon!

    Comment by Daniel Vaughan | September 16, 2009 | Reply

  6. Hello

    I really liked the idea of using your Mediator with the MVVM pattern, but Im really into silverlight, so i tried it out in silvelight 3.

    But I ran into a one problem…when calling CreateDelegate(….) in the method CreateAction()
    I get an MethodAccessException…and it is an internal method call which emits the exception.

    I just wandered if you have thought about implementing the Mediator for Silvelight? Do you have an idea why this Exception occurs in Silverlight? Trust?

    I know that the mediator was made for WPF but did you try it out for Silvelight?

    I like your ideas by the way!

    Best Regards

    Bård Pettersen

    Comment by nescau | October 26, 2009 | Reply

  7. I beleive this version will fix your issue

    http://marlongrech.wordpress.com/2009/04/16/mediator-v2-for-mvvm-wpf-and-silverlight-applications/

    Comment by marlongrech | October 27, 2009 | Reply

  8. Hi i really like your blog, i hope to see more great soon :)

    Comment by Jessica | December 7, 2009 | Reply


Leave a comment