title: Replacing ServiceLocator with DependencyInjection when implementing modal dialogs in MVVM WPF application
body: <p>I am writing my first WPF application and I would like to ask you for help with a problem that I encountered. </p>
  
  <p>I am trying to follow the MVVM pattern and I came to a point where I need to implement modal dialogs. I googled/read on the topic for some time and I was able to settle on a solution. However, when refactoring I encountered a dilemma that concerns using a DI (constructor injection) as a replacement of a service locator.</p>
  
  <p>I am going to be referencing these: <a href="http://pastebin.com/S6xNjtWW" rel="nofollow">http://pastebin.com/S6xNjtWW</a>.</p>
  
  <p>I really like the approach of Roboblob:</p>
  
  <p><em>First:</em> He creates an abstraction of a modal dialog (interface). I named the interface IModalDialog and this is how it looks like:</p>
  
  <pre><code>public interface IModalDialog
  {
      bool? DialogResult { get; set; }
      object DataContext { get; set; }
  
      void Show();
      bool? ShowDialog();
      void Close();        
  
      event EventHandler Closed;
  }
  </code></pre>
  
  <p><em>Second:</em> An abstraction of modal dialog service:</p>
  
  <pre><code>public interface IModalDialogService
  {       
      void ShowDialog&lt;TDialogViewModel&gt;(IModalDialog view, TDialogViewModel viewModel, Action&lt;TDialogViewModel&gt; onDialogClose) where TDialogViewModel : class;        
      void ShowDialog&lt;TDialogViewModel&gt;(IModalDialog view, TDialogViewModel viewModel) where TDialogViewModel : class;        
  }
  </code></pre>
  
  <p><em>Third:</em> The concrete implementation of IModalDialogService:</p>
  
  <pre><code>    public class ModalDialogService : IModalDialogService
      {
      public void ShowDialog&lt;TDialogViewModel&gt;(IModalDialog view, TDialogViewModel viewModel, Action&lt;TDialogViewModel&gt; onDialogClose) where TDialogViewModel : class
      {
          // set datacontext
          if (viewModel != null)
          {
              view.DataContext = viewModel;
          }
          ((System.Windows.Window)view).Owner = System.Windows.Application.Current.MainWindow;
          // register 
          if (onDialogClose != null)
          {
              view.Closed += (sender, e) =&gt; onDialogClose(viewModel);
          }
          view.ShowDialog();
      }
  
  
      public void ShowDialog&lt;TDialogViewModel&gt;(IModalDialog view, TDialogViewModel viewModel) where TDialogViewModel : class
      {
          this.ShowDialog(view, viewModel, null);
      }
  </code></pre>
  
  <p><em>Fourth:</em> There are more implementations of IModalDialog. Each is a Window-derived class that implements IModalDialog.</p>
  
  <hr>
  
  <p>Before I ask the question (describe the problem), I need to explain this beforehand:</p>
  
  <p>Let's say that I have some more services, like for example IMessageBoxService.
  Then I need to declare these dependencies in the constructor of MainWindowViewModel:</p>
  
  <pre><code>public MainWindowViewModel(IModalDialogService a,                               
                             IMessageBoxService b,
                             ...)
  </code></pre>
  
  <p>so that I can inject them (either by hand or using IOC container like Unity, etc.).</p>
  
  <p>In order to be able to use the modal dialog service there is one missing piece of puzzle - the ability to resolve a concrete implementation of IModalDialog based on some key.</p>
  
  <p>Roboblob in his article solves this last piece of puzzle using a ServiceLocator pattern:</p>
  
  <pre><code>public class Bootstrapper
  {
  public static void InitializeIoc()
    {
      SimpleServiceLocator.SetServiceLocatorProvider(new UnityServiceLocator());
      SimpleServiceLocator.Instance.Register&lt;IModalDialogService, ModalDialogService&gt;();
      SimpleServiceLocator.Instance.Register&lt;IMessageBoxService, MessageBoxService&gt;();
    ...    
      SimpleServiceLocator.Instance.Register&lt;IModalWindow, EditUserModalDialogView&gt;(Constants.EditUserModalDialog);
    }
  </code></pre>
  
  <p>}</p>
  
  <p>so he inside his MainWindowViewModel simply calls the static classes Get and resolves a concrete implementation of IModalDialog window based on a key.</p>
  
  <p>Even Josh Smith uses a similar approach in his article but in comments he says that (DI - constructor injection) is a valid option.</p>
  
  <p>The referenced StackOverflow answer also describes a similar WindowViewLoaderService that could be modified and use.</p>
  
  <hr>
  
  <p>So the question is - what would be the best way to replace the ServiceLocator (which resolves the concrete implementations of IModalDialog) with a dependency injection?</p>
  
  <p>My train of thoughts was:</p>
  
  <ol>
  <li><p>One possibility is (due to the project not being very big/developed by me only) to just create a new service (e.g. called IModalDialogResolver) that would create and return new instances of concrete implementations of IModalDialog. Have all the services injected by hand.</p></li>
  <li><p>Then I thought about an IOC container (Unity). I have no experience with it. I thought that maybe I don't have to write the IModalDialogResolver as I could register the different implementations of IModalDialog with Unity container => but then how do I use the container inside MainWindowViewModel? I cannot pass the reference to the constructor as that would be a step back to ServiceLocation.<br />
  So then I thought that maybe I can use one unity container in the bootstrapper to resolve all the services and use another one internally inside the IModalDialogResolver. But I don't know whether this is a good idea regarding the recommended usage of Unity. I really know too little to judge this. But something tells me that this is not a good idea as it creates a hidden dependency on the container + if the container is a singleton that would be equivalent to just passing the reference into the constructor.</p></li>
  </ol>
  
  <p>To maybe better explain the mental block that I have: I would like to use an IOC container (e.g. Unity) to have the interfaces constructed and injected by it. But then I cannot just put IModalDialog as a parameter inside a constructor. So probably I really need to wrap this inside a service and implement myself - but then (provided that Unity can do this out of the box) it doesn't make sense to have Unity there in the first place if i cannot use it.</p>
  
  <p>I know one of the alternatives is to put this one service into a base class, but for the sake of argument, let's not consider this. I really would like to learn about the right way to have this solved using dependency injection.</p>