title: Using the new operator to create objects/dependencies vs. using DI containers
body: <p>What's so bad about manually wiring up dependencies via constructor injection and what's so great about using a DI container (<code>container.Resolve&lt;T&gt;()</code>) as against that?</p>
  
  <p>I mean, apart from the fact that you have to type a few more keystrokes every time you need to create your dependent object and supply it with all its dependencies.</p>
  
  <p>In fact, having a central registry like a DI Container provides can be a heck of a lot more confusing and take away control from you.</p>
  
  <p>I've had this question and another one (below) for some time, but I guess Mark Seemann's article titled <a href="http://blog.ploeh.dk/2010/02/03/ServiceLocatorisanAnti-Pattern/" rel="nofollow">Service Locator is an anti-pattern</a> got me to finally type this question here.</p>
  
  <p>So, my other question is:</p>
  
  <p>What if there's a dependency that itself needs some dynamic (say user supplied) input for its construction? Say, like:</p>
  
  <pre><code>class Dependency : IDependency
  {
    public Dependency(string userInput) { }
  }
  
  class Dependent
  {
      IDependency _dependency;
  
      void DoSomething()
      {
        var container = new MyFavoriteContainerFromTheMarket();
  
        Console.Write("Hey, user. How are you feeling?");
        var userInput = Console.ReadLine();
  
        // Do DI containers have overloads for Resolve instead of for Register
        // that accept parameters to be passed to constructors?
        // In other words, what if I don't know the constructor parameter
        // value at the time of registration of the dependency but only
        // get to know it just before I have to resolve/instantiate the
        // dependency? Do popular DI containers of today have overloads
        // for that? I assume they must
        _dependency = container.Resolve&lt;IDependency&gt;(userInput);
      }
  }
  </code></pre>
  
  <p>Overall, doesn't it feel like DI Containers take away control from you? Wouldn't it just be okay to manually wire up dependencies in some cases?</p>
  
  <p>Sure, I understand sometimes it is easy and saves you typing and creates for a cleaner, more terse code snippet, but having it all mixed up -- some dependencies managed by a container and some you have on your own that you manually supply -- all makes it so confusing and forces you to remember which one is which, and thus makes maintenance more difficult, wouldn't you agree?</p>
  
  <p><strong>Update</strong></p>
  
  <p>Oh, wait. I just realized my whole example was convoluted. I had meant to call the Resolve from client code in the main(). The code I should have originally posted with this question should have been like this:</p>
  
  <pre><code>class Dependency : IDependency
  {
    public Dependency(string userInput) { }
  }
  
  class Dependent
  {
      IDependency _dependency;
  
      public Dependent(IDependency dependency)
      {
        _dependency = dependency;
      }
  
      public void DoSomething()
      {
      }
  }
  
  class Program
  {
    public static void Main(string[] args)
    {
        var container = new MyFavoriteContainerFromTheMarket();
  
        container.Register&lt;IDependency&gt;(new Dependency(/* I want this to come later */));
  
        container.Register&lt;Dependent&gt;(new Dependent(), 
         new ConstructorInjection&lt;IDependency&gt;());
  
        Console.Write("Hey, user. How are you feeling?");
        var userInput = Console.ReadLine();
  
        // It appears I can't do this as it makes perfect sense
        // to have the whole tree of dependencies known at the time
        // of registration.
        var dependent = container.Resolve&lt;Dependent&gt;(userInput);
  
        dependent.DoSomething();
    }
  }
  </code></pre>
  
  <p>I don't know what that update makes of this previous discussion then. It just looks like some idiot (me) not having though out the question in his head before asking. Asking the question itself forced the right answer, which @Maarten confirmed and was trying to help me with.</p>
  
  <p>Sorry, @Maarten. I wasted your time. :-)</p>