title: Ioc/DI - How to use the registered dependencies?
body: <p>I think I'm missing a key part of how to actually use IoC/DI. I happen to be using the Unity container. I know how to setup a class to have it's dependencies injected and I also know how to make Unity register a type.</p>
  
  <p>But what I don't know is how to actually then make use of these registrations.</p>
  
  <p>So for example:</p>
  
  <pre><code>var container = new UnityContainer();
  container.RegisterType&lt;IRepository, XmlRepository&gt;();
  
  var service = new MyService(container.Resolve&lt;IRepository&gt;());
  
  public interface IRepository
  {
      void GetStuff();
  }
  
  public class XmlRepository : IRepository
  {
      public void GetStuff()
      {
          throw new NotImplementedException();
      }
  }
  
  public class MyService
  {
      private readonly IRepository _myRepository;
      public MyService(IRepository repository)
      {
          _myRepository = repository;
      }
  }
  </code></pre>
  
  <p>Here I have a service layer which accepts a parameter of type <code>IRepository</code>. It's the <em>container</em> part that I seem to not be understanding.</p>
  
  <ul>
  <li>Isn't the point of IoC/DI to be able to not have to manually resolve types every time I need to create an instance? In my code I'm getting the container to resolve a type, unless I'm misunderstanding how this is supposed to work, isn't the container supposed to somehow automatically (with reflection?) inject the dependency I told it to use when I registered the type? <code>var service = new MyService(...)</code> Is calling <code>container.Resolve</code> the correct way of doing this?</li>
  <li>I've created a container, but how do I share this container amongst my project/code? This sort of relates to the first question. I had previously thought that there was a single place that you register types. So far the only way I can see how to get around this is to:
  
  <ul>
  <li>Keep registering types wherever I use them and end up with duplicated code</li>
  <li>Pass the container around to every place I'm going to be resolving types</li>
  <li>Neither of these ways are how I'd expect this to work</li>
  </ul></li>
  </ul>