title: How to update MVVM nested ViewModels when Model changes and vice versa?
body: <p>I'm looking for some advice on how to solve a problem which is bugging us at the moment.</p>
  
  <p>Let's say we have a couple of business objects (POCOs) like</p>
  
  <pre><code>public class UserGroup
  {
      public virtual ICollection&lt;Person&gt; People { get; set; }
  }
  
  public class Person
  {
      public virtual ICollection&lt;Adress&gt; Adresses { get; set; }
  }
  
  public class Adress
  {
      public virtual string StreetName { get; set; }
  }
  </code></pre>
  
  <p>This is a bit simplistic but I hope it's enough so that you get the idea. <em>UserGroup</em> has a collection of <em>Person</em> instances and each <em>Person</em> instance has a collection of <em>Address</em> instances.</p>
  
  <p>The ViewModel for the <em>UserGroup</em> POCO could possibly look like this:</p>
  
  <pre><code>public class UserGroupViewModel
  {
      private UserGroup userGroupModel;
  
      public UserGroup UserGroupModel
      {
          get { return this.userGroupModel; }
  
          set
          {
              this.userGroupModel = value;
  
              this.PeopleViewModelCollection =
                   new ObservableCollection&lt;PeopleViewModel&gt;();
  
              foreach (Person p in this.UserGroupModel.People)
              {
                  var personViewModel = new PersonViewModel();
  
  
                  personViewModel.PersonModel = p;
  
                  this.PeopleViewModelCollection.Add(personViewModel);
              }
          }
      }
  
      public ObservableCollection&lt;PersonViewModel&gt; PersonViewModelCollection
      {
          get;
          set;
      }
  }
  </code></pre>
  
  <p>Where as the ViewModel for the <em>Person</em> POCO could look like this:</p>
  
  <pre><code>public class PersonViewModel
  {
      private Person personModel;
  
      public Person PersonModel
      {
          get { return this.personModel; }
  
          set
          {
              this.personModel = value;
  
              this.AddressViewModelCollection =
                   new ObservableCollection&lt;AddressViewModel&gt;();
  
              foreach (Address a in this.PersonModel.Adresses)
              {
                  var addressViewModel = new AddressViewModel();
  
  
                  addressViewModel.AddressModel = a;
  
                  this.AdressViewModelCollection.Add(addressViewModel);
              }
          }
      }
  
      public ObservableCollection&lt;AddressViewModel&gt; AddressViewModelCollection
      {
          get;
          set;
      }
  }
  </code></pre>
  
  <p>Again it is overly simplistic but what I want to show is that ViewModels have ObserableCollections of other ViewModels nested inside them.</p>
  
  <p>The setter of the respective Model property, e.g. <code>PersonViewModel.PersonModel</code> does create ViewModels for all the adresses of the <code>Person</code> and adds them to the <code>ObservableCollection&lt;AdressViewModels&gt; AdressViewModelCollection</code> property of the <code>PersonViewModel</code>.</p>
  
  <p>At this point we could use this code to display these ViewModels in a View. We can e.g. display the <code>StreetName</code> of a <code>Person</code>'s <code>Adress</code>.</p>
  
  <p>Now what should you do when you delete an <code>Adress</code> of a <code>Person</code>? Removing the <code>AdressViewModel</code> from the <code>PersonViewModel.AdressViewModelCollection</code> will update the GUI but it actually does not allow to update the underlying models.</p>
  
  <p>Similar to that if you add another <code>Adress</code> to a <code>Person</code> Model the existing ViewModels are not going to reflect this change since the <code>PersonViewModel.AdressViewModelCollection</code> would only be rebuilt once the <code>PersonViewModel.PersonModel</code> property is set again.</p>
  
  <p>Is it there a (preferably easy) way to achieve this two-way update between ViewModel and Model? Or maybe this is not advisable and it's better to handle this problem in a completely different way?</p>
  
  <p>I'm keen to hear different opinions on this problem!</p>
  
  <p><strong>EDIT:</strong>
  I would like to state that our model classes are generated by the Entity Framework 4.0 and are POCOs (see example of business objects above). I'm not sure if that was clear enough in the question itself. We would like to expose navigational properties (e.g. <code>Person.Addresses</code>) as <code>ICollection&lt;T&gt;</code>.</p>