How to add a child container to the main Windsor container
In version 7.18 of Ucommerce we introduced a new method for our ObjectFactory called AddChildContainer that takes as parameter a WindsorContainer.
This method will add the input WindsorContainer as a child container to the main Ucommerce container.
Castle Windsor will first look up the type in the current container and if it can not be found, it will default back to the parent container. Containers added in this way will be all be registered as a chain, maintaining a similar behavior to how it currently functions with Ucommerce's Apps folder.
The following code creates a new container and using the new method adds it as a child container.
using Castle.MicroKernel.Registration; using Castle.Windsor; using Ucommerce.Catalog; using Ucommerce.Infrastructure; namespace UcommerceCodeSamples.ExtendingUcommerce { public class AddChildContainer { public AddChildContainer() { // Create new Windsor container var childContainer = new WindsorContainer(); // Register a custom implementation for the component childContainer.Register(Component.For<ITaxService>().ImplementedBy<TaxService>()); // Add new container as child container ObjectFactory.Instance.AddChildContainer(childContainer); } } }
This is not only useful for extending Ucommerce and providing an elegant way for Ucommerce Apps to have their own containers, but should also allow for setting up an easier test environment, since you can create a child container and insert your mocks and stubs into it.