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 test scenario creates a new container and using the new method adds it as a child container. Afterwards, we test that Castle Windsor will resolve the type from the newly added container.
// Create new Windsor container var childContainer = new WindsorContainer(); // Register a custom implementation for the component childContainer.Register( Component.For<ITestInterface>() .ImplementedBy<ChildContainerImplementation>() ); // Add new container as child container ObjectFactory.Instance.AddChildContainer(childContainer); // Assert that component from child container is resolved service = ObjectFactory.Instance.Resolve<ITestInterface>(); Assert.AreSame("I am called from the child container!", service.CallMe());
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.