title: IoC container - singleton or passed around instance?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2386487/is-it-better-to-create-a-singleton-to-access-unity-container-or-pass-it-through-t">Is it better to create a singleton to access unity container or pass it through the application?</a>  </p>
  </blockquote>
  
  
  
  <p>I am introducing IoC container into the system. The natural question is, should it be a singleton or an instance passed to a class to use? I lean towards having it as a singleton instance because:</p>
  
  <ol>
  <li>No need to clutter class definition like constructors, additional properties.</li>
  <li>Clearer - one instance per application, one initialization routing.</li>
  <li>Ability to have default mapping with overrides if necessary (like for unit testing).</li>
  </ol>
  
  <p>Here is how it looks:</p>
  
  <pre><code>class Main
  {
    public static void main(params string[] args)
    {
       IoCContaner.Intance.Add&lt;IBar&gt;();
       IoCContaner.Intance.Add&lt;IBaz&gt;();
       IoCContaner.Intance.Add&lt;IQux&gt;();
  
       var foo = new Foo();
       Foo.DoBarStuff();
    }
  }
  
  class Bar : IBar 
  { 
    public Bar(IBaz, IQuz) {} 
    public void DoBazStuff() { _baz.DoStuff(); }
  }
  
  class Foo
  {
    public void DoBarStuff()
    {
       var bar = IoCContaner.Intance.Resolve&lt;IBar&gt;();
       bar.DoBazStuff();
    }
  }
  </code></pre>
  
  <p>Is there anything I am missing and instead I should actually have something like:</p>
  
  <pre><code>class Foo
  {
    IoCContainer _c;
    public Foo(IoCContainer c) { _c = c; }
    ...
    private void DoBarStuff()
    {
       var bar = _c.Resolve&lt;IBar&gt;();
       bar.DoBazStuff();
    }
  }
  </code></pre>
  
  <p>Of course with the second approach I may always to fall back to the first one by passing around a singleton container instance. </p>
  
  <p>EDITED: updated code examples</p>