Simple MVVM Toolkit versus MVVM Light Toolkit
Now that I’ve released Simple MVVM Toolkit version 2, people are starting to ask how it stacks up against some of the other MVVM Toolkits out there. (MVVM, which stands for Model-View-ViewModel, is a UI design pattern that provides better maintainability, testability and designability.) When it comes to deciding which toolkit best suites your needs, there is a spectrum of choices. On one end are very powerful frameworks, such as Prism or Caliburn (Micro), which offer a plethora of features but have a rather steep learning curve. At the other end there are toolkits that are very lightweight, with the basic features you need to build apps using the MVVM design pattern.
Simple MVVM Toolkit would fall into the latter category. It is designed with the idea that a design based on simplicity reduces the time spent learning a framework and makes your apps easier to develop and maintain. But just because a framework is simple does not make it trivial. In fact, Simple MVVM Toolkit includes just about every feature you need to build real-world line of business apps based on the MVVM design pattern. However, instead of trying to accommodate every possible use case, my toolkit focuses on the most common scenarios to provide the best possible value with a minimum amount of friction.
So if you need an MVVM toolkit that’s offers basic features and is relatively easy to use, the range of choices narrows to a few options. Due to time restrictions, I will have to save an exhaustive survey of toolkits for a future post. But here I will compare Simple MVVM Toolkit to the most popular “basic” toolkit out there: MVVM Light Toolkit by Laurent Bugnion. Disclaimer: Choice of toolkits depends greatly on your requirements and is a matter of personal preference. This comparison is meant to highlight features and benefits of the Simple MVVM Toolkit and should not be construed as a criticism of any other toolkit.
Disclaimer: This table reflects the features of an older MVVM Light version. Please consult the toolkit’s page for more information.
Both Simple MVVM and MVVM Light toolkits support WPF, Silverlight and Windows Phone clients. Both toolkits come with three assemblies offering basically the same functionality, each targeted to a different platform.
The area where Simple MVVM Toolkit really shines in the ease with which you can quickly get up and running. Presently MVVM Light requires a manual installation, but the download for Simple MVVM is an installer which gives you the assembly binaries, external libraries, samples and source code. It also registers the toolkit assemblies so that they appear in the Visual Studio Add References dialog, and it installs Visual Studio project and item templates, code and xml snippets. Everything is there and it just works. And if you download the Simple MVVM Toolkit from the Visual Studio Extensions Gallery, you’ll also be notified when a new version of the toolkit is released on the gallery.
MVVM Light does not seem to offer much in the way of documentation. All that’s listed on the Documentation tab of the project are links to a couple blog posts and a few screen shots. Simple MVVM Toolkit, on the other hand, has Documentation that includes the following topics: Introduction, Features, Prerequisites, Download Contents, Installation, Samples, and a Programming Reference. In addition there is a Getting Started topic which correlates to a screencast and provides step-by-step instructions for creating a Silverlight app using the toolkit. I also explore various features of the toolkit on my blog, with a listing posted on the Discussions page of the toolkit project, and I support a Facebook group for the toolkit.
While Laurent lists a number of articles and tutorials that have been written about MVVM Light, I was unable to locate very many samples available for download which demonstrate how to use his toolkit and its various features. He has presented some workshops for which you can download the code, and there are some tutorials by Jesse Liberty and Chris Koenig. That’s not too bad, but, perhaps because I prefer to learn by example, I would like to have something more like a reference application.
That is why I have focused a great deal of my energy on building two categories of sample applications that are installed with the Simple MVVM Toolkit. One is an assortment of small samples, each illustrating a particular feature or aspect of the toolkit: Property Association, Enums, Async, Dependency Injection, Messaging, Navigation, and RIA Services, as well as Getting Started samples for Silverlight, WPF and Windows Phone. The other category of samples consists of a three-part series showing the development of an end-to-end MVVM Silverlight app that invokes operations on a WCF service and interacts with a SQL Server database. Part 1 has basic functions and shows how to use events for dialogs and two-way communication between Views and ViewModels. Part 2 demonstrates navigation, messaging, dependency injection, unit testing and async operations. Part 3 ports the app to WCF RIA Services and shows how to leverage it for entity persistence and batch updates.
Both Simple MVVM and MVVM Light toolkits ship with Visual Studio project and item templates, as well as some code snippets. I would say that the project templates for MVVM Light are rather basic. There is a ViewModeLocator, which exposes a static MainPageViewModel with a single property and no methods or commands. The MainPage view has a TextBlock that is bound to the property on the MainPageViewModel. That’s it. Simple MVVM Toolkit also has ViewModelLocator, ViewModel and View classes, but the locator supplies a service agent (represented by an interface), which the ViewModel uses to create a new Model. In addition Simple MVVM includes a multi-project Visual Studio template that supplies a Silverlight client, a WCF RIA Services web host, and a Unit Testing project. This is designed to be a real-world example with navigation, modal dialogs, messaging and saving changes. Each project template includes a ReadMe file with step-by-step instructions.
While both toolkits include a number of code snippets, Simple MVVM also sports XML snippets that can be used to add bindings and event triggers to XAML files.
Both toolkits provide a ViewModel base class that implements INotifyPropertyChanged with lambda expressions to support type-safe two-way data binding. Both toolkits supply a DelegateCommand class for wiring up ViewModel commands. Commands play a more central role in MVVM Light, which has an EventToCommand behavior, but I favor Blend-style event triggers so that you can call any ViewModel method from any event in the view without all the extra code and funkiness required for commands (for example, SL apps needing to raise the ICommand.CanExecuteChanged event). I prefer to use commands mainly when you need to pass a parameter to a ViewModel method, which is rare because View elements are generally bound to ViewModel properties.
There are several features included in the Simple MVVM Toolkit which appear to be lacking in MVVM Light. For example, the async support in MVVM Light is simply a wrapper over the Dispatcher, whereas Simple MVVM has async support built right into the ViewModel base class, which transparently marshals to the UI thread when firing the PropertyChanged event. There is a Notify method for doing the same when communicating with the View using events.
Simple MVVM has two kinds of ViewModels: a general-purpose VM, good for showing summary lists and such, and a “detail’ VM, useful for binding a VM to a single model entity. The ViewModelDetailBase class has a Model property which you can use to expose the model so that the View can bind directly to it. This eliminates redundant code in the VM where you need to duplicate each and every model property. The detail VM base class also implements IEditableDataObject with deep cloning and allows you to associate VM properties with one or more model properties, so that updates to the model properties cascade to the associated VM property.
Lastly, Simple MVVM has direct support for dependency injection with MEF (Managed Extensibility Framework) and unit testing (for example with the Silverlight Unit Testing Framework). In fact, there is an “injected” ViewModel locator item template that allows for mock service agents to be injected into ViewModels when executed by a unit test. The Simple MVVM RIA Services project template includes this code.
There is a marked difference between the toolkits in terms of philosophy and style. MVVM Light emphasizes the use of a Messenger for loosely-coupled communication in just about all aspects of the app, including data binding, dialogs and navigation. Simple MVVM, on the other hand, takes a more pragmatic approach by using events for communication between Views and the ViewModels they own. The View already knows about its ViewModel, so I don’t see the justification for a message bus in the case of error messages or dialogs. The VM simply exposes an event and the View subscribes to it, popping up a dialog if needing user input. It can then communicate the result back to the VM by means of a callback on the event args that are passed in. Simple.
Simple MVVM, like MVVM Light, does include a MessageBus, but there is a different implementation. MVVM Light allows for any kind of message token and has all kinds of message types – dialog, generic, notification (with and without callback), and property changed. I chose a simpler approach, allowing only string tokens and a one message type: NotificationEventArgs (with or without callback), which is also used for event-based communication between Views and ViewModels. The simplicity of this design is its strength: it is both easy to use and it encompasses every kind of communication you would want to perform.
Speaking of ease of use, the ViewModelBase class in Simple MVVM has helper methods for sending and receiving messages. And the MessageBus uses a leak-proof eventing model, which means you never have to worry about unregistering with the MessageBus. Subscribers are weakly referenced, so the MessageBus will not prevent them from being garbage collected, thereby avoiding a source of memory leaks. (Note: While the Messenger in v3 of MVVM Light has a bug that causes a memory leak if you fail to unregister, for v4 Laurent intends to use the same leak-proof eventing model as Simple MVVM.)
Reference: Simple MVVM Toolkit versus MVVM Light Toolkit from our NCG partner Tony Sneed at the Tony Sneed’s blog.