<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Craig Shoemaker : MVVM</title><link>http://weblogs.asp.net/craigshoemaker/archive/tags/MVVM/default.aspx</link><description>Tags: MVVM</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>VM Workshop: Model View ViewModel (MVVM) and the Presentation Model Pattern in 5 UI Platforms</title><link>http://weblogs.asp.net/craigshoemaker/archive/2009/11/03/vm-workshop-model-view-viewmodel-mvvm-and-the-presentation-model-pattern-in-5-ui-platforms.aspx</link><pubDate>Tue, 03 Nov 2009 16:51:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7245973</guid><dc:creator>craigshoemaker</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/craigshoemaker/rsscomments.aspx?PostID=7245973</wfw:commentRss><comments>http://weblogs.asp.net/craigshoemaker/archive/2009/11/03/vm-workshop-model-view-viewmodel-mvvm-and-the-presentation-model-pattern-in-5-ui-platforms.aspx#comments</comments><description>
&lt;p&gt;For some reason the family of design patterns that exist around the &lt;a href="http://en.wikipedia.org/wiki/Model-view-controller" mce_href="http://en.wikipedia.org/wiki/Model-view-controller"&gt;Model View Controller&lt;/a&gt; pattern seem to be an elusive band of characters. The first time I&amp;nbsp; encountered Model View Controller I studied the text hard trying to understand how the &lt;a href="http://en.wikipedia.org/wiki/Strategy_pattern" mce_href="http://en.wikipedia.org/wiki/Strategy_pattern"&gt;Strategy&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Observer_pattern" mce_href="http://en.wikipedia.org/wiki/Observer_pattern"&gt;Observer&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Composite_pattern" mce_href="http://en.wikipedia.org/wiki/Composite_pattern"&gt;Composite&lt;/a&gt; patterns worked together to accomplish some goal that – try as I might - remained fuzzy. Somehow I have a sense that I am not alone.&lt;/p&gt;

&lt;p&gt;Model View Controller (MVC) is the parent pattern to a number of contemporary patterns that seem to find a natural home in Microsoft development. Variations of MVC have appeared over the years in an attempt to work within the construct of modern event-driven programming models. One of most distinguishing differences among MVC and some of the derivative patterns is that with MVC the controller executes first, where with the other patterns the view tends to execute first and then delegates control to some sort of “presentation” class.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://polymorphicpodcast.com/vmworkshop/patterns.gif" alt="VM Workshop Patterns" mce_src="http://polymorphicpodcast.com/vmworkshop/patterns.gif" height="382" width="550"&gt;&lt;/p&gt;

&lt;p&gt;A few years ago I did a series on Model View Presenter (MVP) [&lt;a href="http://polymorphicpodcast.com/shows/mv-patterns/" mce_href="http://polymorphicpodcast.com/shows/mv-patterns/"&gt;Design Patterns Bootcamp: Model View * Patterns&lt;/a&gt;] in an attempt to discuss how to completely abstract away the user interface layer. MVP proves to be an effective pattern, but can be confusing to some with the heavy use of interfaces and events.&lt;/p&gt;

&lt;p&gt;The recent popularity of XAML applications has brought focus to the &lt;a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx" mce_href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx"&gt;Model View ViewModel&lt;/a&gt; (MVVM) pattern, which is technically a fine-tuned version of &lt;a href="http://martinfowler.com/eaaDev/PresentationModel.html" mce_href="http://martinfowler.com/eaaDev/PresentationModel.html"&gt;Presentation Model&lt;/a&gt; that &lt;a href="http://blogs.msdn.com/johngossman/archive/2005/10/11/479632.aspx" mce_href="http://blogs.msdn.com/johngossman/archive/2005/10/11/479632.aspx"&gt;takes advantage of .NET’s data binding framework&lt;/a&gt;. Further, the advent of ASP.NET MVC has created a strong push for some sort of presentation strategy into the realm of ASP.NET WebForms.&lt;/p&gt;

&lt;p&gt;So this history lesson is great, but how do you do you write the code to implement the patterns?&lt;/p&gt;

&lt;h2&gt;Introducing VM Workshop&lt;/h2&gt;

&lt;p&gt;The &lt;a href="http://polymorphicpodcast.com/vmworkshop" mce_href="http://polymorphicpodcast.com/vmworkshop"&gt;VM Workshop&lt;/a&gt; is a simple reference application demonstrating the Model View ViewModel and Presentation Model pattern in a number of different UI platforms.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://polymorphicpodcast.com/vmworkshop/" mce_href="http://polymorphicpodcast.com/vmworkshop/"&gt;&lt;img src="http://polymorphicpodcast.com/vmworkshop/logo.png" mce_src="http://polymorphicpodcast.com/vmworkshop/logo.png" align="right" border="0" height="227" hspace="15" vspace="15" width="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The project includes examples in:&lt;/p&gt;

&lt;ul&gt;
  
&lt;li&gt;Silverlight &lt;/li&gt;

  
&lt;li&gt;WPF &lt;/li&gt;

  
&lt;li&gt;WebForms &lt;/li&gt;

  
&lt;li&gt;ASP.NET MVC &lt;/li&gt;

  
&lt;li&gt;Ajax &lt;/li&gt;

  
&lt;li&gt;WinForms &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;…and as an &lt;a href="http://vmworkshop.codeplex.com/" mce_href="http://vmworkshop.codeplex.com/"&gt;open source project&lt;/a&gt;, you are invited to help add examples of how to deal with a myriad of different UI use cases!&lt;/p&gt;

&lt;p&gt;The purpose of the VM Workshop is to be a working programmer’s reference, not an exposition in pattern purity. Therefore you may see small details that may not fit the textbook definition of the patterns, but the point is to provide a template for effective pattern use. For instance there is an example in ASP.NET MVC is included in VM Workshop. Obviously the pattern being implemented is Model View Controller, but the construction of a “view model” class is still relevant in this context. While referring to ViewModel class in a ASP.NET application may be technically inappropriate, I use the term “view model” to interchangeably refer to a ViewModel or Presentation Model class for simplicity. &lt;/p&gt;

&lt;p&gt;I recently messaged &lt;a href="http://blogs.msdn.com/johngossman/" mce_href="http://blogs.msdn.com/johngossman/"&gt;John Gossman&lt;/a&gt;, arguably one of the most visible proponent of MVVM, to ask whether or not MVVM is exclusive to XMAL applications or if the pattern may be applied to any UI platform. John replied saying:&lt;/p&gt;

&lt;blockquote&gt;
 
&lt;p&gt;&lt;i&gt;I think the pattern is completely applicable to any UI technology.&amp;nbsp; There are some details around the use of commands and data-binding that tend to be XAML specific, so I tend to use MVVM for the XAML version of the pattern and to keep from having religious wars with Patterns zealots.&amp;nbsp; The more general pattern is called PresentationModel by Fowler.&amp;nbsp; OTOH, one of the nice things is if you design your Model and ViewModel’s correctly you should be able to reuse them between XAML-based and other UI technologies.&lt;/i&gt; &lt;br&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;VM Workshop is meant to be a learn-by-example project showcasing different styles of implementation.&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;Examples in C# and VB.NET&lt;/h3&gt;

&lt;p&gt;Often discussions surrounding design patterns tend to only be available in C#. In order to extend the conversation to as many developers as possible, I’ve include a VB.NET solution that operates in complete parity to the C# version. The only difference in the VB.NET version is that due to lack of lambda support some of the places &lt;a href="http://www.codeplex.com/AutoMapper" mce_href="http://www.codeplex.com/AutoMapper"&gt;AutoMapper&lt;/a&gt; is leveraged in the C# version are removed and objects are mapped manually. &lt;/p&gt;

&lt;h2&gt;A Grid, a Form and an Action&lt;/h2&gt;

&lt;p&gt;The initial example is provided for all the UI platforms. Each implementation features the same UI and workflow composition. First a grid is presented to the user:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://polymorphicpodcast.com/vmworkshop/grid.png" alt="Grid" mce_src="http://polymorphicpodcast.com/vmworkshop/grid.png" height="278" width="234"&gt;&lt;/p&gt;

&lt;p&gt;Upon selecting an individual item, an edit form becomes available allowing the user make changes to the item:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://polymorphicpodcast.com/vmworkshop/form.png" alt="Form" mce_src="http://polymorphicpodcast.com/vmworkshop/form.png" height="257" width="372"&gt;&lt;/p&gt;

&lt;blockquote&gt;
  
&lt;p&gt;&lt;b&gt;Note:&lt;/b&gt; These screen shots here are taken from the WebForms sample, but the UI reflects the appropriate look for each of the respective UI technologies.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;p&gt;Once changes are complete, and the ‘Save' button is pressed, the persistence operation is initiated. After persistence is complete, each implementation tells the UI to how to response respond to the action.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Please note that true persistence is not implemented in VM Workshop.&lt;/b&gt; In an effort to avoid using a database (which keeps the code more portable) persistence functions are simply stubbed out in the code. In the end, the project includes the same messages that would be sent among the application layers in any real-world application. &lt;/p&gt;

&lt;h2&gt;Minimizing Code in the View&lt;/h2&gt;

&lt;p&gt;The motivation for using these patterns is often creating a more testable user interface layer, decoupling your application from UI technologies or perhaps to promote better application composition. No matter what the motivation, the overarching goal is to remove application logic from the view layer. The examples in VM Workshop use a number of general concepts to achieve a thin view. &lt;/p&gt;

&lt;h3&gt;Lazy Loading&lt;/h3&gt;

&lt;p&gt;The grids are populated through a lazy loaded property on the view model class. This ensures that the request for data is restricted to only the times when the data wasn’t previously prepared. Another benefit is that a call to somehow ‘get stuff’ is not required. When the grid binds to the 'Products' property the property’s implementation knows where to get the data if it doesn’t have it.&lt;/p&gt;

&lt;h3&gt;Visibility Properties&lt;/h3&gt;

&lt;p&gt;Visibility properties (in XAML implementations) or boolean properties on the view model classes are used to control whether or not controls, messages or other UI elements appear to the user. Binding the state of these properties to the appropriate elements on the screen relieves the view of having to decide what is shown to the user and what is hidden.&lt;/p&gt;

&lt;h3&gt;No Logical Operations&lt;/h3&gt;

&lt;p&gt;While taking great care to remove all logical operations from the view, even seemingly benign tasks such as casting input into primitive types is delegated to the view model or some other class for execution. Even simple operations like type casting can quickly cascade into a series of business rules. Consider how convoluted validation scenarios can become when the application is expecting a currency value, but is one way or another served a string. While the validation does have UI implications, this type of logic is best handled in a lower level than the view.&lt;/p&gt;

&lt;h3&gt;Presentation Objects and Data Transfer Objects&lt;/h3&gt;

&lt;p&gt;The use of presentation objects or data transfer objects, or DTOs, (I tend to flip between terms depending on my mood) is used to accomplish at least two goals:&lt;/p&gt;

&lt;ol&gt;
  
&lt;li&gt;&lt;b&gt;Hide the domain model from the rest of the application.&lt;/b&gt; Concealing the full-fledged Products class from the view makes sure unnecessary pressure is not exerted on the model to mutate at the whim of the view. 
    &lt;br&gt;&lt;br&gt;&lt;/li&gt;

  
&lt;li&gt;&lt;b&gt;Make custom mapping the responsibility of the type.&lt;/b&gt; The presentation object’s job is to prepare the data coming from the domain model into a form that is natural to the view. This mapping or conversion responsibility is best placed in the hands of the presentation object and not within the procedure of an operation. &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;Get Acquainted&lt;/h2&gt;

&lt;p&gt;Check out the video that will get you oriented with VM Workshop:&lt;/p&gt;

&lt;p&gt;
&lt;object height="344" width="425"&gt;
&lt;param name="movie" value="http://www.youtube.com/v/zXIzIzbO7pM&amp;amp;hl=en&amp;amp;fs=1&amp;amp;"&gt;
&lt;param name="allowFullScreen" value="true"&gt;
&lt;param name="allowscriptaccess" value="always"&gt;
&lt;embed src="http://www.youtube.com/v/zXIzIzbO7pM&amp;amp;hl=en&amp;amp;fs=1&amp;amp;" mce_src="http://www.youtube.com/v/zXIzIzbO7pM&amp;amp;hl=en&amp;amp;fs=1&amp;amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" height="344" width="425"&gt;&lt;/object&gt;
&lt;/p&gt;

&lt;h2&gt;Get Involved&lt;/h2&gt;

&lt;p&gt;The code for &lt;a href="http://vmworkshop.codeplex.com" mce_href="http://vmworkshop.codeplex.com"&gt;VM Workshop is available on CodePlex&lt;/a&gt; and you are encouraged to submit your implementations into the workshop!&lt;/p&gt;

&lt;h2&gt;Resources&lt;/h2&gt;

&lt;p&gt;VM Workshop uses &lt;a href="http://nbuilder.org/" mce_href="http://nbuilder.org/"&gt;nBuilder&lt;/a&gt; create data objects and &lt;a href="http://www.codeplex.com/AutoMapper" mce_href="http://www.codeplex.com/AutoMapper"&gt;AutoMapper&lt;/a&gt; for DTO to domain object mapping.&lt;/p&gt;

&lt;h2&gt;Thanks&lt;/h2&gt;

&lt;p&gt;Thanks to &lt;a href="http://infragistics.com/" mce_href="http://infragistics.com/"&gt;Infragistics&lt;/a&gt; for providing the time to develop the VM Workshop.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7245973" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/Design+Patterns/default.aspx">Design Patterns</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/AJAX/default.aspx">AJAX</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/WPF/default.aspx">WPF</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/MVC/default.aspx">MVC</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/windows+forms/default.aspx">windows forms</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/MVVM/default.aspx">MVVM</category></item><item><title>Expression Blend Design Time Data Using MVVM </title><link>http://weblogs.asp.net/craigshoemaker/archive/2009/08/11/expression-blend-design-time-data-using-mvvm.aspx</link><pubDate>Tue, 11 Aug 2009 22:12:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7166884</guid><dc:creator>craigshoemaker</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/craigshoemaker/rsscomments.aspx?PostID=7166884</wfw:commentRss><comments>http://weblogs.asp.net/craigshoemaker/archive/2009/08/11/expression-blend-design-time-data-using-mvvm.aspx#comments</comments><description>&lt;p&gt;Today’s show features Microsoft MVP and &lt;a href="http://identitymine.com/"&gt;Identity Mine&lt;/a&gt; integrator &lt;a href="http://blog.galasoft.ch/"&gt;Laurent Bugnion&lt;/a&gt;. Laurent discusses his experience with working in XAML applications that use MVVM and working with developers and designers to create beautiful applications.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://polymorphicpodcast.com/shows/jquery/" mce_href="http://polymorphicpodcast.com/shows/jquery/"&gt;&lt;img src="http://polymorphicpodcast.com/images/ListenBanner.png" title="Listen to the Show" alt="Listen to the Show" mce_src="http://polymorphicpodcast.com/images/ListenBanner.png" border="0" height="58" width="378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;table border="0" cellpadding="3" cellspacing="3"&gt;&lt;tbody&gt;

    &lt;tr&gt;
      &lt;th align="left"&gt;Full Speed:&lt;/th&gt;

      &lt;td&gt;&lt;a href="http://polymorphicpodcast.com/podcast/files/PolymorphicPodcast-2009-08-11-blendmvvm.mp3"&gt;download&lt;/a&gt;&lt;/td&gt;

      &lt;td&gt;&lt;object type="application/x-shockwave-flash" data="http://polymorphicpodcast.com/plugins/audio-player/player.swf" id="audioplayer1" height="24" width="290"&gt;     &lt;param name="movie" value="http://polymorphicpodcast.com/plugins/audio-player/player.swf"&gt;     &lt;param name="FlashVars" value="playerID=1&amp;amp;bg=0xE9E6DF&amp;amp;leftbg=0xEEEEEE&amp;amp;lefticon=0x666666&amp;amp;rightbg=0xCCCCCC&amp;amp;rightbghover=0x999999&amp;amp;righticon=0x666666&amp;amp;righticonhover=0xFFFFFF&amp;amp;text=0x666666&amp;amp;slider=0x666666&amp;amp;track=0xFFFFFF&amp;amp;border=0x666666&amp;amp;loader=0xCCCCCC&amp;amp;soundFile=http://polymorphicpodcast.com/podcast/files/PolymorphicPodcast-2009-08-11-blendmvvm.mp3"&gt;     &lt;param name="quality" value="high"&gt;     &lt;param name="wmode" value="transparent"&gt;     &lt;/object&gt;&lt;/td&gt;

    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;th align="left"&gt;Fast Version:&lt;/th&gt;

      &lt;td&gt;&lt;a href="http://polymorphicpodcast.com/podcast/files/PolymorphicPodcast-2009-08-11-blendmvvm_fast.mp3"&gt;download&lt;/a&gt;&lt;/td&gt;

      &lt;td&gt;&lt;object type="application/x-shockwave-flash" data="http://polymorphicpodcast.com/plugins/audio-player/player.swf" id="audioplayer1" height="24" width="290"&gt;     &lt;param name="movie" value="http://polymorphicpodcast.com/plugins/audio-player/player.swf"&gt;     &lt;param name="FlashVars" value="playerID=1&amp;amp;bg=0xE9E6DF&amp;amp;leftbg=0xEEEEEE&amp;amp;lefticon=0x666666&amp;amp;rightbg=0xCCCCCC&amp;amp;rightbghover=0x999999&amp;amp;righticon=0x666666&amp;amp;righticonhover=0xFFFFFF&amp;amp;text=0x666666&amp;amp;slider=0x666666&amp;amp;track=0xFFFFFF&amp;amp;border=0x666666&amp;amp;loader=0xCCCCCC&amp;amp;soundFile=http://polymorphicpodcast.com/podcast/files/PolymorphicPodcast-2009-08-11-blendmvvm_fast.mp3"&gt;     &lt;param name="quality" value="high"&gt;     &lt;param name="wmode" value="transparent"&gt;     &lt;/object&gt;&lt;/td&gt;

    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;


&lt;p&gt;The interview includes the following topics:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Origins of MVVM
    &lt;br&gt;&lt;/li&gt;

  &lt;li&gt;Advantages of using Blend
    &lt;br&gt;&lt;/li&gt;

  &lt;li&gt;Role of integrator
    &lt;br&gt;&lt;/li&gt;

  &lt;li&gt;Providing design time data to Blend with MVVM includes the answers to these questions:&lt;/li&gt;

  &lt;ul&gt;
    &lt;li&gt;Who prepares the data?&lt;/li&gt;

    &lt;li&gt;What are the mechanics that Blend uses to display design time data?&lt;/li&gt;

    &lt;li&gt;Where does the data live in the program?&lt;/li&gt;

    &lt;li&gt;How is the data provided to Blend?
      &lt;br&gt;&lt;/li&gt;
  &lt;/ul&gt;

  &lt;li&gt;Ways to overcome paint points in Blend&lt;/li&gt;

  &lt;ul&gt;

    &lt;li&gt;Applications must pass the “Blendability” test&lt;/li&gt;

    &lt;ul&gt;
      &lt;li&gt;Failure is often dictated by poorly abstracted dependencies on databases, services etc.&lt;/li&gt;
    &lt;/ul&gt;

    &lt;li&gt;Test often in Blend&lt;/li&gt;
  &lt;/ul&gt;

&lt;/ul&gt;

&lt;h2&gt;Resources&lt;/h2&gt;

&lt;p&gt;The following links are resources mentioned in the show:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://community.infragistics.com/pixel8/media/p/91950.aspx"&gt;Implementing MVVM in WPF&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;&lt;a href="http://community.infragistics.com/pixel8/media/p/91949.aspx"&gt;Implementing MVVM in Silverlight&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;&lt;a href="http://joshsmithonwpf.wordpress.com/"&gt;Josh Smith&lt;/a&gt;’s &lt;a href="http://mvvmfoundation.codeplex.com/"&gt;MVVM Foundation&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;Polymorphic Podcast &lt;a href="http://polymorphicpodcast.com/"&gt;FastFeed&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7166884" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/Design+Patterns/default.aspx">Design Patterns</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/Podcast/default.aspx">Podcast</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/WPF/default.aspx">WPF</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/Polymorphic+Podcast/default.aspx">Polymorphic Podcast</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/MVVM/default.aspx">MVVM</category></item><item><title>Hands-On Model-View-ViewModel (MVVM) for Silverlight and WPF</title><link>http://weblogs.asp.net/craigshoemaker/archive/2009/02/26/hands-on-model-view-viewmodel-mvvm-for-silverlight-and-wpf.aspx</link><pubDate>Thu, 26 Feb 2009 14:36:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6929405</guid><dc:creator>craigshoemaker</dc:creator><slash:comments>18</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/craigshoemaker/rsscomments.aspx?PostID=6929405</wfw:commentRss><comments>http://weblogs.asp.net/craigshoemaker/archive/2009/02/26/hands-on-model-view-viewmodel-mvvm-for-silverlight-and-wpf.aspx#comments</comments><description>&lt;p&gt;Over the years I have spent a fair amount of time thinking about &lt;a href="http://polymorphicpodcast.com/shows/mv-patterns/" mce_href="http://polymorphicpodcast.com/shows/mv-patterns/"&gt;design patterns surrounding the presentation layer&lt;/a&gt;. 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.&lt;/p&gt;  &lt;p&gt;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.&lt;/p&gt;  &lt;p&gt;The problems included:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Web pages with over 2,000 lines of code in Page_Load event method&lt;/li&gt;    &lt;li&gt;A complete disregard for any attempt to automate testing&lt;/li&gt;    &lt;li&gt;Un-extensible architecture &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;... and host of other issues pushed me to learn how to avoid these problems in the future.&lt;/p&gt;  &lt;p&gt;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 &lt;a href="http://en.wikipedia.org/wiki/Model_view_controller" mce_href="http://en.wikipedia.org/wiki/Model_view_controller"&gt;Model-View-Controller (MVC)&lt;/a&gt; and it’s derivative patterns.&lt;/p&gt;  &lt;h2&gt;Presentation Models for XAML Applications&lt;/h2&gt;  &lt;p&gt;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. &lt;/p&gt;  &lt;p&gt;Looking to apply a pattern with a more natural fit &lt;a href="http://blogs.msdn.com/johngossman/" mce_href="http://blogs.msdn.com/johngossman/"&gt;John Gossman&lt;/a&gt; helped popularize &lt;a href="http://blogs.msdn.com/johngossman/" mce_href="http://blogs.msdn.com/johngossman/"&gt;Model-View-ViewModel (MVVM)&lt;/a&gt; for use in WPF. (He and his team even went as far as to use the pattern while &lt;a href="http://weblogs.asp.net/controlpanel/blogs/posteditor.aspx?SelectedNavItem=Posts&amp;amp;sectionid=600&amp;amp;postid=6929405#devBlend" mce_href="http://weblogs.asp.net/controlpanel/blogs/posteditor.aspx?SelectedNavItem=Posts&amp;amp;sectionid=600&amp;amp;postid=6929405#devBlend"&gt;developing Expression Blend&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;.) 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.&lt;/p&gt;  &lt;h2&gt;Looking for Guidance&lt;/h2&gt;  &lt;p&gt;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.&amp;nbsp; &lt;/p&gt;  &lt;p&gt;&lt;img src="http://download.infragistics.com/pixel8/images/mvvm-sl-wpf.png" style="display: block; float: none; margin-left: auto; margin-right: auto;" alt="Comparing apples to apples: Implementing Model-View-ViewModel in Silverlight and WPF" mce_src="http://download.infragistics.com/pixel8/images/mvvm-sl-wpf.png" border="0" height="345" width="346"&gt;&lt;/p&gt;  &lt;p&gt;I decided to create some long-form screencasts that will help teach the concepts of MVVM. I contacted Microsoft Program Manager &lt;a href="http://timheuer.com/blog/" mce_href="http://timheuer.com/blog/"&gt;Tim Heuer&lt;/a&gt; to tackle the Silverlight project and WPF Super Ninja &lt;a href="http://joshsmithonwpf.wordpress.com/" mce_href="http://joshsmithonwpf.wordpress.com/"&gt;Josh Smith&lt;/a&gt; to lead the way for a WPF implementation. &lt;/p&gt;  &lt;p&gt;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.&lt;/p&gt;  &lt;p align="center"&gt;&lt;a href="http://community.infragistics.com/pixel8/media/p/91949.aspx" mce_href="http://community.infragistics.com/pixel8/media/p/91949.aspx"&gt;&lt;img src="http://download.infragistics.com/pixel8/images/btn-mvvm-sl.png" alt="Implementing Model-View-ViewModel in Silverlight" mce_src="http://download.infragistics.com/pixel8/images/btn-mvvm-sl.png" border="0" height="188" width="250"&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp; &lt;a href="http://community.infragistics.com/pixel8/media/p/91950.aspx" mce_href="http://community.infragistics.com/pixel8/media/p/91950.aspx"&gt;&lt;img src="http://download.infragistics.com/pixel8/images/btn-mvvm-wpf.png" alt="Implementing Model-View-ViewModel in Silverlight" mce_src="http://download.infragistics.com/pixel8/images/btn-mvvm-wpf.png" border="0" height="188" width="250"&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p align="left"&gt;If you are new to Silverlight, WPF and MVVM, I suggest you &lt;a href="http://community.infragistics.com/pixel8/media/p/91949.aspx" mce_href="http://community.infragistics.com/pixel8/media/p/91949.aspx"&gt;begin by watching the Silverlight video first&lt;/a&gt;. 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, &lt;a href="http://community.infragistics.com/pixel8/media/p/91950.aspx" mce_href="http://community.infragistics.com/pixel8/media/p/91950.aspx"&gt;Josh’s video attempts to take a more real-world looking domain model and prepare it for his WPF application&lt;/a&gt; with a clean ViewModel interface. Both are worth the time as each presenter did an excellent job of demonstrating their approach.&lt;/p&gt;  &lt;p align="left"&gt;I hope you find them useful!&lt;/p&gt;  &lt;h2&gt;Resources&lt;/h2&gt;  &lt;p&gt;For further reading on Model-View-ViewModel:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx" mce_href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx"&gt;WPF Apps With The Model-View-ViewModel Design Pattern&lt;/a&gt; by Josh Smith &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/johngossman/archive/2005/10/08/478683.aspx" mce_href="http://blogs.msdn.com/johngossman/archive/2005/10/08/478683.aspx"&gt;Introduction to Model/View/ViewModel pattern for building WPF apps&lt;/a&gt; by John Gossman &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;nbsp;&lt;/p&gt;  &lt;h3&gt;Foot Notes&lt;/h3&gt;  &lt;ol&gt;   &lt;li id="devBlend"&gt;John describes this experience using MVVM while developing Expression Blend during a Pixel8 interview: &lt;a href="http://community.infragistics.com/pixel8/media/p/91915.aspx" mce_href="http://community.infragistics.com/pixel8/media/p/91915.aspx"&gt;John Gossman Architects WPF&lt;/a&gt; &lt;/li&gt; &lt;/ol&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6929405" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/Design+Patterns/default.aspx">Design Patterns</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/Podcast/default.aspx">Podcast</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/WPF/default.aspx">WPF</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/Pixel8/default.aspx">Pixel8</category><category domain="http://weblogs.asp.net/craigshoemaker/archive/tags/MVVM/default.aspx">MVVM</category></item></channel></rss>