title: How to save data from a DetailView bound to a ViewModel if the repository is a no-go in a viewmodel?
body: <p>we mvvm lovers all know Josh Smith mvvm sample and how he has saved the customer in the detail customer view by injecting the repository object into the customerViewModel`s constructor.</p>
  
  <p>But a viewmodel should not know about repositories. Its just a model of a view nothing must being aware of persistence etc...</p>
  
  <p>How can I register my Action delegate SaveDocumentDelegate on the DocumentViewModel if its set in the code-behind? Actually I should subscribe the delegate in my DocumentController but how can I instantiate the DocumentView in my DocumentController and set it as Datacontext not doing that in code-behind. Only thing that came to my mind is using a contentcontrol in the window and bind it to the type of the viewModel and datatemplate it with the Document UserControl like this:</p>
  
  <pre><code>&lt;UserControl.Resources&gt;
  
          &lt;DataTemplate DataType="{x:Type ViewModel:DocumentViewModel}"&gt;
              &lt;View:DocumentDetailView/&gt;
          &lt;/DataTemplate&gt;
  
      &lt;/UserControl.Resources&gt;
  
  &lt;ContentControl Content="{Binding MyDocumentViewModel}" /&gt;
  </code></pre>
  
  <p>But I do not want to use a control to solve my architectural problems...</p>
  
  <p><strong>xaml</strong>:(view first approach)</p>
  
  <pre><code>public partial class DocumentDetailView : UserControl
      {
          public DocumentDetailView()
          {
              InitializeComponent();
  
              this.DataContext = new DocumentViewModel(new Document());
          }
      }
  </code></pre>
  
  <p><strong>DocumentViewModel:</strong></p>
  
  <pre><code> public class DocumentViewModel : ViewModelBase
      {
          private Document _document;
          private RelayCommand _saveDocumentCommand;
          private Action&lt;Document&gt; SaveDocumentDelegate;
  
          public DocumentViewModel(Document document)
          {
              _document = document;
          }
  
          public RelayCommand SaveDocumentCommand
          {
              get { return _saveDocumentCommand ?? (_saveDocumentCommand = new RelayCommand(() =&gt; SaveDocument())); }
          }
  
          private void SaveDocument()
          {
              SaveDocumentDelegate(_document);
          }        
  
          public int Id
          {
              get { return _document.Id; }
              set
              {
                  if (_document.Id == value)
                      return;
  
                  _document.Id = value;
                  this.RaisePropertyChanged("Id");
              }
          }
  
          public string Name
          {
              get { return _document.Name; }
              set
              {
                  if (_document.Name == value)
                      return;
  
                  _document.Name = value;
                  this.RaisePropertyChanged("Name");
              }
          }
  
          public string Tags
          {
              get { return _document.Tags; }
              set
              {
                  if (_document.Tags == value)
                      return;
  
                  _document.Tags = value;
                  this.RaisePropertyChanged("Tags");
              }
          }
      }
  </code></pre>
  
  <p><strong>UPDATE</strong>:</p>
  
  <pre><code>public class DocumentController
      {
          public DocumentController()
          {    
              var win2 = new Window2();
              var doc =  new DocumentViewModel(new DocumentPage());
              doc.AddDocumentDelegate += new Action&lt;Document&gt;(OnAddDocument);
              win2.DataContext = doc;
              wind2.ShowDialog();
          }
  
          private void OnAddDocument(Document doc)
          {
              _repository.AddDocument(doc);
          }
      }
  </code></pre>
  
  <p>What do you think about that idea?</p>