title: How to sync data in MVVM when using collections
body: <p>Recently I have started to dig into MVVM to structure a WPF application I am working on. I am struggling to understand how I can keep collections in sync between Model and ViewModel, and in conjunction with that, how to validate information the user will enter.</p>
  
  <p>Suppose I have a (theoretical) class Building, the model, that will store a building layout, during runtime in memory, and otherwise in xml via serialization. Building has a member List, and each entry Floor in that list can have other Lists, like List and List, which could again have members which are Lists (ie. List).</p>
  
  <p>The model:</p>
  
  <pre><code>namespace TestMVVM
  {
      public class Building
      {
          public string strName { get; set; }
          public List&lt;Floor&gt; floors { get; set; }
      }
  
      public class Floor
      {
          public int iNumber { get; set; }
          public List&lt;Room&gt; rooms { get; set; }
      }
  
      public class Room
      {
          public int iSize { get; set; }
          public string strName { get; set; }
          public List&lt;Door&gt; doors { get; set; }
      }
  
      public class Door
      {
          public bool bIsLocked { get; set; }
      }
  }
  </code></pre>
  
  <p>In the View, the List of type Floor will be editable in a DataGrid. The user can enter a new row in the DataGrid to add a Floor to the Building class. In another DataGrid, Rooms could be added to a Floor. This is quite easy when I make all Lists into ObservableCollections, and directly couple them with the View. However, this also means there is no proper separation of concerns, and it gets messy once validation comes into play.</p>
  
  <p>So I wrote a ViewModel class, BuildingViewModel. It will hold a reference to an instance of the model. This is where I run into trouble: the ViewModel will hold an ObservableCollection of type FloorViewModel. But when the user adds an entry, how do I also add an entry to the List in the model? And mostly, keep the data in sync? What if a Room is added to a Floor, or a Door to a Room, how to know where in the Model to update which data? Ie. how to sync nested List member data? </p>
  
  <p>Subsequently I would to make sure no duplicate Floors can be created; ie. if the user adds a floor with a number that is already in the List, the DataGrid must report an error. Same if an existing floor is edited, and same for Room names. I would think that kind of error checking cannot happen within the FloorViewModel class, because it has no access to other instances of itself.</p>
  
  <p>I have searched a lot but found no clear answer to this. It would seem like a rather common situation? Maybe I am simply going in the wrong direction with this?</p>
  
  <p>This is the current ViewModel, where ViewModelBase is a generic class holding implementations of INotifyProretyChanged and INotifyDataErrorInfo.</p>
  
  <pre><code>namespace TestMVVM
  {
      public class BuildingViewModel : ViewModelBase
      {
          private Building building;
  
          public string strName
          {
              get { return building.strName; }
              set
              {
                  building.strName = value;
                  if (value == "") AddError("strName", "Name cannot be empty.");
                  OnPropertyChanged("strName");
              }
          }
  
          public ObservableCollection&lt;FloorViewModel&gt; floors
          {
              // what goes here? how to sync members of floor to the model, and validate data?
          }
  
          public BuildingViewModel(Building b)
          {
              building = b;
          }
      }
  
      public class FloorViewModel : ViewModelBase
      {
          public ObservableCollection&lt;Room&gt; rooms
          {
              // what goes here? how to sync members of room to the right Floor of the model, and validate data?
          }
      }
  
      // etc
  }
  </code></pre>