title: How are instances resolved when child containers are used?
body: <p>in the code fragment below, we have a root container and one child container. The child container holds a registration for <code>Person</code> which depends on <code>INameProvider</code>, defined in the root container . <code>INameProvider</code> on its turn depends on <code>IInfoProvider</code> which has a registration in both containers. </p>
  
  <p>When the child container is asked to resolve an instance of <code>Person</code>, the mechanism uses the root container to resolve an instance of <code>INameProvider</code> (as expected), but also to resolve an instance of <code>IInfoProvider</code>. I would expect that the registration for <code>IInfoProvider</code> in the child container would override the one in the root container. </p>
  
  <pre><code>WindsorContainer rootContainer = new WindsorContainer();
  
  // register components for root container
  rootContainer.Register(
     Castle.MicroKernel.Registration.Component.For&lt;IInfoProvider&gt;().
        ImplementedBy&lt;InfoProvider&gt;().
        LifeStyle.Transient);
  rootContainer.Register(
     Castle.MicroKernel.Registration.Component.For&lt;INameProvider&gt;().
        ImplementedBy&lt;BobNameProvider&gt;().
        LifeStyle.Transient);
  
  // create child container
  WindsorContainer childContainer = new WindsorContainer();
  rootContainer.AddChildContainer(childContainer);
  
  // register components for child container
  childContainer.Register(
     Castle.MicroKernel.Registration.Component.For&lt;Person&gt;().
        LifeStyle.Transient);
  childContainer.Register(
     Castle.MicroKernel.Registration.Component.For&lt;INameProvider&gt;().
        ImplementedBy&lt;JimNameProvider&gt;().
        LifeStyle.Transient);
  
  var person = childContainer.Resolve&lt;Person&gt;();
  
  Debug.Assert(person.ToString() == "Jim"); // &lt;- fails, because person.ToString is "Bob"
  </code></pre>
  
  <p>Moreover, if we use the child container to resolve an instance of <code>INameProvider</code> (as in the fragment below) the child container <em>is</em> used to get an instance of <code>IInfoProvider</code>. This is what I expect.</p>
  
  <pre><code>var np = childContainer.Resolve&lt;INameProvider&gt;();
  np.ToString() // &lt;-- equals "Jim"
  </code></pre>
  
  <p>Can anybody explain why the first example uses the root container to resolve an <code>IInfoProvider</code> instead of the child container? </p>
  
  <p>Is there maybe a better pattern to handle various levels of component registration that can be overridden?</p>
  
  <p><strong>Update #1</strong> 
  The actual situation we are dealing with is the following. We are developing a multi-tenant MVC web application where common application functionality is stored in the root container and every tenant has its own child container in which certain things are overridden. For example, the configuration for the datastore is stored in each child container (every tenant has its own database) and some MVC controllers are different. When a controller is created, we lookup the child container using <code>Container.GetChildContainer</code> and try to resolve the objects we need. Our controller has an indirect dependency on the data store configuration, but the one defined in the child container is never used.</p>