Skip to content
Guy Antoine edited this page Feb 5, 2021 · 1 revision

To get going quickly, see the QuickStart.

Throughout the documentation, we will stick with the example of our CoffeeRobot which depends on IBrewEquipment .

Inititialising the Container

There are two ways to access the IocContainer

//Use the IocContainer Singleton
IocContainer.Register<IBrewEquipment, FrenchPress>();

or

//Create an instance
var container = new IocContainer();
//then use it
container.Register<IBrewEquipment, FrenchPress>();

Child Containers

You can create child containers. This allows you to scope implementations to a specific container. Nesting is possible. The behaviour of child containers is as follows:

  • Registrations that are duplicates of registrations in the parent container are allowed
  • When resolving
    • Child container is searched first
    • If no registration found, ZenIoc will continue searching in the parent container
    • If not found, will continue bubbling up the ancestor tree if there are nested containers. This default can be modified under the Container Options
var container = new IocContainer();

//Initialise a child container
var childcontainer = container.NewChildContainer();

//use it
childcontainer.Register<IBrewEquipment, FrenchPress>();

//Initialise a nested child container
var grandChildContainer = childcontainer.NewChildContainer();

//Will resolve successfully by bubbling up to childcontainer
var brewEquipment = grandChildContainer.Resolve<IBrewEquipment>();

📝 Performance Child containers are equally fast. However if ZenIoc has to bubble up to a parent container, there is a small loss in performance.

The relatives of a given container can be accessed with GetParent() and GetChildren() methods:

//Access the parent IocContainer if needed
var copyOfParent = childcontainer.GetParent();

//Access a list of Children
var children = container.GetChildren();

Configure Settings

The container options are configured when the container is initialised. They are local to each instance of the container. If you instaniate a IocContainer your custom ContainerOptions can be passed in the constructor. If you are using the singleton IocContainer, you will have to run IocContainer.Initialize().

Although you can pass a new ContainerOptions class in the constructor, it is more convenient to set the options using an Action as follows:

new IocContainer(options => options.TryResolveUnregistered = false);

The following settings are configurable. Hit the links to get more details on the functionality associated with each:

  • TryResolveUnregistered=true: Smart Resolve function, will try resolve the given type even if it hasn't been registered
  • ResolveShouldBubbleUpContainers=true: When using child containers. If the type is not registered in the child container, ZenIoc will look for it in the parent. It will bubble up to the ultimate ancestor.

Named Containers

Each container has an optional Name property. This can be set during construction, or afterward. The container can be retrieved from anywhere by using the GetContainer() method. This might be useful if you have several child containers.

var container = new IocContainer("NamedContainer");

//retrieve with 
var containerReference = anothercontainer.GetContainer("NamedContainer");
//or
var containerReference = IocContainer.GetContainer("NamedContainer");