title: Typical IoC container usage - passing data down the line
body: <p>I've recently started using an <a href="http://en.wikipedia.org/wiki/Inversion_of_control" rel="nofollow noreferrer">IoC</a> container for the first time, but I'm not educated on the best practices for using it. More specificaly I'm using <a href="http://msdn.microsoft.com/en-us/library/dd203101.aspx" rel="nofollow noreferrer">Unity</a> in a C# .NET project, and I started using it because it came with <a href="http://compositewpf.codeplex.com/" rel="nofollow noreferrer">Prism</a>.</p>
  
  <p>I use the container to resolve the "top level" objects, and they get the correct objects injected based on the container. However, I can't see the best practice clearly when I have an object with children and children's children, and I need some data from the IoC container all the way down, but not in between. How you'd typically organize the use of IoC container?</p>
  
  <p>Initially I'd think that you'd pass the container everywhere it is needed instead of extracting the needed data from the container on top-level and passing this data on. But then again I get problems when I reach objects which take other specific data in addition to the injected interfaces, and I'd prefer not to inject these through properties or init-methods after resolving the object. </p>
  
  <p>I hope this was clear enough, but let's look at a fictional (and slightly stupid..) example. </p>
  
  <pre><code>class Employee
  {
      private ICommands _commands; 
      priate List&lt;Customer&gt; _customers = new List&lt;Customer&gt;(); 
      public Employee(ICommands commands)
      {
          _commands = commands; 
      }
      public void AddCustomer(string customerName)
      {
          var customer = new Customer(customerName, _commands); 
          _customers.Add(customer); 
      }
  }
  
  class Customer 
  {
      private string _name; 
      private ICommands _commands; 
      priate List&lt;Case&gt; _cases = new List&lt;Case&gt;(); 
      public Customer(string, name, ICommands commands)
      {
          _name = name; 
          _commands = commands; 
      }
      public void AddCase()
      {
          var case = new Case(_commands); 
          _cases.Add(case); 
      }
  }
  
  class Case    {
      private ICommands _commands; 
      public Customer(ICommands commands)
      {
          _commands = commands; 
      }
      public void TriggerCommands()
      {
          _command.TriggerSomething(); 
      }
  }
  </code></pre>
  
  <p>So, this example doesn't really make much sense, but the essence is the same of what I need to do. I have some application commands I pass down the line through my ViewModel classes, because some of them need to be able to trigger commands to display something. I also have common storage, etc. which may be needed for some classes but currently are passed through and stored in middle classes. With only commands it's no big deal if you store commands or container, but would one in a typical IoC-usage pass the IoC container instead, and use this for resolving objects down the line? And what about specific data like the customer name? You can't just pass this in on the Resolve(), so you need to inject that afterwards? </p>
  
  <p>Sorry - this was as short as I was able to make it. Won't require answers of the same length ;-) .. Just; what's the best practice of doing stuff like this with IoC containers?</p>