Skip to content
This repository has been archived by the owner on Feb 6, 2021. It is now read-only.
Zeebs edited this page Sep 28, 2020 · 10 revisions

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 DiContainer

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

or

//Create an instance
var container = new DiContainer();
//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, SmartDi 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 DiContainer();

//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 SmartDi 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 DiContainer 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 DiContainer your custom ContainerOptions can be passed in the constructor. If you are using the singleton DiContainer, you will have to run DiContainer.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 DiContainer(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, SmartDi 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 DiContainer("NamedContainer");

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