Life Just Got Easier: Simple MVVM Toolkit for Silverlight

imageIf you’ve made a decision to start using the Model-View-ViewModel pattern (abbreviated as MVVM) for Silverlight development, you’re faced with a rather steep learning curve and a scarcity of of accepted standards and best practices.  I’ve read no less than three Silverlight books. All of them have a chapter on MVVM, in which the pattern is explained and some fairly basic examples are provided.  But when it comes to taking on some controversial issues, such as how to display modal dialogs, they tend to sidestep the issue and recommend that you pick up an MVVM toolkit.

The approach of these authors reflects the reality that a) there is more than one way to do MVVM, and b) you should not attempt to build a serious MVVM app without the aid of a toolkit.  The problem is that, if you are coming up to speed on MVVM, learning the ins and outs of a particular toolkit can be daunting.  What’s needed is a simple MVVM toolkit that comes with a tutorial to take you from square one to building a basic Silverlight application based on the MVVM pattern. Oh and by the way, it would be nice if it included Visual Studio templates and code snippets to make your life easier.

That day has now arrived. I am pleased to announce the Simple MVVM Toolkit for Silverlight, available for download from CodePlex:

It includes a set of helper classes with support for event-based messaging and modal dialogs, an implementation of IEditableDataObject that uses deep copies, and transparent marshaling of events for safe cross-thread calls to update the UI.  It comes with Visual Studio item templates for creating view-models and locators, and code snippets for inserting bindable properties and building out a view-model locator.  While all that is nice, the best part is a tutorial that includes “before and after” versions of a sample application with step-by-step instructions for building an MVVM Silverlight application.  This helps fill the void left by some other toolkits, where documentation and samples may be virtually nonexistent.  I’ve kept things very straightforward in this first phase of the project by implementing a standard n-tier application that uses a basic WCF service to retrieve entities from the Northwind sample database.  In the future I’ll incorporate support for things like dependency injection and WCF RIA Services.

Rather than shying away from some of the more controversial aspects of MVVM, I’ve decided as much as possible to address them directly.  Much of it boils down to your needs and preferences. A good example is going for zero (or almost zero) code-behind.  That might be a laudable goal if you have a team of visual designers using Blend that must be able to work independently from developers.  I’ve found, however, that many shops have developers building the UI.  In this case, achieving zero code-behind results in greater complexity and lowers developer productivity.  On the other hand, putting some UI-related logic in the view still preserves testability and separation of concerns, keeping the application simpler and easier to maintain.  In the Simple MVVM Toolkit I favor the latter approach – although the toolkit does provide some facilities for zero code-behind if you decide to go that route.

Another topic where there are diverging opinions is on how to communicate between various components, such as between the view and view-model or among view-models.  One approach is to use a message bus of some sort.  While that does make sense in many scenarios, especially communicating among view-models, I felt that a simpler approach using events is more practical and easier to use.  For example, when a method in the view-model requires information from the user, it can simply raise an event, and the view can handle the event by showing a dialog and calling back the view-model with the results.  It’s easy to understand and implement, and events can be raised by a helper method in the view-model base class, which guarantees that it fires on the UI thread.

Another area where there are different approaches concerns the use of commands.  Many MVVM toolkits include a delegating command which is used by the view-model to expose a command property that an element such as a button can bind to, so that the click event results in calling a method wired up to the command.  My personal feeling is that using commands for every button click (or other events by means of an event-to-command behavior) requires a lot of extra code in the view-model that is basically unnecessary.  While my Simple MVVM Toolkit does have a DelegateCommand for this purpose, I would suggest using it sparingly, and instead use an event trigger with a CallMethodAction to invoke a method on the view-model directly.  The only time I would resort to a command would be to pass a parameter to a method, where the parameter cannot be bound to a property in the view-model.

So go ahead and download the toolkit, take it for a test drive, and let me know what you think.  It’s not intended as the be-all or end-all toolkit, but it should be enough to get you started building Silverlight MVVM applications that maintain a clean separation of concerns, allow for testability and designability (also called Blendability), and can be more easily maintained over the long haul.  Enjoy.

About Tony Sneed

Sr. Software Solutions Architect, Hilti Global Application Software
This entry was posted in Technical and tagged , . Bookmark the permalink.

8 Responses to Life Just Got Easier: Simple MVVM Toolkit for Silverlight

  1. shankar says:

    Why not MVVMS toolkit in codeplex?

  2. Tony Sneed says:

    I’ll definitely check it out. Looks like it’s focused mostly on the RIA Services aspect, while my Simple MVVM Toolkit is more general-purpose.

    When starting out I did take a close look at Laurent Bugnion’s MVVM Light Toolkit, which currently lacks adequate documentation and has no sample application (he had one but seems to have taken it down). I use a slightly different approach to data binding support with a NotifyPropertyChanged method in the base class that accepts a lambda expression instead of a string. And it fires the PropertyChanged event on the UI thread.

    The other area where I do things differently than Laurent is messaging. Instead of a message bus I use events. There are cases where a message bus does make sense, for example, inter-view communication. But for things like two-way communication between the view and view-model, I think events provide just the right amount of loose coupling and are easier for most developers to understand and use.

  3. George says:

    Thank you. Very nice example for helping those of us that need to get up to speed with MVVM in a hurry.

  4. Josh says:

    Tony, this looks very interesting, and I’m planning on downloading your toolkit and taking it for a spin. I’m just getting started with MVVM, so I’ll be a good test case for you. 🙂

    My focus is on Windows Phone 7 applications. Do you have any idea if/how well your toolkit works in that context?

    • Tony Sneed says:

      @Josh: Thanks very much for checking out my toolkit. I’m working on the documentation and am very interesting in getting feedback. Thanks to your query, I plan to put up a Windows Phone 7 version shortly. 🙂

      • Josh says:

        Sweet. I’ll be keeping an eye out for it. I have been trying to use MVVM Light, and while I definitely dig the concepts it really seems like you have to go through some crazy gyrations to do simple things. Like the 10-page blog post I found about how to databind the WP7 pivot control headers. (The alternative is hard-coding them into your XAML. The horrors! I’d rather spend the time doing stuff that adds value to my app, rather than maintaining the purity of a design pattern.)


  5. Tony Sneed says:

    I’ve started writing documentation for the Simple MVVM Toolkit on CodePlex. Check it out. It’s just one page for now, but there’s more to come!


  6. Danny says:

    Thanks for developing and sharing the toolkit. I’ve looked at many Silverlight books and MVVM was mentioned only in passing if at all. The currently available toolkits can be daunting and overwelming to say the least. I was looking at the MVVMS Lite ToolKit and Prism. My boss said what was that noise – I replied it was my head hitting the table ! We hear SOC , DRY , YAGNI etc. What happened to KISS !

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.