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 🙂

9 thoughts on “Article revisited: MVVM + M (implementing a Fly Weight TreeView)

  1. 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

  2. Hi Marlon,
    I am relatively new to MVVM and WPF. Nevertheless, your article has helped me a lot. I’ve tried implementing a FileSystemWatcher for both directories and files. I am catching the events, but I run into troubles of getting the UI updated since the ObservableCollection cannot be modified by another thread than UI thread.

    I’d appreciate a lot if you could post a tutorial to do the same while respecting the MVVM and OO approach.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s