<feed xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US">
    <title>Nathan Allen-Wagner</title>
    <link rel="self" type="application/atom+xml" href="http://blog.alner.net/Atom.aspx" />
    <subtitle type="html">I can has dot netz</subtitle>
    <id>http://blog.alner.net/Default.aspx</id>
    <author>
        <name>Nathan Allen-Wagner</name>
        <uri>http://blog.alner.net/Default.aspx</uri>
    </author>
    <generator uri="http://subtextproject.com" version="Subtext Version 2.0.0.43">Subtext</generator>
    <updated>2010-03-05T14:25:50Z</updated>
    <entry>
        <title>Who knew overriding GetHashCode() was so much fun?</title>
        <link rel="alternate" type="text/html" href="http://blog.alner.net/archive/2010/03/05/who-knew-overriding-gethashcode-was-so-much-fun.aspx" />
        <id>http://blog.alner.net/archive/2010/03/05/who-knew-overriding-gethashcode-was-so-much-fun.aspx</id>
        <published>2010-03-05T14:23:00Z</published>
        <updated>2010-03-05T14:25:50Z</updated>
        <content type="html">&lt;p&gt;So, I have a project with Code Analysis turned on and it treats warnings as errors. In this project, I have a class that overrides Equals() so that I can use some of the nice collection based linq expression (like Except()). Well, code analysis doesn’t like it if you override Equals, but forget to override GetHashCode(). &lt;/p&gt;  &lt;p&gt;Needless to say, I have to override GetHashCode(). But how does one do this? Some searching turned up a bunch of interesting resources showing how to do this. However, there isn’t some cut and paste code that solves the problem. You have to really understand the implications and usage of this method. For that purpose, I recommend: &lt;a href="http://msdn.microsoft.com/en-us/library/system.object.gethashcode(VS.80).aspx" target="_blank"&gt;MSDN’s Object.GetHashCode Method documentation&lt;/a&gt;. I also found some &lt;a href="http://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overriden-in-c" target="_blank"&gt;interesting discussions on StackOverflow regarding the issue&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;In my case, I have an object that I want to be “Equal” to another if three internal values are the same. This is where things get interesting. GetHashCode is just an integer. As such, how to I get it to return a value that is guaranteed not to have the same value as another object with a different set of 3 properties? &lt;/p&gt;  &lt;p&gt;For Example, if I just add the numbers, then I could have a problem like this:&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2" width="400"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="133"&gt; &lt;/td&gt;        &lt;td valign="top" width="133"&gt;Object A&lt;/td&gt;        &lt;td valign="top" width="133"&gt;Object B&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Field 1 Hash&lt;/td&gt;        &lt;td valign="top" width="133"&gt;1&lt;/td&gt;        &lt;td valign="top" width="133"&gt;1&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Field 2 Hash&lt;/td&gt;        &lt;td valign="top" width="133"&gt;5&lt;/td&gt;        &lt;td valign="top" width="133"&gt;100&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Field 3 Hash&lt;/td&gt;        &lt;td valign="top" width="133"&gt;100&lt;/td&gt;        &lt;td valign="top" width="133"&gt;5&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Sum of Hashes&lt;/td&gt;        &lt;td valign="top" width="133"&gt;&lt;strong&gt;106&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="133"&gt;&lt;strong&gt;106&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;The above sums are equal, but the objects are different!&lt;/p&gt;  &lt;p&gt;So, a number of implementations use seeds and factors to adjust the numbers and reduce the collisions. One such example (from the above stack overflow article) does so like this:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;int &lt;/span&gt;hash = 13; 
hash = (hash * 7) + field1.GetHashCode(); 
hash = (hash * 7) + field2.GethashCode(); 
hash = (hash * 7) + field3.GethashCode(); &lt;/pre&gt;

  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;return &lt;/span&gt;hash;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;I’m not sure I fully understand this yet, but I guess I can see how this would improve the chance that things are not going to collide. In the above scenario, I would end up with something like the following:&lt;/p&gt;

&lt;table border="1" cellspacing="0" cellpadding="2" width="401"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="133"&gt; &lt;/td&gt;

      &lt;td valign="top" width="133"&gt;Object A&lt;/td&gt;

      &lt;td valign="top" width="133"&gt;Object B&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="133"&gt;Field 1 Hash&lt;/td&gt;

      &lt;td valign="top" width="133"&gt;1&lt;/td&gt;

      &lt;td valign="top" width="133"&gt;1&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="133"&gt;Field 2 Hash&lt;/td&gt;

      &lt;td valign="top" width="133"&gt;5&lt;/td&gt;

      &lt;td valign="top" width="133"&gt;100&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="133"&gt;Field 3 Hash&lt;/td&gt;

      &lt;td valign="top" width="133"&gt;100&lt;/td&gt;

      &lt;td valign="top" width="133"&gt;5&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="133"&gt;Sum of Hashes wtih Seed and Factor&lt;/td&gt;

      &lt;td valign="top" width="133"&gt;&lt;strong&gt;91 + 1 + 644 + 5 + 4543 + 100
          &lt;br /&gt;

          &lt;br /&gt;= 4643&lt;/strong&gt;&lt;/td&gt;

      &lt;td valign="top" width="133"&gt;&lt;strong&gt;91 + 1 + 644 + 100 + 5208 + 5
          &lt;br /&gt;

          &lt;br /&gt;= 5213&lt;/strong&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;So this seems better, but we’re still not guaranteed that these are unique (at least as far as my feeble mind can comprehend).&lt;/p&gt;

&lt;p&gt;Microsoft’s MSDN documentation uses a bitwise XOR to combine hash codes from multiple contained fields. This too seems a bit risky, but I haven’t explored how it works in detail. &lt;/p&gt;

&lt;p&gt;So all you intelligent people out there, please tell me what I’m missing. Is there some “algorithm” or factor + seed magic that does guarantee unique hash codes?&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;&lt;img src="http://blog.alner.net/aggbug/95.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blog.alner.net/comments/95.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://blog.alner.net/comments/commentRss/95.aspx</wfw:commentRss>
        <trackback:ping>http://blog.alner.net/services/trackbacks/95.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Making WPF Remember Window Size, State, and Position&amp;hellip;</title>
        <link rel="alternate" type="text/html" href="http://blog.alner.net/archive/2010/03/04/making-wpf-remember-window-size-state-and-positionhellip.aspx" />
        <id>http://blog.alner.net/archive/2010/03/04/making-wpf-remember-window-size-state-and-positionhellip.aspx</id>
        <published>2010-03-04T11:15:00Z</published>
        <updated>2010-03-04T11:15:00Z</updated>
        <content type="html">&lt;p&gt;I had a need to make my WPF app remember it’s size, position and state for the main window. A bit of “Binging” and I eventually heard the sound of found. Turns out there are a couple ideas out there, but the &lt;a href="http://msdn.microsoft.com/en-us/library/aa972163.aspx" target="_blank"&gt;WINDOWPLACEMENT solution from MSDN&lt;/a&gt; seemed to fit the bill the best.&lt;/p&gt;  &lt;p&gt;This solution requires using some WIN32 API calls to get and set the information. The upside is that it seems to leverage the smarts built in to Windows regarding multi-monitor detection. The sample says that if the app was previously displayed on a secondary monitor that is no longer available, the app will show up on the available monitor instead. Nice.&lt;/p&gt;  &lt;p&gt;The sample also shows how to store these settings in the App’s user settings store. I took this approach for now, but I have a feeling this will be relocated to a more generic location at some point in the future.&lt;/p&gt;  &lt;p&gt;Cheers!&lt;/p&gt;&lt;img src="http://blog.alner.net/aggbug/94.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blog.alner.net/comments/94.aspx</wfw:comment>
        <slash:comments>1</slash:comments>
        <wfw:commentRss>http://blog.alner.net/comments/commentRss/94.aspx</wfw:commentRss>
        <trackback:ping>http://blog.alner.net/services/trackbacks/94.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Except() Extension method Returns the &amp;ldquo;Difference&amp;rdquo;</title>
        <link rel="alternate" type="text/html" href="http://blog.alner.net/archive/2010/03/02/except-extension-method-returns-the-ldquodifferencerdquo.aspx" />
        <id>http://blog.alner.net/archive/2010/03/02/except-extension-method-returns-the-ldquodifferencerdquo.aspx</id>
        <published>2010-03-02T17:44:00Z</published>
        <updated>2010-03-02T17:44:00Z</updated>
        <content type="html">&lt;p&gt;Maybe I’m late to the party, but I just found the &lt;font face="Courier New"&gt;&lt;strong&gt;Except()&lt;/strong&gt;&lt;/font&gt; LINQ extension method. I knew I loved .NET!&lt;/p&gt;  &lt;p&gt;For those of you familiar with set theory, this returns the “difference” between two collections. In my case, I needed to append a set of items to an existing collection, but only if they’re not already in the target collection. This thing was just what I needed. &lt;/p&gt;  &lt;p&gt;Here’s the definition:&lt;/p&gt;  &lt;blockquote style="font-size: 0.8em"&gt;   &lt;p /&gt;    &lt;pre class="code"&gt;&lt;span style="color: green"&gt;//
// Summary:
//     Produces the set difference of two sequences by using the default equality
//     comparer to compare values.
//
// Parameters:
//   first:
//     An System.Collections.Generic.IEnumerable&amp;lt;T&amp;gt; whose elements that are not
//     also in second will be returned.
//
//   second:
//     An System.Collections.Generic.IEnumerable&amp;lt;T&amp;gt; whose elements that also occur
//     in the first sequence will cause those elements to be removed from the returned
//     sequence.
//
// Type parameters:
//   TSource:
//     The type of the elements of the input sequences.
//
// Returns:
//     A sequence that contains the set difference of the elements of two sequences.
//
// Exceptions:
//   System.ArgumentNullException:
//     first or second is null.
&lt;/span&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TSource&amp;gt; Except&amp;lt;TSource&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TSource&amp;gt; first, &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TSource&amp;gt; second);&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

  &lt;p /&gt;
&lt;/blockquote&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Couple this with the ToList() and ForEach() extension method, and you can do this work in about 2 lines of code. Love it!&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;&lt;img src="http://blog.alner.net/aggbug/93.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blog.alner.net/comments/93.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://blog.alner.net/comments/commentRss/93.aspx</wfw:commentRss>
        <trackback:ping>http://blog.alner.net/services/trackbacks/93.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Should the Domain Model implement INotifyPropertyChanged?</title>
        <link rel="alternate" type="text/html" href="http://blog.alner.net/archive/2010/02/26/should-the-domain-model-implement-inotifypropertychanged.aspx" />
        <id>http://blog.alner.net/archive/2010/02/26/should-the-domain-model-implement-inotifypropertychanged.aspx</id>
        <published>2010-02-26T00:20:00Z</published>
        <updated>2010-02-26T00:20:00Z</updated>
        <content type="html">&lt;p&gt;I’ve been discussing an architectural question with a colleague this week. We’re working on a WPF application and using Domain Driven Design and MVVM. The current implementation has a relatively “pure” domain model where the classes in the model do not depend on any UI specific or other external assemblies. They are pretty much pure “POCO” objects.&lt;/p&gt;  &lt;p&gt;The question is whether it’s reasonable to have the domain model implement INotifyPropertyChanged and INotifyCollectionChanged (via ObservableCollection). &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;INotifyPropertyChanged is defined in System.dll. It’s part of System.ComponentModel namespace.&lt;/li&gt;    &lt;li&gt;INotifyCollectionChanged and ObservableCollection are both in WindowsBase.dll. These are in System.Collections.Specialized and System.Collections.ObjectModel respectively.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;My take… go for it! Implement INotifyPropertyChanged on the domain model.&lt;/p&gt;  &lt;p&gt;We’ve discussed a number of considerations, options and alternatives. A couple of things to consider:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;WPF has very strong binding support. It works well with a number of different patterns. Each of these provide a way for the UI to keep in sync with the data.&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;INotifyPropertyChanged&lt;/li&gt;      &lt;li&gt;Raise “___Changed” events; one for each property&lt;/li&gt;      &lt;li&gt;Other legacy binding interfaces&lt;/li&gt;   &lt;/ul&gt;    &lt;li&gt;With MVVM, do we &lt;a href="http://blog.alner.net/archive/2010/02/09/mvvm-to-wrap-or-not-to-wrap-blinq-and-clinq.aspx"&gt;fully wrap the model behind view models&lt;/a&gt;, or do we allow the view to bind to model directly. If we fully wrap the model, then the ViewModel can intercept all of the changes and raise PropertyChanged accordingly. If the View does bind directly to portions of the model, when we may need to force the UI to rebind manually.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The team came up with a cool option that may help… Wrap the domain model with a “decorator” that adds INotifyPropertyChanged support. The implementation from the team uses ICustomTypeDescriptor to expose the properties of the model, but intercept the “setter” so that the decorator can raise PropertyChanged. WPF honors ICustomTypeDescriptor, which makes this a pretty slick idea. I definitely think this is cool.&lt;/p&gt;  &lt;p&gt;Another approach might involve using a “dynamic proxy” to add a decorator automatically. While I haven’t looked into it yet, I’m wondering if this could be done with an AOP framework like Spring.NET. This approach would hide more of the complexity under the surface. Code would interact with something that looks and feels like the original object instance.&lt;/p&gt;  &lt;p&gt;There are some challenges with the decorator approach that I’m still wresting through.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;em&gt;Complexity&lt;/em&gt; – Adding this decorator adds complexity. It must implement the ICustomTypeDescriptor interface, which has quite a few methods. The logic inside uses reflection to return PropertyDescriptors that represent each of the properties on the model. To be complete, the decorator needs to traverse any of it’s child objects and collections so that they are also wrapped with this decorator. This is especially important if the Views bind directly to any portion of the domain model.&lt;/li&gt;    &lt;li&gt;&lt;em&gt;Decentralized&lt;/em&gt; – If two different views create separate instances of the decorator over the same instance a domain model, then one of these views will not get notified when the other view (or ViewModel) changes values on the model. This means that the ViewModels need to implement an &lt;em&gt;additional&lt;/em&gt; mechanism for change notification.&lt;/li&gt;    &lt;li&gt;&lt;em&gt;Loss of Fidelity&lt;/em&gt; – One of the use cases that can happen with an object is a relationship between properties. A change in one property may result in changes to other properties. In these cases, the typical INotifyPropertyChanged logic will raise PropertyChanged multiple times; once for each property that was changed. In a decorated scenario, the decorator may not be aware that the model has this dependency between properties and will not raise PropertyChanged for the dependent properties. The work-around is to raise PropertyChanged for all properties, but this means that we loose a little bit of fidelity. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;In my mind, taking a dependency on System.dll and WindowsBase.dll is acceptable for the simplicity and functionality that you get in return. Additionally, I think that the disruption caused by these interfaces is minimal. They don’t change the core behavior of the model or add any sort of constraints on implementation (as a base class might do). Instead, they "augment” the functionality.&lt;/p&gt;  &lt;p&gt;The model is the single source of truth. Adding change notification on the model means that I have a single source of truth for change notification too. This is huge because it makes things &lt;em&gt;simpler&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;The jury is still out on this one. I think that either way is viable. Drop a comment if you have an opinion on the matter.&lt;/p&gt;  &lt;p&gt;Cheers!&lt;/p&gt;&lt;img src="http://blog.alner.net/aggbug/90.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blog.alner.net/comments/90.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://blog.alner.net/comments/commentRss/90.aspx</wfw:commentRss>
        <trackback:ping>http://blog.alner.net/services/trackbacks/90.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Skinning WPF &amp;ndash; Call InitializeComponent in App.Xaml</title>
        <link rel="alternate" type="text/html" href="http://blog.alner.net/archive/2010/02/25/skinning-wpf-ndash-call-initializecomponent-in-app.xaml.aspx" />
        <id>http://blog.alner.net/archive/2010/02/25/skinning-wpf-ndash-call-initializecomponent-in-app.xaml.aspx</id>
        <published>2010-02-25T22:48:17Z</published>
        <updated>2010-02-26T00:24:16Z</updated>
        <content type="html">&lt;p&gt;For the past two days, I’ve been trying to get the infrastructure in place for skinning a WPF application. My goal is to support multiple skins and swap them out dynamically while the app is open.&lt;/p&gt;  &lt;p&gt;I could get the skin to affect the UI if I set it in the main window’s resource dictionary. However there was a quirk… The application uses WPF frames and pages. The skin was not showing up on frame’s pages. It would only show up on the main window.&lt;/p&gt;  &lt;p&gt;I searched Bing, Google and StackOverflow for answers. No luck. &lt;/p&gt;  &lt;p&gt;I setup VS to pull down the MS debug symbols so that I could step through the source code for the .NET framework. This was very cool, but not an immediate help.&lt;/p&gt;  &lt;p&gt;I went back to StackOverflow and searched for stuff about pages, frames and resource dictionaries. I stumbled on &lt;a href="http://stackoverflow.com/questions/1229395/trouble-referencing-a-resource-dictionary-that-contains-a-merged-dictionary" target="_blank"&gt;this article&lt;/a&gt; and found a comment that provided the solution. It wasn’t the accepted answer, nor was it detailed, but it worked for me! The comment by “Chris” indicating that I should check that the App.Xaml’s code-behind is calling InitializeComponent. I added this call and all of a sudden my pages were skinned too!&lt;/p&gt;  &lt;p&gt;Chris indicated that this call to InitializeComponent is what “merges the resource dictionaries.” I looked at the code that is in this generated method and it appears that it is responsible for calling LoadComponent on the App.Xaml file. Without this call, there is no “Application Level” resource dictionary defined in the application. As such, it seems that there is no application container (resource dictionary) available to host the resources for the app.&lt;/p&gt;  &lt;p&gt;There is an interesting quirk. At one point, I removed the explicitly included skin such that the App.Xaml file had no resources in its resource dictionary. Then, my code wouldn’t compile. It said that there was no “InitializeComponent” method defined on the App code-behind. What the heck! It was just there!&lt;/p&gt;  &lt;p&gt;I added an item back into the App.Xaml’s resource dictionary and then the method was back, plus my app was responding to the skin properly.&lt;/p&gt;  &lt;p&gt;So, I’m thinking that I need to follow these rules to get reliable styling / skinning in a WPF app.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Make sure there is at least one resource in the App.Xaml’s resource dictionary. &lt;/li&gt;    &lt;li&gt;Make sure the App.Xaml’s code-behind calls InitializeComponent. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Cheers!&lt;/p&gt;&lt;img src="http://blog.alner.net/aggbug/89.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blog.alner.net/comments/89.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://blog.alner.net/comments/commentRss/89.aspx</wfw:commentRss>
        <trackback:ping>http://blog.alner.net/services/trackbacks/89.aspx</trackback:ping>
    </entry>
    <entry>
        <title>MVVM: To Wrap or Not to Wrap? BLINQ and CLINQ to the Rescue! (Part 3)</title>
        <link rel="alternate" type="text/html" href="http://blog.alner.net/archive/2010/02/09/mvvm-to-wrap-or-not-to-wrap-blinq-and-clinq.aspx" />
        <id>http://blog.alner.net/archive/2010/02/09/mvvm-to-wrap-or-not-to-wrap-blinq-and-clinq.aspx</id>
        <published>2010-02-09T22:45:58Z</published>
        <updated>2010-02-09T23:25:32Z</updated>
        <content type="html">&lt;p&gt;In &lt;a href="http://blog.alner.net/archive/2010/02/09/mvvm-to-wrap-or-not-to-wrap.aspx"&gt;part 1&lt;/a&gt;, I asked the question “&lt;em&gt;Do I expose the model directly, or do I wrap each item in its own view model?&lt;/em&gt;” We looked at two plausible options for handling this and saw some code examples of each.&lt;/p&gt;  &lt;p&gt;In &lt;a href="http://blog.alner.net/archive/2010/02/09/mvvm-to-wrap-or-not-to-wrap-should-viewmodels-wrap.aspx"&gt;part 2&lt;/a&gt;, I asked the question “&lt;em&gt;Do I expose the model’s collections directly, or do I wrap each collection?&lt;/em&gt;” We saw the progression of code to implement this "brute-force". Then we quickly ran away screaming.&lt;/p&gt;  &lt;p&gt;This installment (part 3) asks "&lt;em&gt;How do I &lt;strong&gt;cleanly and easily&lt;/strong&gt; wrap the model’s collections with collections of ViewModels?&lt;/em&gt;" We'll take a look at a couple LINQ extensions that should make this pretty simple and straight forward. We may even find that these frameworks solve other problems too!&lt;/p&gt;  &lt;p&gt;Source code for this article, including the code from part 2, part 3, and unit tests is available for download:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:8eb9d37f-1541-4f29-b6f4-1eea890d4876:90db9cb7-1420-4a0f-b15a-7111324a0827" class="wlWriterEditableSmartContent"&gt;&lt;p /&gt;&lt;div&gt;&lt;a href="http://blog.alner.net/images/blog_alner_net/WindowsLiveWriter/MVVMToWraporNottoWrapBLINQandCLINQtotheR_129C1/MvvmWrappers.zip" target="_self"&gt;MvvmWrappers.zip&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;  &lt;h3&gt;Background:&lt;/h3&gt;  &lt;p&gt;MVVM says that a ViewModel sits between the View (XAML) and the Model. The ViewModel (VM) exposes data from the model plus additional view-specific details that the view (V) can easily bind to. Ideally, the view can be constructed with zero code-behind. Pure XAML with bindings to manage data and UI state. This is all well and good.&lt;/p&gt;  &lt;p&gt;Now, lets say that our model is reasonably real-world and has a entities that contain collections of other entities, which in turn contain collections of other entities. Maybe some of these even have references back to other trees of data. The classic example is customers and orders. &lt;/p&gt;  &lt;h3&gt;The Model:&lt;/h3&gt;  &lt;p&gt;Customer    &lt;br /&gt;    |_ Shipping Addresses (Collection)     &lt;br /&gt;    |_ Billing Address     &lt;br /&gt;    |_ Orders (Collection)     &lt;br /&gt;    |_ First Name     &lt;br /&gt;    |_ Last Name     &lt;br /&gt;    |_ Etc…&lt;/p&gt;  &lt;h3&gt;Step 2 - Revisited: Shipping Addresses (Collection)&lt;/h3&gt;  &lt;p&gt;We want to bind the shipping addresses into the view. This is a collection of shipping address objects, exposed by the model’s customer object. Previously, we created the ViewModel to expose the customer to the view.&lt;/p&gt;  &lt;p&gt;In &lt;a href="http://blog.alner.net/archive/2010/02/09/mvvm-to-wrap-or-not-to-wrap.aspx"&gt;part 1&lt;/a&gt;, we left off with the following customer ViewModel implementation:&lt;/p&gt;  &lt;blockquote style="height: 400px; font-size: 0.8em; overflow: auto"&gt;   &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System.ComponentModel;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.Collections.ObjectModel;
 
&lt;span style="color: blue"&gt;namespace &lt;/span&gt;MvvmWrappers.ViewModels
{
    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CustomerVM &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;INotifyPropertyChanged
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;private &lt;/span&gt;Models.&lt;span style="color: #2b91af"&gt;Customer &lt;/span&gt;_Customer = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
 
        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Constructor - Add an event handler for PropertyChanged.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;CustomerVM(Models.&lt;span style="color: #2b91af"&gt;Customer &lt;/span&gt;customer)
        {
            _Customer = customer;
            _Customer.PropertyChanged += &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PropertyChangedEventHandler&lt;/span&gt;(_Customer_PropertyChanged);
        }
 
        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Watch for changes in the underlying model and propagate those that
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;are exposed by this ViewModel.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;void &lt;/span&gt;_Customer_PropertyChanged(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;PropertyChangedEventArgs &lt;/span&gt;e)
        {
            &lt;span style="color: blue"&gt;switch &lt;/span&gt;(e.PropertyName)
            {
                &lt;span style="color: blue"&gt;case &lt;/span&gt;&lt;span style="color: #a31515"&gt;"FirstName"&lt;/span&gt;:
                &lt;span style="color: blue"&gt;case &lt;/span&gt;&lt;span style="color: #a31515"&gt;"LastName"&lt;/span&gt;:
                    &lt;span style="color: blue"&gt;this&lt;/span&gt;.OnPropertyChanged(e.PropertyName);
                    &lt;span style="color: blue"&gt;break&lt;/span&gt;;
            }
        }
 
        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Delegate the storage to the model.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;Also delegate the INotifyPropertyChanged handling to the model.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public string &lt;/span&gt;FirstName
        {
            &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_Customer.FirstName; }
            &lt;span style="color: blue"&gt;set &lt;/span&gt;{ _Customer.FirstName = &lt;span style="color: blue"&gt;value&lt;/span&gt;; }
        }
 
        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Delegate the storage to the model.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;Also delegate the INotifyPropertyChanged handling to the model.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public string &lt;/span&gt;LastName
        {
            &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_Customer.LastName; }
            &lt;span style="color: blue"&gt;set &lt;/span&gt;{ _Customer.LastName = &lt;span style="color: blue"&gt;value&lt;/span&gt;; }
        }
        
        &lt;span style="color: blue"&gt;#region &lt;/span&gt;INotifyPropertyChanged
        ...
        &lt;span style="color: blue"&gt;#endregion
 
    &lt;/span&gt;}
 
}&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

  &lt;pre class="code"&gt; &lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;The code in part 2 worked, but overwhelmed the implementation with complex “plumbing” code. So, we seek a better solution. &lt;/p&gt;

&lt;p&gt;Enter Bindable LINQ (BLINQ) and Continuous LINQ (CLINQ). These are two frameworks on &lt;a href="http://www.codeplex.com/" target="_blank"&gt;Codeplex&lt;/a&gt; that provide potential solutions to the problem. Both of them implement enhanced LINQ implementations that return a collection that implements INotifyCollectionChanged (like ObservableCollection). Furthermore, they keep that collection in sync with a separate “source” collection. &lt;/p&gt;

&lt;h4&gt;Bindable LINQ (BLINQ)&lt;/h4&gt;

&lt;p&gt;For example, if I have an ObservableCollection&amp;lt;Customer&amp;gt;, I could write a LINQ query that looked something like:&lt;/p&gt;

&lt;blockquote style="font-size: 0.8em"&gt;
  &lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;Models.&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&amp;gt; customers = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;MvvmBlinq.Models.&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&amp;gt;();

&lt;span style="color: #2b91af"&gt;IBindableCollection&lt;/span&gt;&amp;lt;Models.&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;&amp;gt; filteredCustomers = (
    &lt;span style="color: blue"&gt;from &lt;/span&gt;c &lt;span style="color: blue"&gt;in &lt;/span&gt;customers.AsBindable()
    &lt;span style="color: blue"&gt;where &lt;/span&gt;c.LastName.StartsWith(&lt;span style="color: #a31515"&gt;"A"&lt;/span&gt;)
    &lt;span style="color: blue"&gt;select &lt;/span&gt;c);

customers.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;MvvmBlinq.Models.&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;() { LastName = &lt;span style="color: #a31515"&gt;"Adams" &lt;/span&gt;});
customers.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;MvvmBlinq.Models.&lt;span style="color: #2b91af"&gt;Customer&lt;/span&gt;() { LastName = &lt;span style="color: #a31515"&gt;"Benedict" &lt;/span&gt;});

&lt;span style="color: #2b91af"&gt;Debug&lt;/span&gt;.Assert(filteredCustomers.Count == 1, &lt;span style="color: #a31515"&gt;"Filtered customers should have 1 and only 1 record."&lt;/span&gt;);&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the above code, you can see that we create an ObservableCollection to hold Customer objects. Initially it is empty. Next, we define a Linq query over that collection that selects customers with a last name that starts with the letter “A”. This query holds records in an IBindableCollection, which also implements INotifyCollectionChanged among other standard collection interfaces. Next, we add 2 customers to the underlying (source) &lt;font face="Courier New"&gt;customers&lt;/font&gt; ObservableCollection. Lastly, we check the &lt;font face="Courier New"&gt;filteredCustomers&lt;/font&gt;  linked collection.&lt;/p&gt;

&lt;p&gt;Astute readers will notice that without the BLINQ provider, we would not expect to find anything in  &lt;font face="Courier New"&gt;filteredCustomers&lt;/font&gt; because it was created before we even added items to the &lt;font face="Courier New"&gt;customers&lt;/font&gt; collection. &lt;em&gt;This is the gift of BLINQ and CLINQ. They will keep the second collection in sync with the underlying source collection&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You say, “So what…. how does this help me?” &lt;/p&gt;

&lt;p&gt;Glad you asked! In our case of ViewModels, we want to expose the ShippingAddresses collection by creating a new collection of ShippingAddressVM ViewModels. Each one of these ShippingAddressVMs will wrap the underlying ShippingAddress model.&lt;/p&gt;

&lt;p&gt;You say, “Code please…”&lt;/p&gt;

&lt;blockquote style="height: 400px; font-size: 0.8em; overflow: auto"&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.ComponentModel;
&lt;span style="color: blue"&gt;using &lt;/span&gt;Bindable.Linq;

&lt;span style="color: blue"&gt;namespace &lt;/span&gt;MvvmBlinq.ViewModels
{
    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CustomerVM &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;INotifyPropertyChanged
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;private &lt;/span&gt;Models.&lt;span style="color: #2b91af"&gt;Customer &lt;/span&gt;_Customer = &lt;span style="color: blue"&gt;null&lt;/span&gt;;

        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Constructor - Add an event handler for PropertyChanged.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;CustomerVM(Models.&lt;span style="color: #2b91af"&gt;Customer &lt;/span&gt;customer)
        {
            _Customer = customer;
            _Customer.PropertyChanged += &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PropertyChangedEventHandler&lt;/span&gt;(_Customer_PropertyChanged);

            ShippingAddresses = (&lt;span style="color: blue"&gt;from &lt;/span&gt;sa &lt;span style="color: blue"&gt;in &lt;/span&gt;_Customer.ShippingAddresses.AsBindable()
                                 &lt;span style="color: blue"&gt;select new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;(sa));
        }

        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Watch for changes in the underlying model and propagate those that
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;are exposed by this ViewModel.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;void &lt;/span&gt;_Customer_PropertyChanged(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;PropertyChangedEventArgs &lt;/span&gt;e)
        {
            &lt;span style="color: blue"&gt;switch &lt;/span&gt;(e.PropertyName)
            {
                &lt;span style="color: blue"&gt;case &lt;/span&gt;&lt;span style="color: #a31515"&gt;"ShippingAddresses"&lt;/span&gt;:
                    &lt;span style="color: green"&gt;// If this is set to a new collection, then we need to reset the binding here.
                    &lt;/span&gt;ShippingAddresses = (&lt;span style="color: blue"&gt;from &lt;/span&gt;sa &lt;span style="color: blue"&gt;in &lt;/span&gt;_Customer.ShippingAddresses.AsBindable()
                                         &lt;span style="color: blue"&gt;select new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;(sa));
                    &lt;span style="color: blue"&gt;this&lt;/span&gt;.OnPropertyChanged(e.PropertyName);
                    &lt;span style="color: blue"&gt;break&lt;/span&gt;; 

                &lt;span style="color: blue"&gt;case &lt;/span&gt;&lt;span style="color: #a31515"&gt;"FirstName"&lt;/span&gt;:
                &lt;span style="color: blue"&gt;case &lt;/span&gt;&lt;span style="color: #a31515"&gt;"LastName"&lt;/span&gt;:
                    &lt;span style="color: blue"&gt;this&lt;/span&gt;.OnPropertyChanged(e.PropertyName);
                    &lt;span style="color: blue"&gt;break&lt;/span&gt;;
            }
        }

        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;A collection of shipping addresses, wrapped in a ShippingAddressVM ViewModel
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IBindableCollection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;&amp;gt; _ShippingAddresses;
        &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IBindableCollection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;&amp;gt; ShippingAddresses
        {
            &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_ShippingAddresses; }
            &lt;span style="color: blue"&gt;set
            &lt;/span&gt;{
                &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: blue"&gt;value &lt;/span&gt;== _ShippingAddresses)
                    &lt;span style="color: blue"&gt;return&lt;/span&gt;;

                _ShippingAddresses = &lt;span style="color: blue"&gt;value&lt;/span&gt;;

                OnPropertyChanged(&lt;span style="color: #a31515"&gt;"ShippingAddresses"&lt;/span&gt;);
            }
        }

        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Delegate the storage to the model.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;Also delegate the INotifyPropertyChanged handling to the model.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public string &lt;/span&gt;FirstName
        {
            &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_Customer.FirstName; }
            &lt;span style="color: blue"&gt;set &lt;/span&gt;{ _Customer.FirstName = &lt;span style="color: blue"&gt;value&lt;/span&gt;; }
        }

        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Delegate the storage to the model.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;Also delegate the INotifyPropertyChanged handling to the model.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public string &lt;/span&gt;LastName
        {
            &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_Customer.LastName; }
            &lt;span style="color: blue"&gt;set &lt;/span&gt;{ _Customer.LastName = &lt;span style="color: blue"&gt;value&lt;/span&gt;; }
        }

        &lt;span style="color: blue"&gt;#region &lt;/span&gt;INotifyPropertyChanged
        ...
        &lt;span style="color: blue"&gt;#endregion

    &lt;/span&gt;}
}&lt;/pre&gt;

  &lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Compared to our last attempt, this version is much simpler! We hook the ShippingAddresses ViewModel collection up to the Model in the constructor via the Bindable LINQ query. This query is a standard LINQ query, except for the additional “AsBindable()” extension method in the query. The query doesn’t filter out any records. Rather, it creates a new ShippingAddressVM instance to wrap each of the underlying ShippingAddress model instances.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Net Result: A synchronized, bindable, ViewModel-wrapped collection.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You may also note that we re-bind the BLINQ collection if the model’s ShippingAddress PropertyChanged notification fires. This is to support the case where the model’s collection of shipping addresses is completely replaced with a new collection.&lt;/p&gt;

&lt;h4&gt;Continuous LINQ (CLINQ)&lt;/h4&gt;

&lt;p&gt;The pattern for using CLINQ is very similar to BLINQ. The two frameworks are remarkably similar, although each has some unique features.&lt;/p&gt;

&lt;blockquote style="height: 400px; font-size: 0.8em; overflow: auto"&gt;
  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;System;
&lt;span style="color: blue"&gt;using &lt;/span&gt;System.ComponentModel;
&lt;span style="color: blue"&gt;using &lt;/span&gt;ContinuousLinq;

&lt;span style="color: blue"&gt;namespace &lt;/span&gt;MvvmClinq.ViewModels
{
    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CustomerVM &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;INotifyPropertyChanged
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;private &lt;/span&gt;Models.&lt;span style="color: #2b91af"&gt;Customer &lt;/span&gt;_Customer = &lt;span style="color: blue"&gt;null&lt;/span&gt;;

        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Constructor - Add an event handler for PropertyChanged.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;CustomerVM(Models.&lt;span style="color: #2b91af"&gt;Customer &lt;/span&gt;customer)
        {
            _Customer = customer;
            _Customer.PropertyChanged += &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PropertyChangedEventHandler&lt;/span&gt;(_Customer_PropertyChanged);

            ShippingAddresses = (&lt;span style="color: blue"&gt;from &lt;/span&gt;sa &lt;span style="color: blue"&gt;in &lt;/span&gt;_Customer.ShippingAddresses
                                 &lt;span style="color: blue"&gt;select new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;(sa));
        }

        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Watch for changes in the underlying model and propagate those that
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;are exposed by this ViewModel.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;void &lt;/span&gt;_Customer_PropertyChanged(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;PropertyChangedEventArgs &lt;/span&gt;e)
        {
            &lt;span style="color: blue"&gt;switch &lt;/span&gt;(e.PropertyName)
            {
                &lt;span style="color: blue"&gt;case &lt;/span&gt;&lt;span style="color: #a31515"&gt;"ShippingAddresses"&lt;/span&gt;:
                    &lt;span style="color: green"&gt;// If this is set to a new collection, then we need to reset the binding here.
                    &lt;/span&gt;ShippingAddresses = (&lt;span style="color: blue"&gt;from &lt;/span&gt;sa &lt;span style="color: blue"&gt;in &lt;/span&gt;_Customer.ShippingAddresses
                                         &lt;span style="color: blue"&gt;select new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;(sa));
                    &lt;span style="color: blue"&gt;this&lt;/span&gt;.OnPropertyChanged(e.PropertyName);
                    &lt;span style="color: blue"&gt;break&lt;/span&gt;; 

                &lt;span style="color: blue"&gt;case &lt;/span&gt;&lt;span style="color: #a31515"&gt;"FirstName"&lt;/span&gt;:
                &lt;span style="color: blue"&gt;case &lt;/span&gt;&lt;span style="color: #a31515"&gt;"LastName"&lt;/span&gt;:
                    &lt;span style="color: blue"&gt;this&lt;/span&gt;.OnPropertyChanged(e.PropertyName);
                    &lt;span style="color: blue"&gt;break&lt;/span&gt;;
            }
        }

        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;A collection of shipping addresses, wrapped in a ShippingAddressVM ViewModel
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ReadOnlyContinuousCollection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;&amp;gt; _ShippingAddresses;
        &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ReadOnlyContinuousCollection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;&amp;gt; ShippingAddresses
        {
            &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_ShippingAddresses; }
            &lt;span style="color: blue"&gt;set
            &lt;/span&gt;{
                &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: blue"&gt;value &lt;/span&gt;== _ShippingAddresses)
                    &lt;span style="color: blue"&gt;return&lt;/span&gt;;

                _ShippingAddresses = &lt;span style="color: blue"&gt;value&lt;/span&gt;;

                OnPropertyChanged(&lt;span style="color: #a31515"&gt;"ShippingAddresses"&lt;/span&gt;);
            }
        }

        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Delegate the storage to the model.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;Also delegate the INotifyPropertyChanged handling to the model.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public string &lt;/span&gt;FirstName
        {
            &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_Customer.FirstName; }
            &lt;span style="color: blue"&gt;set &lt;/span&gt;{ _Customer.FirstName = &lt;span style="color: blue"&gt;value&lt;/span&gt;; }
        }

        &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="color: green"&gt;Delegate the storage to the model.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &lt;/span&gt;&lt;span style="color: green"&gt;Also delegate the INotifyPropertyChanged handling to the model.
        &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="color: blue"&gt;public string &lt;/span&gt;LastName
        {
            &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_Customer.LastName; }
            &lt;span style="color: blue"&gt;set &lt;/span&gt;{ _Customer.LastName = &lt;span style="color: blue"&gt;value&lt;/span&gt;; }
        }

        &lt;span style="color: blue"&gt;#region &lt;/span&gt;INotifyPropertyChanged
        ...
        &lt;span style="color: blue"&gt;#endregion

    &lt;/span&gt;}
}&lt;/pre&gt;
  &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;You’ll notice that the above code is almost identical to the BLINQ example. In fact, it is slightly simpler since CLINQ doesn’t require the “AsBindable()” call. CLINQ returns a “ContinuousCollection” or a “ReadOnlyContinuousCollection”. This output also supports the standard collection interfaces, including INotifyCollectionChanged. In this case, CLINQ sees that the query reshapes the data and returns a Read Only collection. &lt;/p&gt;

&lt;h4&gt;CLINQ vs. BLINQ vs. Others&lt;/h4&gt;

&lt;p&gt;So what is the difference between these frameworks? Kyle Lanser posted &lt;a href="http://stackoverflow.com/questions/167622/bindable-linq-vs-continuous-linq" target="_blank"&gt;a nice answer to this question on StackOverflow&lt;/a&gt;. He indicates that they are very similar in some respects. Both projects come with source code and both have some pretty nice sample applications.&lt;/p&gt;

&lt;p&gt;BLINQ includes the ability to walk the query tree and detect other objects &amp;amp; collections that support change notification. It will then monitor these to keep the bound collection fully in sync with the underlying collection and other objects in the LINQ query. (See the &lt;a href="http://bindablelinq.codeplex.com/" target="_blank"&gt;BLINQ site on CodePlex&lt;/a&gt; for further details.)&lt;/p&gt;

&lt;p&gt;CLINQ appears to support the possibility for Bi-Directional synchronization in cases where the CLINQ query does not reshape the data, but simply filters it. In this case, it may be possible to make changes against the bound query and see those changes in the source query. I have not tested this yet since it doesn’t really help with MVVM model wrapping scenario. You can &lt;a href="http://clinq.codeplex.com/" target="_blank"&gt;find Continuous LINQ on CodePlex&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There are other frameworks out there. The one that I’ve seen is called &lt;a href="http://obtics.codeplex.com/" target="_blank"&gt;Obtics&lt;/a&gt;. This is another CodePlex project. It appears to be quite feature rich, but time did not permit me to investigate it all that much. My initial skimming of the project made me feel a bit overwhelmed. There is a lot of “stuff” in there, which prevented me from grasping it quickly.&lt;/p&gt;

&lt;h4&gt;Bi-Directional Sync?&lt;/h4&gt;

&lt;p&gt;The implementations above do not support bi-directionally synchronization of the source and the bound collection. Changes to the model will show up in the BLINQ collection, but not vice-versa. &lt;/p&gt;

&lt;p&gt;In our case, this makes sense. We are projecting a new object from the LINQ query, which reshapes the data. A given projection may not have all the data needed to reverse the flow of data. This is the same behavior as a SQL Server view. SQL won’t let you insert into a view that pulls from multiple sources or restructures the data. (Yes, there are InsteadOf triggers, but that’s beside the point ;-)&lt;/p&gt;

&lt;p&gt;So what do we do to get data back into the model?&lt;/p&gt;

&lt;p&gt;I’m going to have to get back to you on that. The short answer is that you route that “command” or logic through your ViewModel, which in turn modifies the underlying model on your behalf. In many cases, this should be relatively straight forward. In cases where you need the View to be able to push data into the ViewModel’s bound collection, you may want to take a look at the code in part 2 of this series and see if you can morph that toward a workable solution.&lt;/p&gt;

&lt;h4&gt;Other Uses of BLINQ / CLINQ&lt;/h4&gt;

&lt;p&gt;These two frameworks offer some pretty compelling features in a couple other scenarios.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Filtering&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;WPF offers the CollectionView as an intermediary between your collection and the bound control. This supports some nice features for data navigation, filtering and grouping. This is a good option, but BLINQ and CLINQ offer an alternative that may be more or less appropriate depending on your scenario. &lt;/p&gt;

&lt;p&gt;WPF’s CollectionViewSource implements filters by raising the Filter event. In that event, you indicate whether or not to include the current item in the view. &lt;/p&gt;

&lt;p&gt;Using BLINQ / CLINQ, the filtering could be done in the ViewModel by exposing a bound collection that is backed by a single LINQ query which defines the filter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scalar Values&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;BLINQ has a nice feature that lets you create a scalar value output of a LINQ query that is bound to an underlying collection. This scalar value is kept in sync with the underlying collection automatically. The View could then bind to this value and it would update automatically whenever any records in the underlying collections changed the resulting aggregate. &lt;/p&gt;

&lt;h4&gt;Conclusion&lt;/h4&gt;

&lt;p&gt;We started with the question of whether to try and fully wrap the Model behind ViewModels. Part 1 showed two options. If you take the approach of exposing the Model to the View directly, then you may not need the structure outlined above.&lt;/p&gt;

&lt;p&gt;However, if you decide that you do want to fully (or mostly) wrap the Model, then the above seems like a feasible approach. It is relatively straight forward to construct and maintain. Plus, the usage of LINQ provides a really powerful way to filter, aggregate, and reshape the data from the Model. Isn’t that the whole point of ViewModels anyway?&lt;/p&gt;

&lt;p&gt;Drop a comment if you have an opinion or experience on the matter.&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;&lt;img src="http://blog.alner.net/aggbug/88.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blog.alner.net/comments/88.aspx</wfw:comment>
        <slash:comments>2</slash:comments>
        <wfw:commentRss>http://blog.alner.net/comments/commentRss/88.aspx</wfw:commentRss>
        <trackback:ping>http://blog.alner.net/services/trackbacks/88.aspx</trackback:ping>
    </entry>
    <entry>
        <title>MVVM: To Wrap or Not to Wrap? Should ViewModels wrap collections too? (Part 2)</title>
        <link rel="alternate" type="text/html" href="http://blog.alner.net/archive/2010/02/09/mvvm-to-wrap-or-not-to-wrap-should-viewmodels-wrap.aspx" />
        <id>http://blog.alner.net/archive/2010/02/09/mvvm-to-wrap-or-not-to-wrap-should-viewmodels-wrap.aspx</id>
        <published>2010-02-09T11:31:28Z</published>
        <updated>2010-02-09T23:31:28Z</updated>
        <content type="html">&lt;p&gt;In my &lt;a href="http://blog.alner.net/archive/2010/02/09/mvvm-to-wrap-or-not-to-wrap.aspx"&gt;previous post&lt;/a&gt;, I asked the question “&lt;em&gt;Do I expose the model directly, or do I wrap each item in its own view model?&lt;/em&gt;” We looked at two plausible options for handling this and saw some code examples of each.&lt;/p&gt;
&lt;p&gt;Now it’s time for a more interesting and challenging scenario: &lt;em&gt;Do I expose the model’s collections directly, or do I wrap each collection? &lt;/em&gt;Let me paint this picture a bit so that we can see the scenario and the challenges.&lt;/p&gt;
&lt;p&gt;MVVM says that a ViewModel sits between the View (XAML) and the Model. The ViewModel (VM) exposes data from the model plus additional view-specific details that the view (V) can easily bind to. Ideally, the view can be constructed with zero code-behind. Pure XAML with bindings to manage data and UI state. This is all well and good.&lt;/p&gt;
&lt;p&gt;Now, lets say that our model is reasonably real-world and has a entities that contain collections of other entities, which in turn contain collections of other entities. Maybe some of these even have references back to other trees of data. The classic example is customers and orders. &lt;/p&gt;
&lt;h3&gt;The Model:&lt;/h3&gt;
&lt;p&gt;Customer &lt;br /&gt;
    |_ Shipping Addresses (Collection) &lt;br /&gt;
    |_ Billing Address &lt;br /&gt;
    |_ Orders (Collection) &lt;br /&gt;
    |_ First Name &lt;br /&gt;
    |_ Last Name &lt;br /&gt;
    |_ Etc…&lt;/p&gt;
&lt;h3&gt;Step 2: Shipping Addresses (Collection)&lt;/h3&gt;
&lt;p&gt;We want to bind the shipping addresses into the view. This is a collection of shipping address objects, exposed by the model’s customer object. Previously, we created the ViewModel to expose the customer to the view. One option fully wrapped the customer object and the other exposed portions of the model directly to the view.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Disclaimer: &lt;br /&gt;
&lt;/strong&gt;The code below shows an intermediate solution on the road to something reasonable. Please see &lt;a href="http://blog.alner.net/archive/2010/02/09/mvvm-to-wrap-or-not-to-wrap-blinq-and-clinq.aspx"&gt;future posts in this series&lt;/a&gt; for improved options.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In the previous post, we left off with the following class:&lt;/p&gt;
&lt;blockquote style="HEIGHT: 400px; FONT-SIZE: 0.8em; OVERFLOW: auto"&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;using &lt;/span&gt;System.ComponentModel;
&lt;span style="COLOR: blue"&gt;using &lt;/span&gt;System.Collections.ObjectModel;
 
&lt;span style="COLOR: blue"&gt;namespace &lt;/span&gt;MvvmWrappers.ViewModels
{
    &lt;span style="COLOR: blue"&gt;public class &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;CustomerVM &lt;/span&gt;: &lt;span style="COLOR: #2b91af"&gt;INotifyPropertyChanged
    &lt;/span&gt;{
        &lt;span style="COLOR: blue"&gt;private &lt;/span&gt;Models.&lt;span style="COLOR: #2b91af"&gt;Customer &lt;/span&gt;_Customer = &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;;
 
        &lt;span style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Constructor - Add an event handler for PropertyChanged.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR: blue"&gt;public &lt;/span&gt;CustomerVM(Models.&lt;span style="COLOR: #2b91af"&gt;Customer &lt;/span&gt;customer)
        {
            _Customer = customer;
            _Customer.PropertyChanged += &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;PropertyChangedEventHandler&lt;/span&gt;(_Customer_PropertyChanged);
        }
 
        &lt;span style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Watch for changes in the underlying model and propagate those that
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &lt;/span&gt;&lt;span style="COLOR: green"&gt;are exposed by this ViewModel.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR: blue"&gt;void &lt;/span&gt;_Customer_PropertyChanged(&lt;span style="COLOR: blue"&gt;object &lt;/span&gt;sender, &lt;span style="COLOR: #2b91af"&gt;PropertyChangedEventArgs &lt;/span&gt;e)
        {
            &lt;span style="COLOR: blue"&gt;switch &lt;/span&gt;(e.PropertyName)
            {
                &lt;span style="COLOR: blue"&gt;case &lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;"FirstName"&lt;/span&gt;:
                &lt;span style="COLOR: blue"&gt;case &lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;"LastName"&lt;/span&gt;:
                    &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;.OnPropertyChanged(e.PropertyName);
                    &lt;span style="COLOR: blue"&gt;break&lt;/span&gt;;
            }
        }
 
        &lt;span style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Delegate the storage to the model.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Also delegate the INotifyPropertyChanged handling to the model.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR: blue"&gt;public string &lt;/span&gt;FirstName
        {
            &lt;span style="COLOR: blue"&gt;get &lt;/span&gt;{ &lt;span style="COLOR: blue"&gt;return &lt;/span&gt;_Customer.FirstName; }
            &lt;span style="COLOR: blue"&gt;set &lt;/span&gt;{ _Customer.FirstName = &lt;span style="COLOR: blue"&gt;value&lt;/span&gt;; }
        }
 
        &lt;span style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Delegate the storage to the model.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Also delegate the INotifyPropertyChanged handling to the model.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR: blue"&gt;public string &lt;/span&gt;LastName
        {
            &lt;span style="COLOR: blue"&gt;get &lt;/span&gt;{ &lt;span style="COLOR: blue"&gt;return &lt;/span&gt;_Customer.LastName; }
            &lt;span style="COLOR: blue"&gt;set &lt;/span&gt;{ _Customer.LastName = &lt;span style="COLOR: blue"&gt;value&lt;/span&gt;; }
        }
        
        &lt;span style="COLOR: blue"&gt;#region &lt;/span&gt;INotifyPropertyChanged
        ...
        &lt;span style="COLOR: blue"&gt;#endregion
 
    &lt;/span&gt;}
 
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;pre class="code"&gt; &lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;Now we need to add a collection that exposes the Shipping Addresses, but not directly. We want to expose each shipping address through a ViewModel. This means that we need to somehow wrap each item, and put these into a collection. Additionally, we are good binding citizens and expose the data as a collection that supports INotifyCollectionChanged, such as ObservableCollection.&lt;/p&gt;
&lt;p&gt;Assuming that we previously created a ViewModel for shipping address, our first attempt at modifying the CustomerVM might look like this:&lt;/p&gt;
&lt;blockquote style="HEIGHT: 400px; FONT-SIZE: 0.8em; OVERFLOW: auto"&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;using &lt;/span&gt;System.ComponentModel;
&lt;span style="COLOR: blue"&gt;using &lt;/span&gt;System.Collections.ObjectModel;
&lt;span style="COLOR: blue"&gt;using &lt;/span&gt;System;

&lt;span style="COLOR: blue"&gt;namespace &lt;/span&gt;MvvmWrappers.ViewModels
{
    &lt;span style="COLOR: blue"&gt;public class &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;CustomerVM &lt;/span&gt;: &lt;span style="COLOR: #2b91af"&gt;INotifyPropertyChanged
    &lt;/span&gt;{
        &lt;span style="COLOR: blue"&gt;private &lt;/span&gt;Models.&lt;span style="COLOR: #2b91af"&gt;Customer &lt;/span&gt;_Customer = &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;;

        &lt;span style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Constructor - Add an event handler for PropertyChanged.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR: blue"&gt;public &lt;/span&gt;CustomerVM(Models.&lt;span style="COLOR: #2b91af"&gt;Customer &lt;/span&gt;customer)
        {
            _Customer = customer;
            _Customer.PropertyChanged += &lt;/pre&gt;
&lt;pre class="code"&gt;                &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;PropertyChangedEventHandler&lt;/span&gt;(_Customer_PropertyChanged);

            &lt;span style="COLOR: green"&gt;// Wrap each ShippingAddress in a ViewModel.
            &lt;/span&gt;_ShippingAddresses = &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;&amp;gt;();
            &lt;span style="COLOR: blue"&gt;foreach &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;var &lt;/span&gt;sa &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;customer.ShippingAddresses)
                _ShippingAddresses.Add(&lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;(sa));
        }

        &lt;span style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Watch for changes in the underlying model and propagate those that
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &lt;/span&gt;&lt;span style="COLOR: green"&gt;are exposed by this ViewModel.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR: blue"&gt;void &lt;/span&gt;_Customer_PropertyChanged(&lt;span style="COLOR: blue"&gt;object &lt;/span&gt;sender, &lt;span style="COLOR: #2b91af"&gt;PropertyChangedEventArgs &lt;/span&gt;e)
        {
            &lt;span style="COLOR: blue"&gt;switch &lt;/span&gt;(e.PropertyName)
            {
                &lt;span style="COLOR: blue"&gt;case &lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;"FirstName"&lt;/span&gt;:
                &lt;span style="COLOR: blue"&gt;case &lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;"LastName"&lt;/span&gt;:
                    &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;.OnPropertyChanged(e.PropertyName);
                    &lt;span style="COLOR: blue"&gt;break&lt;/span&gt;;
            }
        }


        &lt;span style="COLOR: blue"&gt;private &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;&amp;gt; _ShippingAddresses;
        &lt;span style="COLOR: blue"&gt;public &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;&amp;gt; ShippingAddresses
        {
            &lt;span style="COLOR: blue"&gt;get &lt;/span&gt;{ &lt;span style="COLOR: blue"&gt;return &lt;/span&gt;_ShippingAddresses; }
            &lt;span style="COLOR: blue"&gt;set
            &lt;/span&gt;{
                &lt;span style="COLOR: blue"&gt;if &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;value &lt;/span&gt;== _ShippingAddresses)
                    &lt;span style="COLOR: blue"&gt;return&lt;/span&gt;;

                _ShippingAddresses = &lt;span style="COLOR: blue"&gt;value&lt;/span&gt;;
                OnPropertyChanged(&lt;span style="COLOR: #a31515"&gt;"ShippingAddresses"&lt;/span&gt;);
            }
        }
        


        &lt;span style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Delegate the storage to the model.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Also delegate the INotifyPropertyChanged handling to the model.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR: blue"&gt;public string &lt;/span&gt;FirstName
        {
            &lt;span style="COLOR: blue"&gt;get &lt;/span&gt;{ &lt;span style="COLOR: blue"&gt;return &lt;/span&gt;_Customer.FirstName; }
            &lt;span style="COLOR: blue"&gt;set &lt;/span&gt;{ _Customer.FirstName = &lt;span style="COLOR: blue"&gt;value&lt;/span&gt;; }
        }

        &lt;span style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Delegate the storage to the model.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Also delegate the INotifyPropertyChanged handling to the model.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR: blue"&gt;public string &lt;/span&gt;LastName
        {
            &lt;span style="COLOR: blue"&gt;get &lt;/span&gt;{ &lt;span style="COLOR: blue"&gt;return &lt;/span&gt;_Customer.LastName; }
            &lt;span style="COLOR: blue"&gt;set &lt;/span&gt;{ _Customer.LastName = &lt;span style="COLOR: blue"&gt;value&lt;/span&gt;; }
        }
        
        &lt;span style="COLOR: blue"&gt;#region &lt;/span&gt;INotifyPropertyChanged
        ...
        &lt;span style="COLOR: blue"&gt;#endregion

    &lt;/span&gt;}
}&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is a good start, but lacking a couple things. First off, the model’s collections support INotifyCollectionChanged (via ObservableCollection). We’d like the UI to automatically respond if an item is added or removed from the model. &lt;/p&gt;
&lt;p&gt;Likewise, we’d like any changes to the ViewModel collections to be propagated to the Model. &lt;/p&gt;
&lt;p&gt;As in the previous case, this probably means that we need to intercept the CollectionChanged event and handle it accordingly. Here’s a brute-force approach, albeit a quite painful and ugly:&lt;/p&gt;
&lt;blockquote style="HEIGHT: 400px; FONT-SIZE: 0.8em; OVERFLOW: auto"&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;using &lt;/span&gt;System;&lt;/pre&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;using &lt;/span&gt;System;
&lt;span style="COLOR: blue"&gt;using &lt;/span&gt;System.Collections.Generic;
&lt;span style="COLOR: blue"&gt;using &lt;/span&gt;System.Collections.ObjectModel;
&lt;span style="COLOR: blue"&gt;using &lt;/span&gt;System.Collections.Specialized;
&lt;span style="COLOR: blue"&gt;using &lt;/span&gt;System.ComponentModel;

&lt;span style="COLOR: blue"&gt;namespace &lt;/span&gt;MvvmWrappers.ViewModels
{
    &lt;span style="COLOR: blue"&gt;public class &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;CustomerVM &lt;/span&gt;: &lt;span style="COLOR: #2b91af"&gt;INotifyPropertyChanged
    &lt;/span&gt;{
        &lt;span style="COLOR: blue"&gt;private &lt;/span&gt;Models.&lt;span style="COLOR: #2b91af"&gt;Customer &lt;/span&gt;_Customer = &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;;
        &lt;span style="COLOR: blue"&gt;private bool &lt;/span&gt;_IgnoreChanges = &lt;span style="COLOR: blue"&gt;false&lt;/span&gt;;



        &lt;span style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Constructor - Add an event handler for PropertyChanged.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR: blue"&gt;public &lt;/span&gt;CustomerVM(Models.&lt;span style="COLOR: #2b91af"&gt;Customer &lt;/span&gt;customer)
        {
            _Customer = customer;
            _Customer.PropertyChanged += &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;PropertyChangedEventHandler&lt;/span&gt;(_Customer_PropertyChanged);

            &lt;span style="COLOR: green"&gt;// Wrap each ShippingAddress in a ViewModel.
            &lt;/span&gt;_ShippingAddresses = &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;&amp;gt;();
            &lt;span style="COLOR: blue"&gt;foreach &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;var &lt;/span&gt;sa &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;customer.ShippingAddresses)
                _ShippingAddresses.Add(&lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;(sa));


            _ShippingAddresses.CollectionChanged += &lt;/pre&gt;
&lt;pre class="code"&gt;                &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;NotifyCollectionChangedEventHandler&lt;/span&gt;(ShippingAddressVMs_CollectionChanged);
            _Customer.ShippingAddresses.CollectionChanged += &lt;/pre&gt;
&lt;pre class="code"&gt;                &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;NotifyCollectionChangedEventHandler&lt;/span&gt;(ShippingAddresses_CollectionChanged);
        }

        &lt;span style="COLOR: blue"&gt;void &lt;/span&gt;ShippingAddressVMs_CollectionChanged(&lt;span style="COLOR: blue"&gt;object &lt;/span&gt;sender, &lt;span style="COLOR: #2b91af"&gt;NotifyCollectionChangedEventArgs &lt;/span&gt;e)
        {
            &lt;span style="COLOR: blue"&gt;if &lt;/span&gt;(_IgnoreChanges)
                &lt;span style="COLOR: blue"&gt;return&lt;/span&gt;;

            _IgnoreChanges = &lt;span style="COLOR: blue"&gt;true&lt;/span&gt;;

            &lt;span style="COLOR: green"&gt;// If a reset, then e.OldItems is empty. Just clear and reload.
            &lt;/span&gt;&lt;span style="COLOR: blue"&gt;if &lt;/span&gt;(e.Action == System.Collections.Specialized.&lt;span style="COLOR: #2b91af"&gt;NotifyCollectionChangedAction&lt;/span&gt;.Reset)
            {
                _Customer.ShippingAddresses.Clear();

                &lt;span style="COLOR: blue"&gt;foreach &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;var &lt;/span&gt;sa &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;ShippingAddresses)
                    _Customer.ShippingAddresses.Add(sa.GetUnderlyingObject());
            }
            &lt;span style="COLOR: blue"&gt;else
            &lt;/span&gt;{
                &lt;span style="COLOR: green"&gt;// Remove items from collection.
                &lt;/span&gt;&lt;span style="COLOR: blue"&gt;var &lt;/span&gt;toRemove = &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;Models.&lt;span style="COLOR: #2b91af"&gt;ShippingAddress&lt;/span&gt;&amp;gt;();

                &lt;span style="COLOR: blue"&gt;if &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;null &lt;/span&gt;!= e.OldItems &amp;amp;&amp;amp; e.OldItems.Count &amp;gt; 0)
                    &lt;span style="COLOR: blue"&gt;foreach &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;var &lt;/span&gt;item &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;e.OldItems)
                        &lt;span style="COLOR: blue"&gt;foreach &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;var &lt;/span&gt;existingItem &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;_Customer.ShippingAddresses)
                            &lt;span style="COLOR: blue"&gt;if &lt;/span&gt;(((&lt;span style="COLOR: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;)item).IsViewFor(existingItem))
                                toRemove.Add(existingItem);

                &lt;span style="COLOR: blue"&gt;foreach &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;var &lt;/span&gt;item &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;toRemove)
                    _Customer.ShippingAddresses.Remove(item);

                &lt;span style="COLOR: green"&gt;// Add new items to the collection.
                &lt;/span&gt;&lt;span style="COLOR: blue"&gt;if &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;null &lt;/span&gt;!= e.NewItems &amp;amp;&amp;amp; e.NewItems.Count &amp;gt; 0)
                    &lt;span style="COLOR: blue"&gt;foreach &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;var &lt;/span&gt;item &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;e.NewItems)
                        _Customer.ShippingAddresses.Add(((&lt;span style="COLOR: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;)item).GetUnderlyingObject());
            }

            _IgnoreChanges = &lt;span style="COLOR: blue"&gt;false&lt;/span&gt;;
        }

        &lt;span style="COLOR: blue"&gt;void &lt;/span&gt;ShippingAddresses_CollectionChanged(&lt;span style="COLOR: blue"&gt;object &lt;/span&gt;sender, &lt;span style="COLOR: #2b91af"&gt;NotifyCollectionChangedEventArgs &lt;/span&gt;e)
        {
            &lt;span style="COLOR: blue"&gt;if &lt;/span&gt;(_IgnoreChanges)
                &lt;span style="COLOR: blue"&gt;return&lt;/span&gt;;

            _IgnoreChanges = &lt;span style="COLOR: blue"&gt;true&lt;/span&gt;;
            
            &lt;span style="COLOR: green"&gt;// If a reset, then e.OldItems is emtpy. Just clear and reload.
            &lt;/span&gt;&lt;span style="COLOR: blue"&gt;if &lt;/span&gt;(e.Action == System.Collections.Specialized.&lt;span style="COLOR: #2b91af"&gt;NotifyCollectionChangedAction&lt;/span&gt;.Reset)
            {
                ShippingAddresses.Clear();

                &lt;span style="COLOR: blue"&gt;foreach &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;var &lt;/span&gt;sa &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;_Customer.ShippingAddresses)
                    _ShippingAddresses.Add(&lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;(sa));
            }
            &lt;span style="COLOR: blue"&gt;else
            &lt;/span&gt;{
                &lt;span style="COLOR: green"&gt;// Remove items from collection.
                &lt;/span&gt;&lt;span style="COLOR: blue"&gt;var &lt;/span&gt;toRemove = &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;&amp;gt;();

                &lt;span style="COLOR: blue"&gt;if &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;null &lt;/span&gt;!= e.OldItems &amp;amp;&amp;amp; e.OldItems.Count &amp;gt; 0)
                    &lt;span style="COLOR: blue"&gt;foreach &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;var &lt;/span&gt;item &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;e.OldItems)
                        &lt;span style="COLOR: blue"&gt;foreach &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;var &lt;/span&gt;existingItem &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;ShippingAddresses)
                            &lt;span style="COLOR: blue"&gt;if &lt;/span&gt;(existingItem.IsViewFor((Models.&lt;span style="COLOR: #2b91af"&gt;ShippingAddress&lt;/span&gt;)item))
                                toRemove.Add(existingItem);

                &lt;span style="COLOR: blue"&gt;foreach &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;var &lt;/span&gt;item &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;toRemove)
                    ShippingAddresses.Remove(item);

                &lt;span style="COLOR: green"&gt;// Add new items to the collection.
                &lt;/span&gt;&lt;span style="COLOR: blue"&gt;if &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;null &lt;/span&gt;!= e.NewItems &amp;amp;&amp;amp; e.NewItems.Count &amp;gt; 0)
                    &lt;span style="COLOR: blue"&gt;foreach &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;var &lt;/span&gt;item &lt;span style="COLOR: blue"&gt;in &lt;/span&gt;e.NewItems)
                        ShippingAddresses.Add(&lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;((Models.&lt;span style="COLOR: #2b91af"&gt;ShippingAddress&lt;/span&gt;)item));
            }

            _IgnoreChanges = &lt;span style="COLOR: blue"&gt;false&lt;/span&gt;;
        }

        &lt;span style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Watch for changes in the underlying model and propagate those that
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &lt;/span&gt;&lt;span style="COLOR: green"&gt;are exposed by this ViewModel.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR: blue"&gt;void &lt;/span&gt;_Customer_PropertyChanged(&lt;span style="COLOR: blue"&gt;object &lt;/span&gt;sender, &lt;span style="COLOR: #2b91af"&gt;PropertyChangedEventArgs &lt;/span&gt;e)
        {
            &lt;span style="COLOR: blue"&gt;switch &lt;/span&gt;(e.PropertyName)
            {
                &lt;span style="COLOR: blue"&gt;case &lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;"FirstName"&lt;/span&gt;:
                &lt;span style="COLOR: blue"&gt;case &lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;"LastName"&lt;/span&gt;:
                    &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;.OnPropertyChanged(e.PropertyName);
                    &lt;span style="COLOR: blue"&gt;break&lt;/span&gt;;

                &lt;span style="COLOR: blue"&gt;case &lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;"ShippingAddresses"&lt;/span&gt;:
                    &lt;span style="COLOR: green"&gt;// Underlying collection was rebuilt.
                    &lt;/span&gt;&lt;span style="COLOR: blue"&gt;this&lt;/span&gt;.ShippingAddresses_CollectionChanged(
                        &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;,
                        &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;NotifyCollectionChangedEventArgs&lt;/span&gt;(
                            &lt;span style="COLOR: #2b91af"&gt;NotifyCollectionChangedAction&lt;/span&gt;.Reset
                        )
                    );

                    &lt;span style="COLOR: blue"&gt;break&lt;/span&gt;;
            }
        }

        &lt;span style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR: green"&gt;A collection of shipping addresses, wrapped in a ShippingAddressVM ViewModel
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR: blue"&gt;private &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;&amp;gt; _ShippingAddresses;
        &lt;span style="COLOR: blue"&gt;public &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="COLOR: #2b91af"&gt;ShippingAddressVM&lt;/span&gt;&amp;gt; ShippingAddresses
        {
            &lt;span style="COLOR: blue"&gt;get &lt;/span&gt;{ &lt;span style="COLOR: blue"&gt;return &lt;/span&gt;_ShippingAddresses; }
            &lt;span style="COLOR: blue"&gt;set
            &lt;/span&gt;{
                &lt;span style="COLOR: blue"&gt;if &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;value &lt;/span&gt;== _ShippingAddresses)
                    &lt;span style="COLOR: blue"&gt;return&lt;/span&gt;;

                _ShippingAddresses.CollectionChanged -= ShippingAddressVMs_CollectionChanged;
                _ShippingAddresses = &lt;span style="COLOR: blue"&gt;value&lt;/span&gt;;
                _ShippingAddresses.CollectionChanged += ShippingAddressVMs_CollectionChanged;

                &lt;span style="COLOR: green"&gt;// Underlying collection was rebuilt.
                &lt;/span&gt;&lt;span style="COLOR: blue"&gt;this&lt;/span&gt;.ShippingAddressVMs_CollectionChanged(
                    &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;,
                    &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;NotifyCollectionChangedEventArgs&lt;/span&gt;(
                        &lt;span style="COLOR: #2b91af"&gt;NotifyCollectionChangedAction&lt;/span&gt;.Reset
                    )
                );

                OnPropertyChanged(&lt;span style="COLOR: #a31515"&gt;"ShippingAddresses"&lt;/span&gt;);
            }
        }

        &lt;span style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Delegate the storage to the model.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Also delegate the INotifyPropertyChanged handling to the model.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR: blue"&gt;public string &lt;/span&gt;FirstName
        {
            &lt;span style="COLOR: blue"&gt;get &lt;/span&gt;{ &lt;span style="COLOR: blue"&gt;return &lt;/span&gt;_Customer.FirstName; }
            &lt;span style="COLOR: blue"&gt;set &lt;/span&gt;{ _Customer.FirstName = &lt;span style="COLOR: blue"&gt;value&lt;/span&gt;; }
        }

        &lt;span style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Delegate the storage to the model.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Also delegate the INotifyPropertyChanged handling to the model.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR: blue"&gt;public string &lt;/span&gt;LastName
        {
            &lt;span style="COLOR: blue"&gt;get &lt;/span&gt;{ &lt;span style="COLOR: blue"&gt;return &lt;/span&gt;_Customer.LastName; }
            &lt;span style="COLOR: blue"&gt;set &lt;/span&gt;{ _Customer.LastName = &lt;span style="COLOR: blue"&gt;value&lt;/span&gt;; }
        }

        &lt;span style="COLOR: blue"&gt;#region &lt;/span&gt;INotifyPropertyChanged&lt;/pre&gt;
&lt;pre class="code"&gt;        ...
        &lt;span style="COLOR: blue"&gt;#endregion

    &lt;/span&gt;}
}&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Now we see that the collections stay in sync when items are added to the Model. The Model also stays in sync with changes to the ViewModel (I wrote Unit Tests to prove it ;-). This implementation works, but is less than ideal.&lt;/p&gt;
&lt;p&gt;What to do? Is there a simple way to achieve MVVMs goals of wrapping the Model in VMs, including collections?&lt;/p&gt;
&lt;p&gt;There may be hope! See &lt;a href="http://blog.alner.net/archive/2010/02/09/mvvm-to-wrap-or-not-to-wrap-blinq-and-clinq.aspx"&gt;part 3&lt;/a&gt; for a review of some interesting frameworks that help with objects that support change notification. We’ll look into using something like &lt;a target="_blank" href="http://bindablelinq.codeplex.com/"&gt;BLINQ&lt;/a&gt;,  &lt;a target="_blank" href="http://clinq.codeplex.com/"&gt;CLINQ&lt;/a&gt;, or &lt;a target="_blank" href="http://obtics.codeplex.com/"&gt;Obtics&lt;/a&gt;. These three frameworks offer LINQ based implementations that can expose an observable collection as a new, synchronized collection that is based on a LINQ query against the source.&lt;/p&gt;
&lt;p&gt;Cheers!&lt;/p&gt;&lt;img src="http://blog.alner.net/aggbug/87.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blog.alner.net/comments/87.aspx</wfw:comment>
        <slash:comments>4</slash:comments>
        <wfw:commentRss>http://blog.alner.net/comments/commentRss/87.aspx</wfw:commentRss>
        <trackback:ping>http://blog.alner.net/services/trackbacks/87.aspx</trackback:ping>
    </entry>
    <entry>
        <title>MVVM: To Wrap or Not to Wrap? How much should the ViewModel wrap the Model? (Part 1)</title>
        <link rel="alternate" type="text/html" href="http://blog.alner.net/archive/2010/02/09/mvvm-to-wrap-or-not-to-wrap.aspx" />
        <id>http://blog.alner.net/archive/2010/02/09/mvvm-to-wrap-or-not-to-wrap.aspx</id>
        <published>2010-02-09T11:11:36Z</published>
        <updated>2010-02-09T17:38:50Z</updated>
        <content type="html">&lt;p&gt;The WPF community seems pretty happy with the MVVM pattern for WPF development. I can see why. It’s a great fit for WPF and its strong data-binding support. It provides a very nice layer for managing UI state, translating, formatting, and aggregating data.&lt;/p&gt;
&lt;p&gt;So let’s say that I adopt MVVM for my project. I think there are some scenarios that still need definition. One of these is “To Wrap or Not to Wrap?” Let me explain…&lt;/p&gt;
&lt;p&gt;MVVM says that a ViewModel sits between the View (XAML) and the Model. The ViewModel (VM) exposes data from the model plus additional view-specific details that the view (V) can easily bind to. Ideally, the view can be constructed with zero code-behind. Pure XAML with bindings to manage data and UI state. This is all well and good.&lt;/p&gt;
&lt;p&gt;Now, lets say that our model is reasonably real-world and has a entities that contain collections of other entities, which in turn contain collections of other entities. Maybe some of these even have references back to other trees of data. The classic example is customers and orders. &lt;/p&gt;
&lt;h3&gt;The Model:&lt;/h3&gt;
&lt;p&gt;Customer &lt;br /&gt;
    |_ Shipping Addresses (Collection) &lt;br /&gt;
    |_ Billing Address &lt;br /&gt;
    |_ Orders (Collection) &lt;br /&gt;
    |_ First Name &lt;br /&gt;
    |_ Last Name &lt;br /&gt;
    |_ Etc…&lt;/p&gt;
&lt;h3&gt;The Question:&lt;/h3&gt;
&lt;p&gt;I want to use MVVM, so I start working on the view models. I create one for Customer, start adding my properties and realize that I have a decision to make. &lt;em&gt;Do I expose the model directly, or do I wrap each item in its own view model?&lt;/em&gt; &lt;/p&gt;
&lt;p&gt;We have a couple options, and each has some consequences. &lt;/p&gt;
&lt;h3&gt;STEP 1: The Customer Object&lt;/h3&gt;
&lt;p&gt;Let’s start with the Customer object itself:&lt;/p&gt;
&lt;p&gt;Customer has a bunch of data on it. The VM guidelines suggest that the VM should expose a property for each value on the model that it wants to expose. In this case, I want to show the first and last name on the view.&lt;/p&gt;
&lt;h4&gt;Option 1: Wrap Customer Model&lt;/h4&gt;
&lt;p&gt;To wrap first and last name, I create two new properties on the VM. One for each property. Being a good binding citizen, I make the VM support INotifyPropertyChanged and setup my property setter’s accordingly. (See my &lt;a href="http://blog.alner.net/archive/2010/02/01/inotifypropertychanged-snippets.aspx"&gt;INotifyPropertyChanged Snippets post&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;My model happens to support INotifyPropertyChanged too, and I want the View to know right away if the data changes in the underlying model. What to do? Looks like I need to add an event handler on my Customer instance’s PropertyChanged event. In that event, I need to check what property changed and bubble selected events via my VM’s own PropertyChanged event. Here’s the CustomerVM class so far…&lt;/p&gt;
&lt;blockquote style="HEIGHT: 400px; FONT-SIZE: 0.8em; OVERFLOW: auto"&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;using &lt;/span&gt;System.ComponentModel;
&lt;span style="COLOR: blue"&gt;using &lt;/span&gt;System.Collections.ObjectModel;

&lt;span style="COLOR: blue"&gt;namespace &lt;/span&gt;MvvmWrappers.ViewModels
{
    &lt;span style="COLOR: blue"&gt;public class &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;CustomerVM &lt;/span&gt;: &lt;span style="COLOR: #2b91af"&gt;INotifyPropertyChanged
    &lt;/span&gt;{
        &lt;span style="COLOR: blue"&gt;private &lt;/span&gt;Models.&lt;span style="COLOR: #2b91af"&gt;Customer &lt;/span&gt;_Customer = &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;;

        &lt;span style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Constructor - Add an event handler for PropertyChanged.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR: blue"&gt;public &lt;/span&gt;CustomerVM(Models.&lt;span style="COLOR: #2b91af"&gt;Customer &lt;/span&gt;customer)
        {
            _Customer = customer;
            _Customer.PropertyChanged += &lt;span style="COLOR: blue"&gt;new &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;PropertyChangedEventHandler&lt;/span&gt;(_Customer_PropertyChanged);
        }

        &lt;span style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Watch for changes in the underlying model and propagate those that
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &lt;/span&gt;&lt;span style="COLOR: green"&gt;are exposed by this ViewModel.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR: blue"&gt;void &lt;/span&gt;_Customer_PropertyChanged(&lt;span style="COLOR: blue"&gt;object &lt;/span&gt;sender, &lt;span style="COLOR: #2b91af"&gt;PropertyChangedEventArgs &lt;/span&gt;e)
        {
            &lt;span style="COLOR: blue"&gt;switch &lt;/span&gt;(e.PropertyName)
            {
                &lt;span style="COLOR: blue"&gt;case &lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;"FirstName"&lt;/span&gt;:
                &lt;span style="COLOR: blue"&gt;case &lt;/span&gt;&lt;span style="COLOR: #a31515"&gt;"LastName"&lt;/span&gt;:
                    &lt;span style="COLOR: blue"&gt;this&lt;/span&gt;.OnPropertyChanged(e.PropertyName);
                    &lt;span style="COLOR: blue"&gt;break&lt;/span&gt;;
            }
        }

        &lt;span style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Delegate the storage to the model.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Also delegate the INotifyPropertyChanged handling to the model.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR: blue"&gt;public string &lt;/span&gt;FirstName
        {
            &lt;span style="COLOR: blue"&gt;get &lt;/span&gt;{ &lt;span style="COLOR: blue"&gt;return &lt;/span&gt;_Customer.FirstName; }
            &lt;span style="COLOR: blue"&gt;set &lt;/span&gt;{ _Customer.FirstName = &lt;span style="COLOR: blue"&gt;value&lt;/span&gt;; }
        }

        &lt;span style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
        /// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Delegate the storage to the model.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &lt;/span&gt;&lt;span style="COLOR: green"&gt;Also delegate the INotifyPropertyChanged handling to the model.
        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
        &lt;/span&gt;&lt;span style="COLOR: blue"&gt;public string &lt;/span&gt;LastName
        {
            &lt;span style="COLOR: blue"&gt;get &lt;/span&gt;{ &lt;span style="COLOR: blue"&gt;return &lt;/span&gt;_Customer.LastName; }
            &lt;span style="COLOR: blue"&gt;set &lt;/span&gt;{ _Customer.LastName = &lt;span style="COLOR: blue"&gt;value&lt;/span&gt;; }
        }
        
        &lt;span style="COLOR: blue"&gt;#region &lt;/span&gt;INotifyPropertyChanged
        ...
        &lt;span style="COLOR: blue"&gt;#endregion

    &lt;/span&gt;}

}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;pre class="code"&gt; &lt;/pre&gt;
&lt;/blockquote&gt;
&lt;h4&gt;Option 2: Not to Wrap&lt;/h4&gt;
&lt;p&gt;Let's say that I decide the above code is too much. So, I choose to expose the Customer model directly to the view. I keep the VM around though, because I may have other view related properties that I want to manage. These might include things like whether to expand or collapse the shipping addresses section. The VM is still valuable, but is not doing as much.&lt;/p&gt;
&lt;blockquote style="HEIGHT: 400px; FONT-SIZE: 0.8em; OVERFLOW: auto"&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;public class &lt;/span&gt;&lt;span style="COLOR: #2b91af"&gt;CustomerVM &lt;/span&gt;: &lt;span style="COLOR: #2b91af"&gt;INotifyPropertyChanged
&lt;/span&gt;{
    &lt;span style="COLOR: blue"&gt;private &lt;/span&gt;Models.Customer _Customer = &lt;span style="COLOR: blue"&gt;null&lt;/span&gt;;
    &lt;span style="COLOR: blue"&gt;public &lt;/span&gt;CustomerVM(Models.Customer customer)
    {
        _Customer = customer;
    }

    
    &lt;span style="COLOR: blue"&gt;private &lt;/span&gt;Models.Customer _Customer;
    &lt;span style="COLOR: blue"&gt;public &lt;/span&gt;Models.Customer Customer 
    {
        &lt;span style="COLOR: blue"&gt;get&lt;/span&gt;{ &lt;span style="COLOR: blue"&gt;return &lt;/span&gt;_Customer;}
        &lt;span style="COLOR: blue"&gt;set
        &lt;/span&gt;{
            &lt;span style="COLOR: blue"&gt;if &lt;/span&gt;(&lt;span style="COLOR: blue"&gt;value &lt;/span&gt;== _Customer)
                &lt;span style="COLOR: blue"&gt;return&lt;/span&gt;;
            
            _Customer = &lt;span style="COLOR: blue"&gt;value&lt;/span&gt;;
            OnPropertyChanged(&lt;span style="COLOR: #a31515"&gt;"Customer"&lt;/span&gt;);
        }
    }

    &lt;span style="COLOR: blue"&gt;#region &lt;/span&gt;INotifyPropertyChanged
    ...
    &lt;span style="COLOR: blue"&gt;#endregion

&lt;/span&gt;}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt; &lt;/p&gt;
&lt;h4&gt;Conclusions:&lt;/h4&gt;
&lt;p&gt;Where do we stand? Both options are viable (IMHO anyway). &lt;/p&gt;
&lt;p&gt;The first options seems to be a more rigorous and “pure” implementation, but the cost is additional plumbing. In some cases, this is definitely justified. For example, I may want to have a property that exposes the full name in a single field. Here, the VM is very helpful and makes this task simple. However, there are many cases where the values are simply passed through.&lt;/p&gt;
&lt;p&gt;The second option is simpler and seems more pragmatic. It says “I’ll add properties on the VM where they are needed, but I’m not trying to control all access to the model.” &lt;/p&gt;
&lt;p&gt;In reviewing the sample application from the &lt;a href="http://caliburn.codeplex.com/"&gt;Caliburn framework&lt;/a&gt;, they implement the VM using option 2. The ViewModel exposes the model directly, including a number of child collections. The view then binds to both the model and the VM. This seems reasonable and simpler, but exposes the model directly to the view. &lt;/p&gt;
&lt;p&gt;There are discussions of this question by others, but I don’t see conclusive answers. Ward Bell asks the question in his &lt;a href="http://neverindoubtnet.blogspot.com/2009/05/birth-and-death-of-m-v-vm-triads.html"&gt;Birth and Death of MVVM Triads&lt;/a&gt;. The question is also &lt;a href="http://stackoverflow.com/questions/1114555/a-view-model-mvvm-design-issue"&gt;asked on StackOverflow&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For me, the jury is still out. I prefer Option 1 because is it a bit more pure and clean. In the above case, the complexity is manageable. Things get more interesting in &lt;a href="http://blog.alner.net/archive/2010/02/09/mvvm-to-wrap-or-not-to-wrap-should-viewmodels-wrap.aspx"&gt;Part 2 of this post&lt;/a&gt;, where I’ll show some scenarios related to exposing a collection. &lt;/p&gt;
&lt;p&gt;Cheers!&lt;/p&gt;&lt;img src="http://blog.alner.net/aggbug/86.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blog.alner.net/comments/86.aspx</wfw:comment>
        <slash:comments>4</slash:comments>
        <wfw:commentRss>http://blog.alner.net/comments/commentRss/86.aspx</wfw:commentRss>
        <trackback:ping>http://blog.alner.net/services/trackbacks/86.aspx</trackback:ping>
    </entry>
    <entry>
        <title>C# Snippets for Properties that support INotifyPropertyChanged</title>
        <link rel="alternate" type="text/html" href="http://blog.alner.net/archive/2010/02/01/inotifypropertychanged-snippets.aspx" />
        <id>http://blog.alner.net/archive/2010/02/01/inotifypropertychanged-snippets.aspx</id>
        <published>2010-02-01T15:08:16Z</published>
        <updated>2010-02-26T00:50:24Z</updated>
        <content type="html">&lt;p&gt;Since WPF likes INotifyPropertyChanged on bound objects, I looked for a snippet to help out. &lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.designerwpf.com/2009/04/30/inotifypropertychanged-snippets-and-why-you-should-use-these-instead-of-dependencyproperties/"&gt;Matthias Shapiro&lt;/a&gt; had a good version for the interface implementation and a property declaration. I made a couple adjustments to the snippets based on personal preference (check if the value has changed before raising the event / remove the region from the property. &lt;/p&gt;
&lt;p&gt;Then added some property verification support based on the snippet sample from &lt;a href="http://www.hardcodet.net/2008/03/resharper-snippet-for-inotifypropertychanged"&gt;Philipp Sumi&lt;/a&gt;. The property verification uses reflection to check if the property exists. If not, it throws an exception. This function is marked with a Conditional attribute so that it is only called in DEBUG builds.&lt;/p&gt;
&lt;p&gt;The resulting snippets are as follows:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;INotifyPropertyChanged Interface Implementation&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;xml &lt;/span&gt;&lt;span style="COLOR: red"&gt;version&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;1.0&lt;/span&gt;" &lt;span style="COLOR: red"&gt;encoding&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;utf-8&lt;/span&gt;" &lt;span style="COLOR: blue"&gt;?&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;CodeSnippets &lt;/span&gt;&lt;span style="COLOR: red"&gt;xmlns&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;    &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;CodeSnippet &lt;/span&gt;&lt;span style="COLOR: red"&gt;Format&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;1.0.0&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Header&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Author&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                &lt;/span&gt;Matthias Shapiro (Original), &lt;br /&gt;                Philipp Sumi (VerifyProperty method), &lt;br /&gt;                Nathan Allen-Wagner (Modifications)&lt;br /&gt;            &lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Author&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;HelpUrl&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;http://blog.alner.net/&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;HelpUrl&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;SnippetTypes&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;SnippetType&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;Expansion&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;SnippetType&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;            &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;SnippetTypes&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Title&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                &lt;/span&gt;NotifyObject: &lt;br /&gt;                Use this to add the INotifyPropertyChange implementation&lt;br /&gt;                to a class. Supports property name verification in DEBUG&lt;br /&gt;                builds.&lt;br /&gt;            &lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Title&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Shortcut&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;notifyo&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Shortcut&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;        &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Header&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Snippet&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Code &lt;/span&gt;&lt;span style="COLOR: red"&gt;Language&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;csharp&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                &amp;lt;![CDATA[&lt;/span&gt;&lt;span style="COLOR: gray"&gt;#region INotifyPropertyChanged&lt;br /&gt;&lt;br /&gt;                /// &amp;lt;summary&amp;gt;&lt;br /&gt;                /// The PropertyChanged event is used by consuming code&lt;br /&gt;                /// (like WPF's binding infrastructure) to detect when&lt;br /&gt;                /// a value has changed.&lt;br /&gt;                /// &amp;lt;/summary&amp;gt;&lt;br /&gt;                public event PropertyChangedEventHandler PropertyChanged;&lt;br /&gt;&lt;br /&gt;                /// &amp;lt;summary&amp;gt;&lt;br /&gt;                /// Raise the PropertyChanged event for the &lt;br /&gt;                /// specified property.&lt;br /&gt;                /// &amp;lt;/summary&amp;gt;&lt;br /&gt;                /// &amp;lt;param name="propertyName"&amp;gt;&lt;br /&gt;                /// A string representing the name of &lt;br /&gt;                /// the property that changed.&amp;lt;/param&amp;gt;&lt;br /&gt;                /// &amp;lt;remarks&amp;gt;&lt;br /&gt;                /// Only raise the event if the value of the property &lt;br /&gt;                /// has changed from its previous value&amp;lt;/remarks&amp;gt;&lt;br /&gt;                protected void OnPropertyChanged(string propertyName)&lt;br /&gt;                {&lt;br /&gt;                    // Validate the property name in debug builds&lt;br /&gt;                    VerifyProperty(propertyName);&lt;br /&gt;&lt;br /&gt;                    if(null != PropertyChanged)&lt;br /&gt;                    {&lt;br /&gt;                        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));&lt;br /&gt;                    }&lt;br /&gt;                }&lt;br /&gt;                &lt;br /&gt;                /// &amp;lt;summary&amp;gt;&lt;br /&gt;                /// Verifies whether the current class provides a property with a given&lt;br /&gt;                /// name. This method is only invoked in debug builds, and results in&lt;br /&gt;                /// a runtime exception if the &amp;lt;see cref="OnPropertyChanged"/&amp;gt; method&lt;br /&gt;                /// is being invoked with an invalid property name. This may happen if&lt;br /&gt;                /// a property's name was changed but not the parameter of the property's&lt;br /&gt;                /// invocation of &amp;lt;see cref="OnPropertyChanged"/&amp;gt;.&lt;br /&gt;                /// &amp;lt;/summary&amp;gt;&lt;br /&gt;                /// &amp;lt;param name="propertyName"&amp;gt;The name of the changed property.&amp;lt;/param&amp;gt;&lt;br /&gt;                [System.Diagnostics.Conditional("DEBUG")]&lt;br /&gt;                private void VerifyProperty(string propertyName)&lt;br /&gt;                {&lt;br /&gt;                    Type type = this.GetType();&lt;br /&gt;&lt;br /&gt;                    // Look for a *public* property with the specified name&lt;br /&gt;                    System.Reflection.PropertyInfo pi = type.GetProperty(propertyName);&lt;br /&gt;                    if (pi == null)&lt;br /&gt;                    {&lt;br /&gt;                        // There is no matching property - notify the developer&lt;br /&gt;                        string msg = "OnPropertyChanged was invoked with invalid " + &lt;br /&gt;                                        "property name {0}. {0} is not a public " + &lt;br /&gt;                                        "property of {1}.";&lt;br /&gt;                        msg = String.Format(msg, propertyName, type.FullName);&lt;br /&gt;                        System.Diagnostics.Debug.Fail(msg);&lt;br /&gt;                    }&lt;br /&gt;                }&lt;br /&gt;&lt;br /&gt;        #endregion&lt;br /&gt;        $end$&lt;/span&gt;&lt;span style="COLOR: blue"&gt;]]&amp;gt;&lt;br /&gt;            &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Code&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;        &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Snippet&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;    &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;CodeSnippet&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;CodeSnippets&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;font color="#0000ff" /&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;font color="#0000ff"&gt;&lt;strong&gt;Property Declaration for INotifyPropertyChanged&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;
&lt;blockquote&gt;&lt;font color="#0000ff" /&gt;
&lt;pre class="code"&gt;&lt;span style="COLOR: blue"&gt;&lt;font color="#0000ff"&gt;&amp;lt;?&lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;&lt;font color="#0000ff"&gt;xml &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: red"&gt;&lt;font color="#0000ff"&gt;version&lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;1.0&lt;/span&gt;" &lt;span style="COLOR: red"&gt;encoding&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;utf-8&lt;/span&gt;" &lt;span style="COLOR: blue"&gt;?&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;CodeSnippets &lt;/span&gt;&lt;span style="COLOR: red"&gt;xmlns&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;    &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;CodeSnippet &lt;/span&gt;&lt;span style="COLOR: red"&gt;Format&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;1.0.0&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Header&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Author&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                &lt;/span&gt;Matthias Shapiro (Original), &lt;br /&gt;                Nathan Allen-Wagner (Modifications)&lt;br /&gt;            &lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Author&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;HelpUrl&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;http://blog.alner.net/&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;HelpUrl&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;SnippetTypes&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;SnippetType&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;Expansion&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;SnippetType&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;            &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;SnippetTypes&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Title&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                &lt;/span&gt;INotifyPropertyChanged Property: Use this to create a&lt;br /&gt;                new property INotifyPropertyChanged implementation.&lt;br /&gt;            &lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Title&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Shortcut&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;notifyp&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Shortcut&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;        &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Header&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;        &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Snippet&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Declarations&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Literal&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                    &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;ID&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;publicProperty&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;ID&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                    &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;ToolTip&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;The name of the private property&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;ToolTip&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                    &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Default&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;MyProperty&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Default&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Literal&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Literal&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                    &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;ID&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;type&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;ID&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                    &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;ToolTip&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                        &lt;/span&gt;The type of the property&lt;br /&gt;                        (e.g. string, double, bool, Brush, etc.)&lt;br /&gt;                    &lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;ToolTip&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                    &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Default&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;string&lt;span style="COLOR: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Default&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Literal&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;            &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Declarations&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;            &amp;lt;&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Code &lt;/span&gt;&lt;span style="COLOR: red"&gt;Language&lt;/span&gt;&lt;span style="COLOR: blue"&gt;=&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;csharp&lt;/span&gt;"&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;                &amp;lt;![CDATA[&lt;br /&gt;        &lt;/span&gt;&lt;span style="COLOR: gray"&gt;private $type$ _$publicProperty$;&lt;br /&gt;        public $type$ $publicProperty$ &lt;br /&gt;        {&lt;br /&gt;            get{ return _$publicProperty$;}&lt;br /&gt;            set&lt;br /&gt;            {&lt;br /&gt;                if (value == _$publicProperty$)&lt;br /&gt;                    return;&lt;br /&gt;                &lt;br /&gt;                _$publicProperty$ = value;&lt;br /&gt;                OnPropertyChanged("$publicProperty$");&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        $end$&lt;/span&gt;&lt;span style="COLOR: blue"&gt;]]&amp;gt;&lt;br /&gt;            &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Code&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;        &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;Snippet&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;    &amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;CodeSnippet&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR: rgb(163,21,21)"&gt;CodeSnippets&lt;/span&gt;&lt;span style="COLOR: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;As usual, &lt;em&gt;use at your own risk.&lt;/em&gt; Let me know if you find any issues.&lt;/p&gt;
&lt;p&gt;Cheers!&lt;/p&gt;&lt;img src="http://blog.alner.net/aggbug/85.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blog.alner.net/comments/85.aspx</wfw:comment>
        <slash:comments>2</slash:comments>
        <wfw:commentRss>http://blog.alner.net/comments/commentRss/85.aspx</wfw:commentRss>
        <trackback:ping>http://blog.alner.net/services/trackbacks/85.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Win7: Large Fonts a Little Rough&amp;hellip;</title>
        <link rel="alternate" type="text/html" href="http://blog.alner.net/archive/2010/01/27/win7-large-fonts-a-little-roughhellip.aspx" />
        <id>http://blog.alner.net/archive/2010/01/27/win7-large-fonts-a-little-roughhellip.aspx</id>
        <published>2010-01-27T12:02:02Z</published>
        <updated>2010-01-27T12:02:02Z</updated>
        <content type="html">&lt;p&gt;Previously, I posted:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;Large Fonts – Not sure yet…&lt;/strong&gt;       &lt;br /&gt;My machine has a pretty high resolution display (1680 x 1050). Win 7 defaulted the fonts to use “medium” fonts, instead of small fonts. This is nice because things are not so tiny, but it has to resize some of the images here an there. Also, not all applications have good support for “larger fonts”, so the windows can have an odd layout in some cases. I like that Win 7 is supporting large fonts and hi-res displays better. I think it’s just a matter of time for apps to start handling this more gracefully.       &lt;br /&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;So, I like the fact that it makes my fonts readable on a high-res monitor, but I have noticed that many apps don’t work quite right. This shows up on web sites, Seesmic’s intro screens, etc. It also makes a number of items slightly blurry since windows has to re-size bitmap images. &lt;/p&gt;  &lt;p&gt;I think that “large font” support will improve over time, but for the next couple days, I’ll switch to the default size and see how that goes.&lt;/p&gt;  &lt;p&gt;Cheers!&lt;/p&gt;&lt;img src="http://blog.alner.net/aggbug/84.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://blog.alner.net/comments/84.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://blog.alner.net/comments/commentRss/84.aspx</wfw:commentRss>
        <trackback:ping>http://blog.alner.net/services/trackbacks/84.aspx</trackback:ping>
    </entry>
</feed>