title: Where should the crud logic be implemented in mvvm?
body: <p>In my MVVM Light application I do a search in a customer list. The search narrows the customer list which are displayed in a master/detail view with a datagrid (the master CustomerSearchResultView) and a separately defined usercontrol with FirstName, Lastname, Address etc, etc (the detail - CustomerSearchDetailView). Here are the main content of the master/detail view:</p>
  
  <pre><code> &lt;StackPanel MinWidth="150" &gt;
          &lt;TextBlock Text="Customer Search Result List" /&gt;
               &lt;Grid&gt;
                  &lt;DataGrid Name="CustomerList" ItemsSource="{Binding SearchResult}" SelectedItem="{Binding SelectedRow, Mode=TwoWay}" &gt;
                        .....
                  &lt;/DataGrid&gt;
              &lt;/Grid&gt;
              &lt;Grid Grid.Column="2"&gt;
                  &lt;TextBlock Text="Customer Details" Style="{StaticResource Heading2}" Margin="30,-23,0,0"/&gt;
                  &lt;content:CustomerSearchDetail DataContext="{Binding SelectedRow}" /&gt;
              &lt;/Grid&gt;
          &lt;/Grid&gt;
      &lt;/StackPanel&gt;
  </code></pre>
  
  <p>Both have their corresponding ViewModels. Please remark the DC for the CustomerSearchDetail, SelectedRow - it is a property on the CustomerSearchResultViewModel and is defined like this:</p>
  
  <pre><code>private Customer _selectedRow;
  ...
  public Customer SelectedRow
          {
              get { return _selectedRow; }
              set
              {
                  _selectedRow = value;
                  RaisePropertyChanged("SelectedRow");
              }
          }
  ...
  </code></pre>
  
  <p><strong>Because of this I have not defined any DC on the CustomerSearchDetailView - it is set in the Binding on the "Master" view (as shown above) and it seems to work ok.</strong></p>
  
  <p>In my Model folder I have created the Customer class that is in use here. It implements ObservableObject and IDataErrorInfo and have public properties that raisepropertychanged events.</p>
  
  <p>I run the application and everything seems to be ok. Note: the ViewModel for the CustomerSearchDetailView (that is CustomerSearchDetailViewModel.cs) is at this stage just an empty shell and not in use (as far as I can see ... the constructor is never accessed)</p>
  
  <p>Now I want to add Save/Update functionality to my customer in the detail view. Ok, I add a Save button to the CustomerSearchDetailView like this:</p>
  
  <pre><code>&lt;Button Content="Save" Command="{Binding Path = SaveCommand}" Width="80" Margin="0,0,15,0"/&gt;
  </code></pre>
  
  <p>I create my "SaveCommand" RelayCommand property in my CustomerSearchDetailViewModel - but it is never accessed.</p>
  
  <p>Hmmmmm ... well after some googling back and forth I come up with this:</p>
  
  <pre><code> &lt;Button Content="Save" Command="{Binding Source={StaticResource MyCustDetails}, Path = SaveCommand}" Width="80" Margin="0,0,15,0"/&gt;
  </code></pre>
  
  <p>I defined the "MyCustDetails" as a resource in this view pointing to the CustomerSearchDetailViewModel. And voila! I now hit the method when debugging ... but alas, my customer was of course "null". (In fact I spent 2 hours implementing the CommandParameter here and binding it to the "SelectedRow" Property on the master view - but the customer was still "null").</p>
  
  <p>More googling and searching for mvvm examples, and I implemented my "SaveCommand" on the Customer class (the model object). And guess what? The edited customer got passed along - I could send it to my EF layer and everything seems to be ok ....</p>
  
  <p>And - If you are still with me - here comes my questions:</p>
  
  <p><em>1.) I would like - and thought that was the "proper MVVM way" of doing things - to have my CRUD/Repository accessing in the ViewModel. How can I do that in my scenario?</em></p>
  
  <p><em>2.) Now that I have my CRUD in place via the Model class (Customer) - should i bother with question 1? In fact I have deleted the CustomerSearchDetailViewModel and everything runs ok. I feel I have invented the View - Model (MV) framework ... :-P</em></p>
  
  <p>I would very much like feedback on this - and I apologize for this "wall of text".</p>