title: Resolving windows in Structure Map or how to manage multiple windows in WPF MVVM?
body: <p>I have been reading Mark Seeman's book on dependency injection in .NET and I'm struggling to configure composition root in WPF application.</p>
  
  <p>My container will be registered in the application startup method:</p>
  
  <pre><code>protected override void OnStartup(StartupEventArgs e)
  {
      base.OnStartup(e);
  
      var container = new Container();
      container.Configure(r =&gt;
                          {
                              r.For&lt;IAccountServices&gt;().Use&lt;AccountServicesProxy&gt;();
                              r.For&lt;MainWindow&gt;().Use&lt;MainWindow&gt;();
                          });
  }
  </code></pre>
  
  <p>This makes sense as the application startup represents my composition root. </p>
  
  <p>WPF windows in my application are based on view models. View models use constructor injection. E.g. I may compose a view model by injecting implementation of <code>IAccountServices</code>.</p>
  
  <p>When it comes to creating my main window, I can do the following inside of the OnStartup method:</p>
  
  <pre><code>var mainWindow = container.GetInstance&lt;MainWindow&gt;();
  mainWindow.Show();
  </code></pre>
  
  <p>Once I'm inside of the main window, I might want open up another window. So far I've been able to come up with one way of doing this, which is to create a window factory and ask window factory to resolve instance of the window. I'll have to make sure that window factory is available in every view model that might need to open a new window. In my mind this is as bad as passing IoC container around my application (service locator anti-pattern comes to mind). </p>
  
  <p>Does this approach seem right to you? My gut feeling tells me that this is wrong, but I haven't come up with a better way of achieving this (yet).</p>