title: Moving from Service Locator to Dependency Injection
body: <p>I prepared sample application based on which I would like to discuss moving to Dependency Injection instead of Service Locator. I am pretty new in DI so please be patient with me. Sample app is written with Simple Injector as DI library. Registrations in Bootstrapper works as expected. I have singleton for IMessageBox interface and new instance ComputationCores each time I need it.</p>
  
  <p>I read some articles about DI and so I know that there should be some Composition root and how it should be all working. But I found just very basic examples without real-word complexity. </p>
  
  <p>Sample code:</p>
  
  <pre><code>public class DependencyResolver
  {
  
      public static Func&lt;Type, object&gt; ResolveMe;
  
      public static T GetInstance&lt;T&gt;() where T : class
      {
          return (T)ResolveMe(typeof (T));
      }
  }
  
  public interface IMessageBox
  {
      void ShowMessage(string message);
  }
  
  public class StandardMessageBox : IMessageBox
  {
      public StandardMessageBox()
      {
          Console.WriteLine("StandardMessageBox constructor called...");
      }
  
      ~StandardMessageBox()
      {
          Console.WriteLine("StandardMessageBox destructor called...");
      }
  
      public void ShowMessage(string message)
      {
          Console.WriteLine(message);
      }
  }
  
  public interface IComputationCoreAlpha
  {
      int RunComputation(int myParam);
  }
  
  public class SyncComputationCoreAlpha : IComputationCoreAlpha
  {
      public SyncComputationCoreAlpha()
      {
          Console.WriteLine("SyncComputationCoreAlpha constructor called...");
      }
  
      ~SyncComputationCoreAlpha()
      {
          Console.WriteLine("SyncComputationCoreAlpha destructor called...");
      }
  
      public int RunComputation(int myParam)
      {
          return myParam * myParam;
      }
  }
  
  public class AsyncComputationCoreAlpha : IComputationCoreAlpha
  {
      public AsyncComputationCoreAlpha()
      {
          Console.WriteLine("AsyncComputationCoreAlpha constructor called...");
      }
  
      ~AsyncComputationCoreAlpha()
      {
          Console.WriteLine("AsyncComputationCoreAlpha destructor called...");
      }
  
  
      public int RunComputation(int myParam)
      {
          return myParam * myParam;
      }
  }
  
  public interface IComputationCoreBeta
  {
      int RunComputation(int myParam);
  }
  
  public class SyncComputationCoreBeta : IComputationCoreBeta
  {
      public SyncComputationCoreBeta()
      {
          Console.WriteLine("SyncComputationCoreBeta constructor called...");
      }
  
      ~SyncComputationCoreBeta()
      {
          Console.WriteLine("SyncComputationCoreBeta destructor called...");
      }
  
  
      public int RunComputation(int myParam)
      {
          return myParam * myParam;
      }
  }
  
  public class AsyncComputationCoreBeta : IComputationCoreBeta
  {
      public AsyncComputationCoreBeta()
      {
          Console.WriteLine("AsyncComputationCoreBeta constructor called...");
      }
  
      ~AsyncComputationCoreBeta()
      {
          Console.WriteLine("AsyncComputationCoreBeta destructor called...");
      }
  
      public int RunComputation(int myParam)
      {
          return myParam * myParam;
      }
  }
  
  public interface IProjectSubPart
  {
      int DoCalculations(int myParam);
  }
  
  public class ProjectSubPart1 : IProjectSubPart
  {
      public int DoCalculations(int myParam)
      {
          var messageBoxService = DependencyResolver.GetInstance&lt;IMessageBox&gt;();
          messageBoxService.ShowMessage("Hardly working 1...");
          var ccA = DependencyResolver.GetInstance&lt;IComputationCoreAlpha&gt;();
          var ccB = DependencyResolver.GetInstance&lt;IComputationCoreAlpha&gt;();
  
          return ccA.RunComputation(myParam) + ccB.RunComputation(myParam + 1);
      }
  }
  
  public class ProjectSubPart2 : IProjectSubPart
  {
      public int DoCalculations(int myParam)
      {
          var messageBoxService = DependencyResolver.GetInstance&lt;IMessageBox&gt;();
          messageBoxService.ShowMessage("Hardly working 2...");
          var ccA = DependencyResolver.GetInstance&lt;IComputationCoreAlpha&gt;();
  
          return ccA.RunComputation(myParam * 3);
      }
  }
  
  public class ProjectSubPartN : IProjectSubPart
  {
      public int DoCalculations(int myParam)
      {
          var messageBoxService = DependencyResolver.GetInstance&lt;IMessageBox&gt;();
          messageBoxService.ShowMessage("Hardly working N...");
          return -3;
      }
  }
  
  public class ManhattanProject
  {
      public void RunProject()
      {
          var messageBoxService = DependencyResolver.GetInstance&lt;IMessageBox&gt;();
          messageBoxService.ShowMessage("Project started...");
          var subPart1 = new ProjectSubPart1();
          var subPart2 = new ProjectSubPart2();
          var subPartN = new ProjectSubPartN();
  
          var result = subPart1.DoCalculations(1) + subPart2.DoCalculations(2) + subPartN.DoCalculations(3);
  
          messageBoxService.ShowMessage(string.Format("Project finished with magic result {0}", result));
      }
  }
  
  public class Sample
  {
      public void Run()
      {
          BootStrapper();
  
          var mp = DependencyResolver.GetInstance&lt;ManhattanProject&gt;();
          mp.RunProject();
  
      }
  
      private void BootStrapper()
      {
          var container = new Container();
          container.RegisterSingle&lt;IMessageBox, StandardMessageBox&gt;();
          container.Register&lt;IComputationCoreAlpha, SyncComputationCoreAlpha&gt;();
          container.Register&lt;IComputationCoreBeta, AsyncComputationCoreBeta&gt;();
  
          DependencyResolver.ResolveMe = container.GetInstance;
      }
  
  }
  </code></pre>
  
  <p>In DI it is allowed to call Container.GetInstance (resolve method) just in Composition root, nowhere else. Majority of dependencies should be injected in constructors.</p>
  
  <p>Q1:
  If I would move to DI I suppose that constructor of ManhattanProject should looks something like this:
  ManhattanProject(IMessageBox mb, IComputationCoreAlpha cca, IComputationCoreBeta ccb). But this would lead to having one instance per mb, cca, ccb. And not everytime new instance of cca, ccb on my demands. </p>
  
  <p>Q1a:
  I suppose this could be solved by some kind of abstract factory for cca, ccb which could give me new instance per request. But then - what is the purpose of BootStrapper?</p>
  
  <p>Q2:
  ManhattanProject can consist from more ProjectSubParts which use different coputationCores - e.g. 42. Thus it is totaly inappropriate to use constructor injection in this way (to provide Computation cores) and some kind of facade should be used. Since facade should have limited count of args in constructor too I would end up with many and many nested facades. I suppose this is wrong.</p>
  
  <p>Q3:
  I am using ProjectSubParts which allows me to do some work. All inherits from IProjectSubPart interace. In case I would want to inject different implementations for different ProjectSubParts how should I do that? Should I create new interface for each ProjectSubpart to allow DI container to resolve which implementation use?</p>
  
  <p>Q4:
  Based on provided sample (and Service Locator pattern) it would be very easy for me to create instance of IComputationCoreAlpha which could internally create
  new and clean inner object by calling DependencyResolver.GetInstance everytime I need. Moreover I would have full control over them and I could call Dispose on them when I would be done with their usage. If in DI concept whole graph
  would be created in CompositionRoot how would be this kind of usage possible?</p>
  
  <p>Thanks</p>