5 Advanced Usage of Lightweight IOC Container
Simon Gockner edited this page 6 years ago

This is an advanced usage guide of the Lightweight IOC Container.

Bootstrapping container

The best way to instantiate the IocContainer is to create a Bootstrapper class. This Bootstrapper is there to instantiate the IocContainer as well as installing all the IIocInstallers.

Example Bootstrapper:

public class Bootstrapper
{
    public IIocContainer InstantiateContainer()
    {
        IIocContainer kernel = new IocContainer();

        return kernel.Install(new Installer());
    }
}

Register types with IIocInstallers

The easiest way to register classes with the IocContainer is to use IIocInstallers and install them in the Bootstrapper.

Example IIocInstaller:

public class Installer : IIocInstaller
{
    public void Install(IIocContainer container)
    {
        container.Register<IFoo, Foo>();
    }
}

Use abstract factories to resolve instances

Instead of using the Resolve<>() method to resolve instances from the IocContainer and thus needing an IocContainer-object, it is recommended to use abstract factories to resolve instances.

Example abstract factory:

public interface IFooFactory
{
    IFoo Create();
}

These factories are only created as interfaces and will be implemented by the LightweightIocContainer.