Archive for August, 2009

Comparing Cairngorm and Model View ViewModel

Several months ago at our RIA meetup, I presented a short comparison of two client-side flex architectures: Cairngorm and Model View ViewModel. I’ve used both of these architectures in large-scale production projects, but for the purpose of simple comparison, I decided to demonstrate the two designs by implementing an identical application twice – once in each architecture.

If you aren’t familiar with the basics of these two RIA architecture patterns you can learn more here:

This post will not cover the basics of these two patters, quick google searches will reveal lots of that type of information. Rather, below, you’ll find links to the code for the two – functionally equivalent – RSS reader applications followed by a quick highlight comparison of how these two patterns compare.

To get started, grab the two projects: Cairngorm and MVVM. As you look through the code, note a few things:

  • I know it’s not perfect. This was done pretty quickly, and the group had the benefit of me explaining my intent as i went along
  • I know it’s too simple. This RSS reader example is really too simplistic to be considered a true demonstration of these two architectures. The application is really just a starting point for comparison rather than an exhaustive stress test. My comparisons and analysis is drawn more from previous experience than it is from the code that i post here.

That being said here are the major points for comparison:

  • Decoupling vs Clarity.  In general, Cairngorm – with it’s highly event-based architecture – decouples your view and your logic code more than MVVM does.  But that comes at a cost.  In Cairngorm, I often find it more difficult to trace the origin – and destination – of events.  Additionally, vanilla Cairngorm asks you to create a class for every single event in your system.  If you have a large system, that’s A LOT of events.  They eventually get confusing.  MVVM has less of this, but with the trade off that you need to be careful about putting too much logic or “smarts” in your ViewModel classes.  You need to find ways to decouple this logic without breaking the pattern
  • View Helpers vs ViewModels. When it comes down to it, despite what the flex docs (and my examples) seem to imply, more often than not, you’ll need to write code that interprets responses from the server before display.  In MVVM, the path to do this is relatively certain.  When a model is updated, your ViewModel hears the change, interprets the response, and passes the message on to the view.  With Cairngorm, if your goal is to keep logic outside of the MXML, you’ll likely need to write separate response classes that massage model data first and then place the data into the view.
  • Emphasis on Binding. While data binding can – and should – be used in both frameworks, MVVM really emphasizes data binding for just about everything.  In a well-factored MVVM view, everything, down to button state is bound to the ViewModel.  While this can be tedious at times, if you design your ViewModel classes well, this gives you a tremendous ability to do very comprehensive unit testing and have high confidence in the quality of your application.

I hope this was a helpful introductory overview of these two architectures.  Bottom line: they are both good but be sure to use your judgment when applying them.  Taken to an extreme, each has weaknesses.