title: MVVM separation of Data Access from ViewModel
body: <p>I am new to WPF and MVVM and so far I have an app that gets some ContactList objects from the DB2 database and displays their information in the UI. Currently I have a ContactListModel class and an InformationViewModel class which I am binding to. My InformationViewModel class is set as the DataContext for my View. The problem is that my InformationViewModel class also contains my database access code i.e db connection and SQL command and I would like to move this to my ContactListModel class so that I have a separate data access layer. Can anyone help me with this? Thanks!</p>
  
  <p><strong>ContactListModel.cs</strong></p>
  
  <pre><code>public class ContactListModel//: INotifyPropertyChanged
  {
      public int ContactListID { get; set; }
      public string ContactListName { get; set; }
      public ObservableCollection&lt;AggregatedLabelModel&gt; AggLabels { get; set; }
  }
  </code></pre>
  
  <p><strong>InformationViewModel.cs</strong></p>
  
  <pre><code>public class InformationViewModel
  {
      public InformationViewModel()
      {
          GetData();
      }
  
      private ObservableCollection&lt;ContactListModel&gt; myContactLists;
  
      public IEnumerable&lt;ContactListModel&gt; ContactLists
      {
          get { return myContactLists; }
      }
  
  
      public void GetData()
      {
  
          myContactLists = new ObservableCollection&lt;ContactListModel&gt;();
  
          DB2Connection conn = null;
          try
          {
              conn = new DB2Connection("SERVER CONNECTION;");
          }
          catch (Exception ex)
          {
              System.Windows.MessageBox.Show(ex.Message + " " + ex.InnerException);
          }
  
          //get all contactLists and their labels
          DB2Command command = new DB2Command("SELECT QUERY");
          command.Connection = conn;
  
          conn.Open();
  
          //Add unique contactLists to dictionary
          Dictionary&lt;int, ContactListModel&gt; myContactDictionary = new Dictionary&lt;int, ContactListModel&gt;();
  
          using (DB2DataReader dr = command.ExecuteReader())
          {
              while (dr.Read())
              {
                  int id = Convert.ToInt32(dr["CONTACT_LIST_ID"]);
  
                  if (!myContactDictionary.ContainsKey(id))
                  {
  
                      ContactListModel contactList = new ContactListModel();
  
                      contactList.ContactListID = id;
                      contactList.ContactListName = dr["CONTACT_LIST_NAME"].ToString();
                      contactList.AggLabels = new ObservableCollection&lt;AggregatedLabelModel&gt;()
                  {
                      new AggregatedLabelModel()
                      {
                          ID = Convert.ToInt32(dr["LABEL_ID"]),
                          Name = dr["LABEL_NAME"].ToString()
                      }
  
                  };
                      myContactDictionary.Add(id, contactList);
                  }
                  else
                  {
                      //populate existing contact lists with remaining labels
                      ContactListModel contactList = myContactDictionary[id];
  
                      contactList.AggLabels.Add
                      (
                          new AggregatedLabelModel()
                          {
                              ID = Convert.ToInt32(dr["LABEL_ID"]),
                              Name = dr["LABEL_NAME"].ToString()
                          }
                      );
                  }
              }
          }
          conn.Close();
  }
  </code></pre>