<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Wizards of Smart &#187; ViewModel</title>
	<atom:link href="http://wizardsofsmart.net/tag/viewmodel/feed/" rel="self" type="application/rss+xml" />
	<link>http://wizardsofsmart.net</link>
	<description>.NET Design Patterns</description>
	<lastBuildDate>Thu, 29 Jul 2010 16:40:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0-beta2-14896</generator>
		<item>
		<title>A History of Violence against HTML</title>
		<link>http://wizardsofsmart.net/patterns/a-history-of-violence-against-html/</link>
		<comments>http://wizardsofsmart.net/patterns/a-history-of-violence-against-html/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 19:17:21 +0000</pubDate>
		<dc:creator>panesofglass</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[PresentationModel]]></category>
		<category><![CDATA[ViewModel]]></category>
		<category><![CDATA[Web Application]]></category>

		<guid isPermaLink="false">http://wizardsofsmart.net/?p=223</guid>
		<description><![CDATA[[This is the second in a series on the use of MVC for building web "applications".] HTML was designed as a document format with hypermedia connections to other, related documents. Today, however, we abuse HTML and treat it as an application platform. It somehow performs remarkably well in most cases. I suppose that’s a testament to [...]]]></description>
			<content:encoded><![CDATA[<p>[This is the second in a <a title="The Problem with the MVC Frameworks" href="http://wizardsofsmart.net/patterns/the-problem-with-the-mvc-frameworks-introduction/">series</a> on the use of MVC for building web "applications".]</p>
<p><span>HTML</span> was designed as a document format with hypermedia connections to other, related documents. Today, however, we abuse <span>HTML</span> and treat it as an application platform. It somehow performs remarkably well in most cases. I suppose that’s a testament to the agility of declarative markup. Nevertheless, I think we could all agree that <span>HTML</span> at least <em>started</em> life as a document format. If this is the case, I argue that <span>HTML</span> is not a view format; rather it is a serialization format for a model.</p>
<p>I’m sure many reading this are probably calling me a fool or worse. Well, don’t take my word for it. Let’s look at what some of the leading web developer/designers are saying:</p>
<blockquote><p>In markup, semantics is concerned with the meaning of an element and how that element describes the content it contains.<br />
-Molly E. Holzschlag (<a href="http://www.informit.com/articles/article.asp?p=369225&amp;rl=1" onclick="urchinTracker('/outgoing/www.informit.com/articles/article.asp?p=369225_amp_rl=1&amp;referer=');">http://www.informit.com/articles/article.asp?p=369225&amp;rl=1</a>)</p></blockquote>
<blockquote><p>Although it might be tempting to use the visual layout as the basis for the structure of your markup, this could lead to you overusing elements, particularly <code>&lt;div&gt;</code> elements. This will also result in establishing your content order primarily to accomplish the visual layout rather than it making sense when no style sheets are available. <strong>To avoid presentational markup and ordering problems, begin by first looking at the content and then working out from the meaning</strong>(emphasis mine).<br />
-Andy Clarke (<a href="http://www.transcendingcss.com/" onclick="urchinTracker('/outgoing/www.transcendingcss.com/?referer=');">Transcending <span>CSS</span></a>)</p></blockquote>
<blockquote><p>The notion that HTML is a display language has been proven long ago to be a <a title="Definition of 'Bad Thing' in the Jargon File." href="http://catb.org/jargon/html/B/Bad-Thing.html" onclick="urchinTracker('/outgoing/catb.org/jargon/html/B/Bad-Thing.html?referer=');">Bad Thing</a>. CSS taught us to separate our presentation from our content, and the doors that opened when we did it were invaluable. Today, however, we’re still delivering web applications as HTML documents with a bit of scripting layered on top. This works, but is strangled by all of HTML’s limitations. Fundamentally, <strong>HTML is not an application delivery format, it is a content description language</strong>, despite the <a href="http://www.sitepoint.com/blogs/2008/11/25/w3c-markup-validation-service-adds-experimental-html5-support/" onclick="urchinTracker('/outgoing/www.sitepoint.com/blogs/2008/11/25/w3c-markup-validation-service-adds-experimental-html5-support/?referer=');">valiant efforts of the HTML5 Working Group</a>.<br />
- Meitar Moscovitz (<a href="http://www.sitepoint.com/blogs/2008/12/22/svg-is-the-future-of-application-development/" onclick="urchinTracker('/outgoing/www.sitepoint.com/blogs/2008/12/22/svg-is-the-future-of-application-development/?referer=');">SVG Is The Future Of Application Development</a>)</p></blockquote>
<p>No, I can’t quote them directly, but I believe my argument has the same spirit. We should look at <span>HTML</span> as a PresentationModel, or ViewModel if you prefer, in the client (i.e. the browser). Therefore, <span>HTML</span> be treated as a serialization of your object model into a document format.</p>
<p>What then is the view? What’s the controller? These are excellent questions. Let’s think about how MVC works. The controller is responsible for receiving requests and relaying responses. The view is supposed to display the representation of the model. For the web, the controller easily aligns with the <span>HTTP</span> protocol and the view to the styles, transforms, and behaviors applied to the model (our <span>HTML</span> document). (I’ve used styles and transforms here separately to emphasize the potential use of <span>XSLT</span> stylesheets in addition to <span>CSS</span>.) Sure, using a document serialization, you could just display the model directly, and the confusion of <span>HTML</span> is here emphasized. It’s a model built out like a visual representation.</p>
<p>Are there some exceptions to this? Sure. If you want to treat <span>HTML</span> as a purely view format, then you can eliminate everything from your <span>HTML</span> except for JavaScript bindable areas and use <span>JSON</span> objects as your PresentationModels. Pass some of those around instead of <span>HTML</span>, and you’re really treating <span>HTML</span> like a true view. However, we don’t generally do that. We usually serialize the model into the <span>HTML</span> and apply our view aspects afterward via <span>CSS</span> and JavaScript.</p>
<p>If we really want to get serious about making an excellent MVC framework, we have to recognize and use our building blocks appropriately. <span>HTML</span> is a model serialization format. Treat it as such.</p>
]]></content:encoded>
			<wfw:commentRss>http://wizardsofsmart.net/patterns/a-history-of-violence-against-html/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ViewModel Futures</title>
		<link>http://wizardsofsmart.net/patterns/viewmodel-futures/</link>
		<comments>http://wizardsofsmart.net/patterns/viewmodel-futures/#comments</comments>
		<pubDate>Wed, 13 May 2009 04:10:49 +0000</pubDate>
		<dc:creator>panesofglass</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[MVP]]></category>
		<category><![CDATA[MVVM]]></category>
		<category><![CDATA[ViewModel]]></category>

		<guid isPermaLink="false">http://wizardsofsmart.net/patterns/viewmodel-futures</guid>
		<description><![CDATA[Glenn Block asked two questions on Twitter tonight that were too good to pass up. First, do you want better tooling, more testable data binding, or a ViewModel base class? I agree with whoever said patterns exist to make up for faults in a language; however, in this case, I think I would choose enhancements [...]]]></description>
			<content:encoded><![CDATA[<p>Glenn Block asked two questions on Twitter tonight that were too good to pass up. First, do you want better tooling, more testable data binding, or a ViewModel base class? I agree with whoever said patterns exist to make up for faults in a language; however, in this case, I think I would choose enhancements to data binding. It&#8217;s a little too magical, imho.</p>
<p>I would like to see a ViewModel base eventually, though, as that would be one less thing to wire up myself, and it hopefully silence all the people complaining about the lack of a model in MVC. (Yes, I am thinking the backend models should be sharable among WPF, Silverlight, and MVC.)</p>
<p>Glenn&#8217;s second question related to tv target audience. I am always a fan of simple explocicity, to borrow a word from JB. I think that&#8217;s what the experts use. Also, I don&#8217;t know many who want to me mediocre some day, so going for the best and letting it trickle down is a great idea.</p>
<p>Just my $.02.</p>
]]></content:encoded>
			<wfw:commentRss>http://wizardsofsmart.net/patterns/viewmodel-futures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Composite WPF Patterns</title>
		<link>http://wizardsofsmart.net/patterns/composite-wpf-patterns/</link>
		<comments>http://wizardsofsmart.net/patterns/composite-wpf-patterns/#comments</comments>
		<pubDate>Sat, 19 Jul 2008 13:00:55 +0000</pubDate>
		<dc:creator>panesofglass</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[CompositeWpf]]></category>
		<category><![CDATA[Dan Crevier]]></category>
		<category><![CDATA[DataBinding]]></category>
		<category><![CDATA[DelegateCommand]]></category>
		<category><![CDATA[John Grossman]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[MVVM]]></category>
		<category><![CDATA[Pete Weissbrod]]></category>
		<category><![CDATA[PresentationModel]]></category>
		<category><![CDATA[ViewModel]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://panesofglass.com/?p=10</guid>
		<description><![CDATA[Microsoft&#8217;sComposite Application Guidance for WPF (Composite WPF) gives WPF a lightweight yet effective application block with which to build exciting applications. However, the biggest struggle is deciding on a pattern for applying the library. The Model-View-ViewModel (MVVM), mentioned across the web as the perfect pattern for implementing MVC with WPF, was for some reason not specifically [...]]]></description>
			<content:encoded><![CDATA[<p>Microsoft&#8217;s<a href="http://msdn.microsoft.com/en-us/library/cc707819.aspx" onclick="urchinTracker('/outgoing/msdn.microsoft.com/en-us/library/cc707819.aspx?referer=');">Composite Application Guidance for WPF (Composite WPF)</a> gives <abbr title="Windows Presentation Foundation">WPF</abbr> a lightweight yet effective application block with which to build exciting applications. However, the biggest struggle is deciding on a pattern for applying the library. The <a href="http://blogs.sqlxml.org/bryantlikes/archive/2006/09/27/WPF-Patterns.aspx" onclick="urchinTracker('/outgoing/blogs.sqlxml.org/bryantlikes/archive/2006/09/27/WPF-Patterns.aspx?referer=');">Model-View-ViewModel (MVVM)</a>, mentioned across the web as the perfect pattern for implementing <abbr title="Model-View-Controller">MVC</abbr> with WPF, was for some reason not specifically mentioned in the Composite WPF documentation. Nevertheless, the MVVM pattern is many times used and alluded to under the name PresentationModel and is arguably the best approach in most cases.</p>
<p><a href="http://blogs.msdn.com/dancre/default.aspx" onclick="urchinTracker('/outgoing/blogs.msdn.com/dancre/default.aspx?referer=');">Dan Crevier</a> posted a <a href="http://blogs.msdn.com/dancre/archive/2006/10/11/datamodel-view-viewmodel-pattern-series.aspx" onclick="urchinTracker('/outgoing/blogs.msdn.com/dancre/archive/2006/10/11/datamodel-view-viewmodel-pattern-series.aspx?referer=');">series of excellent articles</a> defining the pattern and giving examples of the implementation of MVVM in WPF. These were written several years ago, before development of the Composite WPF block, but they are simple to understand and can give added understanding to the objects included in Composite WPF. For example, his post on <a href="http://blogs.msdn.com/dancre/archive/2006/09/15/dm-v-vm-part-7-encapsulating-commands.aspx" onclick="urchinTracker('/outgoing/blogs.msdn.com/dancre/archive/2006/09/15/dm-v-vm-part-7-encapsulating-commands.aspx?referer=');">encapsulating commands</a> is very similar to the <a href="http://msdn.microsoft.com/en-us/library/cc707894.aspx" onclick="urchinTracker('/outgoing/msdn.microsoft.com/en-us/library/cc707894.aspx?referer=');">DelegateCommand</a> object provided with CompositeWPF, though the latter uses lambdas to define Execute and CanExecute methods, whereas the former uses a base class and inheritance to define the methods. Using Dan&#8217;s example, the equivalent DelegateCommand, contained within the DelegateCommand&#8217;s definition, would look something like this:</p>
<pre name="code" class="c#">
public DelegateCommand&lt;object&gt; MyCommand { get; private set; }

public Window1()
{
    InitializeComponent();
    MyCommand = new DelegateCommand&lt;object&gt;(
        p =&gt; {
                 string text = p as string;
                 // Do something with text
             },
        p =&gt; !string.IsNullOrEmpty(p as string));
}
</pre>
<p>Dan&#8217;s definitions of the Model, which he calls DataModel, View and ViewModel match very closely with Composite WPF&#8217;s <a href="http://msdn.microsoft.com/en-us/library/cc707885.aspx" onclick="urchinTracker('/outgoing/msdn.microsoft.com/en-us/library/cc707885.aspx?referer=');">PresentationModel</a> pattern. In this case, the DataModel is a representative object that is fit for display within the View. Using the entities generated for <a href="http://msdn.microsoft.com/en-us/library/bb425822.aspx" onclick="urchinTracker('/outgoing/msdn.microsoft.com/en-us/library/bb425822.aspx?referer=');">Linq to SQL</a>, you might add properties to the partial classes create the necessary properties to which to bind. These classes should also implement INotifyPropertyChanged so that other DataModels can respond to changes from another, related View and update their data accordingly.</p>
<p>The View should use bindings for everything. The beauty of WPF is truly in its <a href="http://www.beacosta.com/blog/?cat=2" onclick="urchinTracker('/outgoing/www.beacosta.com/blog/?cat=2&amp;referer=');">DataBinding</a> functionality. The observer pattern is built right into the Binding, so that whenever one view changes, other views are notified by their bindings to their DataModels. Even the actions to be performed on a user interaction with the UI can be removed to the ViewModel, DataModel, or in the case of Composite WPF, a controller, Shell, or App level, as appropriate. This leaves the View looking very <a href="http://martinfowler.com/eaaDev/PassiveScreen.html" onclick="urchinTracker('/outgoing/martinfowler.com/eaaDev/PassiveScreen.html?referer=');">passive</a>, indeed, which greatly helps when unit testing.</p>
<p>The <a href="http://blogs.msdn.com/dancre/archive/2006/09/17/dm-v-vm-part-8-view-models.aspx" onclick="urchinTracker('/outgoing/blogs.msdn.com/dancre/archive/2006/09/17/dm-v-vm-part-8-view-models.aspx?referer=');">ViewModel</a> is the actual <a href="http://msdn.microsoft.com/en-us/library/cc707885.aspx" onclick="urchinTracker('/outgoing/msdn.microsoft.com/en-us/library/cc707885.aspx?referer=');">PresentationModel</a>. It hosts the DataModel, as well as the View&#8217;s commands. ViewModel should be the object used to bind to the View&#8217;s DataContext and should implement INotifyPropertyChanged if any of the properties could change. The <a href="http://msdn.microsoft.com/en-us/library/cc707865.aspx" onclick="urchinTracker('/outgoing/msdn.microsoft.com/en-us/library/cc707865.aspx?referer=');">QuickStarts</a> and <a href="http://msdn.microsoft.com/en-us/library/cc707869.aspx" onclick="urchinTracker('/outgoing/msdn.microsoft.com/en-us/library/cc707869.aspx?referer=');">RIStockTrader</a> examples provide excellent samples of the PresentationModel, though be warned that the examples often use different patterns, if for no other reason than to show other pattern implementations.</p>
<p>That covers the MVVM pattern. However, there&#8217;s one more aspect that needs to be covered: composite regions. Composite WPF provides regions for managing the display of different modules in the Shell, but what if you need to display multiple modules or views within a &#8220;summary&#8221; view that is already on the Shell? In this case, you can use a Controller to manage the child use cases. The <a href="http://msdn.microsoft.com/en-us/library/cc707868.aspx" onclick="urchinTracker('/outgoing/msdn.microsoft.com/en-us/library/cc707868.aspx?referer=');">UIComposition QuickStart</a> shows a terrific example of creating a controller in a ViewModel for the purposes of managing child use cases. This removes the dependencies and depth of a View that includes a tab control containing a large number of child use cases. (The UIComposition QuickStart uses a <a href="http://msdn.microsoft.com/en-us/library/cc707873.aspx" onclick="urchinTracker('/outgoing/msdn.microsoft.com/en-us/library/cc707873.aspx?referer=');">Supervising Controller</a> pattern instead of the PresentationModel/MVVM pattern, so you&#8217;ll have to adjust it accordingly, but it works beautifully.)</p>
<p>The MVVM pattern is a great solution for WPF applications by allowing WPF&#8217;s DataBinding and Commanding to remove much of the logic formerly found in the View layer out to a unit testable ViewModel. The architecture stays clean and also fairly shallow in that an additional Presenter class is unnecessary (as opposed to the Supervising Controller pattern.)</p>
<p>Additional Resources:<br />
<a href="http://blogs.msdn.com/johngossman/default.aspx" onclick="urchinTracker('/outgoing/blogs.msdn.com/johngossman/default.aspx?referer=');">John Grossman &#8211; Tales from the Smart Client</a><br />
<a href="http://www.acceptedeclectic.com/2008/01/model-view-viewmodel-pattern-for-wpf.html" onclick="urchinTracker('/outgoing/www.acceptedeclectic.com/2008/01/model-view-viewmodel-pattern-for-wpf.html?referer=');">Pete Weissbrod &#8211; Model-View-ViewModel Pattern for WPF: Yet Another Approach</a></p>
]]></content:encoded>
			<wfw:commentRss>http://wizardsofsmart.net/patterns/composite-wpf-patterns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
