Hands-On Model-View-ViewModel (MVVM) for Silverlight and WPF

Over the years I have spent a fair amount of time thinking about design patterns surrounding the presentation layer. Just as your values as a grown-up are often determined by experiences you have as a child – I think my interest in these patterns stem from frustrations I had as a newbie developer.

When I first started, I made my way onto a team developing software for a financial company. The obvious pre-requisites emerged: quality and accuracy were paramount to the success of the project, and the features and scope of the project were always expanding. The problem was soon I realized that the way this application was built would compromise our ability to deliver to the necessary standards.

The problems included:

  • Web pages with over 2,000 lines of code in Page_Load event method
  • A complete disregard for any attempt to automate testing
  • Un-extensible architecture

... and host of other issues pushed me to learn how to avoid these problems in the future.

A drive to push as much code of the the user interface layer seem to be a step in the right direction. While by no means a silver bullet, breaking apart the application would help bring some order to the chaos and provide a foundation for automated testing and application extensibility. The usual suspects for this kind of separation are Model-View-Controller (MVC) and it’s derivative patterns.

Presentation Models for XAML Applications

With the emergence of WPF, the change in architecture brought about a need to shift the approach of separation of concerns in the presentation layer. The approach of Model-View-Controller and it’s offspring were an obvious choice, but the existing models were required no small amount of shoe-horning to make a comfortable match for XAML applications.

Looking to apply a pattern with a more natural fit John Gossman helped popularize Model-View-ViewModel (MVVM) for use in WPF. (He and his team even went as far as to use the pattern while developing Expression Blend1.) As WPF matured and Silverlight began to emerge, this time MVVM seemed like the go-to pattern, but resources with concrete instruction are few and far between.

Looking for Guidance

As with all the Model View * patterns, there usually follows a learning curve to anyone new to the approach. Therefore I set out to find a way to provide an "apples to apples" comparison of how to implement MVVM in both Silverlight and WPF. 

Comparing apples to apples: Implementing Model-View-ViewModel in Silverlight and WPF

I decided to create some long-form screencasts that will help teach the concepts of MVVM. I contacted Microsoft Program Manager Tim Heuer to tackle the Silverlight project and WPF Super Ninja Josh Smith to lead the way for a WPF implementation.

The charge was simple: create a quiz application that harnesses the power of MVVM. The result is the following two screencasts (each an hour in length) that dive into the details of using MVVM.

Implementing Model-View-ViewModel in Silverlight   Implementing Model-View-ViewModel in Silverlight

If you are new to Silverlight, WPF and MVVM, I suggest you begin by watching the Silverlight video first. Tim does a great job of explaining the pattern from the ground up and showing how all the dots are connected. Once you are a little more comfortable with the basics, Josh’s video attempts to take a more real-world looking domain model and prepare it for his WPF application with a clean ViewModel interface. Both are worth the time as each presenter did an excellent job of demonstrating their approach.

I hope you find them useful!

Resources

For further reading on Model-View-ViewModel:

Foot Notes

  1. John describes this experience using MVVM while developing Expression Blend during a Pixel8 interview: John Gossman Architects WPF
Published Thursday, February 26, 2009 6:36 AM by craigshoemaker

Comments

# re: Hands-On Model-View-ViewModel (MVVM) for Silverlight and WPF

Thursday, February 26, 2009 11:16 AM by Josh Smith

Thanks a lot for taking the time to produce these videos, Craig.  I'm honored to be a part of this project! :)

# re: Hands-On Model-View-ViewModel (MVVM) for Silverlight and WPF

Thursday, February 26, 2009 12:14 PM by craigshoemaker

Say nothing of it, Josh... glad to have you aboard!

For those interested Josh and I are already discussing a follow up. No promises on when it will be available, but we are tossing some ideas around.

# ASP.NET MVC Archived Blog Posts, Page 1

Thursday, February 26, 2009 1:59 PM by ASP.NET MVC Archived Blog Posts, Page 1

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

# Hands-On Model-View-ViewModel (MVVM) for Silverlight and WPF - Craig Shoemaker

Thursday, February 26, 2009 4:32 PM by DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

# re: Hands-On Model-View-ViewModel (MVVM) for Silverlight and WPF

Friday, February 27, 2009 4:38 PM by Greg Duncan

Nice! I'm trying to get up to speed on MVVM now so the timing on these are great!

These will appear in the podcast feed right? (They aren't currently there, at least not at, feeds2.feedburner.com/Pixel8 )

# re: Hands-On Model-View-ViewModel (MVVM) for Silverlight and WPF

Friday, February 27, 2009 5:09 PM by craigshoemaker

I've avoided putting videos in the feed since they are so large... but I suppose I should add them. thanks, Greg!

# ???Implementing MVVM in Silverlight??? ??? a UI perspective

Pingback from  ???Implementing MVVM in Silverlight??? ??? a UI perspective

# Designer WPF » Blog Archive » Bookmarks for February 27th

Pingback from  Designer WPF  » Blog Archive   » Bookmarks for February 27th

# This Week on C9: TechFest, Windows 7, Dev Tools, and WPF in Visual Studio 2010 | CHARGED's Digital Lifestyle at Work or Play

Pingback from  This Week on C9: TechFest, Windows 7, Dev Tools, and WPF in Visual Studio 2010 | CHARGED's Digital Lifestyle at Work or Play

# Silverlight MVVM + dependency injection « Burela’s house-o-blog

Pingback from  Silverlight MVVM + dependency injection « Burela’s house-o-blog

# Silverlight Design Pattern: the Model View ViewModel (MVVM)

Monday, March 09, 2009 5:35 PM by vdcruijsen.net

Silverlight Design Pattern: the Model View ViewModel (MVVM)

# Binary Bob’s Blog » My MVVM in Silverlight notes

Monday, May 18, 2009 7:48 PM by Binary Bob’s Blog » My MVVM in Silverlight notes

Pingback from  Binary Bob’s Blog » My MVVM in Silverlight notes

# re: Hands-On Model-View-ViewModel (MVVM) for Silverlight and WPF

Wednesday, May 20, 2009 2:44 PM by Ben Hayat

Hi Craig;

Great questions by you and great answers by Tim. I love these types of screen casts when one person is asking questions and the other responds. Lots more to learn from.

Tim is great. I always enjoy his way of coming across.

Thanks guys!

..Ben

# re: Hands-On Model-View-ViewModel (MVVM) for Silverlight and WPF

Saturday, July 04, 2009 6:25 AM by jbe

Interested in more resources with concrete instruction on how to implement the Model-View-ViewModel pattern with WPF?

Then you might have a look at the WPF Application Framework (WAF) project: http://waf.codeplex.com

# hotgazpacho » Blog Archive » Daily Digest for July 25th

Pingback from  hotgazpacho  » Blog Archive   » Daily Digest for July 25th

# re: Hands-On Model-View-ViewModel (MVVM) for Silverlight and WPF

Saturday, September 05, 2009 5:05 PM by hk

the silverlight url has been changed:

community.infragistics.com/.../91949.aspx

is the correct url

Leave a Comment

(required) 
(required) 
(optional)
(required)