Select version
v7.18 v7.16 v7.13 v7.12 v7.11 v7.9 v7.8 v7.7 v7.6 v7.5 v7.3 v7.1 v7.0 v6.8 v6.7 v6.6 v6 v5 v4 v3 v2

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.

Ucommerce Aps - Vestergade 58N, 4th. floor - 8000 Aarhus C - Denmark
Phone +45 61 799 997 - Email [email protected] Ucommerce logo