title: MVVM with Unity and Unit Testing architectural design
body: <p>I'm building a Visual Studio-like application in WPF and I'm having some problems identifying the best architectural design organization of my components. I plan to use Unity as my dependency-injection container and Visual Studio Unit Testing framework and probably moq for mocking library.</p>
  
  <p>I'll first describe the structure of my solution, then my questions:</p>
  
  <p>I have a <strong>WPF project</strong> that contains:</p>
  
  <ul>
  <li>My Unity container initialization (bootstrapper) on application startup (in App.xaml.cs)</li>
  <li>All my application Views (XAML). </li>
  </ul>
  
  <p>Another project called <strong>ViewModel</strong> this contains:</p>
  
  <ul>
  <li>All my application ViewModels. All my ViewModels inherit from a ViewModelBase which exposes a ILogger property</li>
  </ul>
  
  <p>My initialization logic is as follows:</p>
  
  <ol>
  <li>Application Startup </li>
  <li>Unity container creation and registration of types: MainView and MainViewModel</li>
  <li>Resolve my MainView and show it. </li>
  </ol>
  
  <p><code>var window = Container.Resolve&lt;MainView&gt;();</code></p>
  
  <p><code>window.Show();</code></p>
  
  <p>My MainView constructor receives a MainViewModel object in its constructor:</p>
  
  <pre><code>public MainView(MainViewModel _mvm)
  </code></pre>
  
  <ol>
  <li><p>My MainViewModel has a Child ViewModel for each of its panels:</p>
  
  <pre><code>public ToolboxViewModel ToolboxVM{get; set;}
  public SolutionExplorerViewModel SolutionExplorerVM { get; set; }
  public PropertiesViewModel PropertiesVM { get; set; }
  public MessagesViewModel MessagesVM { get; set; }
  </code></pre></li>
  </ol>
  
  <p>And I'm planning to create a InitializePanels() method that initializes each of the panels.</p>
  
  <p>Now here my questions:
  How can my MainViewModel.InitializePanels() initialize all those panels? given the following options:</p>
  
  <p><strong>Option 1:</strong> Initialize the ViewModels manually:</p>
  
  <pre><code>ToolboxVM = new ToolboxViewModel();
  //Same for the rest of VM...
  </code></pre>
  
  <p><strong>Cons:</strong> </p>
  
  <ul>
  <li>I'm not using the Unity container so my dependencies (e.g. ILogger) are not automatically resolved</li>
  </ul>
  
  <p><strong>Option 2:</strong> Use setter injection by annotating my properties:</p>
  
  <pre><code>[Dependency]
  public ToolboxViewModel ToolboxVM{get; set;}
  //... Same for rest of Panel VM's
  </code></pre>
  
  <p><strong>Cons:</strong> </p>
  
  <ul>
  <li>I've read that Unity Setter dependencies should be avoided since they generate a dependency with Unity in this case</li>
  <li>I've also read that you should avoid using Unity for Unit Tests, so how to make this dependency clear in my Unit Tests? Having many dependent properties could be a nightmare to configure.</li>
  </ul>
  
  <p><strong>Option 3:</strong> Use Unity Constructor injection to pass ALL my Panel ViewModels to the MainViewModel constructor so they are automatically resolved by Unity container:</p>
  
  <pre><code>public MainViewModel(ToolboxViewModel _tbvm, SolutionExploerViewModel _sevm,....)
  </code></pre>
  
  <p><strong>Pros:</strong></p>
  
  <ul>
  <li>The dependency would be evident and clear at time of creation, which could help building my ViewModel UnitTests.</li>
  </ul>
  
  <p><strong>Cons:</strong> </p>
  
  <ul>
  <li>Having so many constructor parameters could get ugly pretty quickly</li>
  </ul>
  
  <p><strong>Option 4:</strong> Registering all my VM types at container buildup. Then passing the UnityContainer instance through constructor injection to my MainViewModel:</p>
  
  <pre><code>public MainViewModel(IUnityContainer _container)
  </code></pre>
  
  <p>That way I could do something like:</p>
  
  <pre><code>        Toolbox = _container.Resolve&lt;ToolboxViewModel&gt;();
          SolutionExplorer = _container.Resolve&lt;SolutionExplorerViewModel&gt;();
          Properties = _container.Resolve&lt;PropertiesViewModel&gt;();
          Messages = _container.Resolve&lt;MessagesViewModel&gt;();
  </code></pre>
  
  <p><strong>Cons:</strong></p>
  
  <ul>
  <li>If I decide NOT to use Unity for my UnitTests, as many people suggest,then I won't be able to resolve and initialize my Panel ViewModels.</li>
  </ul>
  
  <p>Given that lengthy explanation, what is the best approach so that I can take advantage of a Dependency Injection Container and end up with a Unit-Testable solution??</p>
  
  <p>Thanks in advance,</p>