title: How not to use the default constructor using the MVVM UI Pattern?
body: <p>Using <a href="http://en.wikipedia.org/wiki/Dependency_injection" rel="nofollow"><strong>DI</strong></a> in a <a href="https://msdn.microsoft.com/en-us/magazine/dd419663.aspx" rel="nofollow"><strong>WPF</strong></a> application along with the <a href="http://en.wikipedia.org/wiki/Model_View_ViewModel" rel="nofollow"><strong>MVVM</strong></a> UI Architecture Design Pattern.</p>
  
  <p>When setting the <code>Window.DataContext</code> property, the compiler complains:</p>
  
  <blockquote>
    <p><em>The type [("my view model type")] does not include any accessible constructors.</em></p>
  </blockquote>
  
  <p>Which had to be that there was no default constructor set in my view model class.</p>
  
  <p><strong>ProductManagementViewModel</strong></p>
  
  <pre><code>public class ProductManagementViewModel 
      : ViewModel&lt;ObservableCollection&lt;Product&gt;, Product&gt; {
      public ProductManagementViewModel(ObservableCollection&lt;Product&gt; model)
          : base(model) { }
  
      public Product Current { get; set; }
  }
  </code></pre>
  
  <p><strong>ProductManagementView.xaml</strong></p>
  
  <pre><code>&lt;Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:Models="clr-namespace:WMI.Courses.DesignPatterns.Mvvm.Models" 
      xmlns:ViewModel="clr-namespace:MyProject.ProductManagement.Management" 
      mc:Ignorable="d" 
      x:Class="MyProject.ProductManagement.Management.ProductManagementView"      
      ResizeMode="NoResize"
      Title="{Binding ViewTitle}"
      Width="800"&gt;
      &lt;Window.DataContext&gt;
          &lt;ViewModel:ProductManagementViewModel /&gt;
      &lt;/Window.DataContext&gt;
      [...]
  &lt;/Window&gt;
  </code></pre>
  
  <p>Besides, it's best to use <a href="http://blog.ploeh.dk/2011/03/03/InjectionConstructorsshouldbesimple/" rel="nofollow"><strong>Constructor Injection</strong></a>, so since my View Model class depends on an <a href="https://msdn.microsoft.com/en-us/library/ms668604%28v=vs.110%29.aspx" rel="nofollow"><strong>ObservableCollection</strong></a>, it has to accept it through the constructor. And then, the only way found to solve the problem was to have a default constructor within the class.</p>
  
  <p><strong>ProductManagementViewModel</strong></p>
  
  <pre><code>public class ProductManagementViewModel 
      : ViewModel&lt;ObservableCollection&lt;Product&gt;&gt; {
      public ProductManagementViewModel() 
          : this(new ObservableCollection&lt;Product&gt;()) { }
      [...]
  }
  </code></pre>
  
  <p>This makes me feel somehow dirty, and it's like I have no other choice than that.</p>
  
  <p><strong>How not to use the default constructor using the MVVM UI Pattern?</strong></p>