5 IIocInstaller
Simon Gockner edited this page 6 years ago

The IIocInstaller interface is the base class for all installers that are used by the IocContainer.

Base interface

The base interface only consists of one method, the Install() method:

void Install(IIocContainer container);

The Install() method installs the given IRegistrationBases in the given IIocContainer.

To create your own installer, you have to implement the IIocInstaller interface and the Install() method. Inside the Install() method you have to register the IRegistrationBases with the given IIocContainer:

container.Register<IFoo, Foo>();

The following is an example IIocInstaller:

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

AssemblyInstaller

The AssemblyInstaller is an implementation of the IIocInstaller. It installs all IIocInstallers for a given Assembly.
To do so, it checks each Type in the Assembly it is given if it is derived from IIocInstaller:

public AssemblyInstaller(Assembly assembly)
{
    Installers = new List<IIocInstaller>();

    Type[] types = assembly.GetTypes();
    foreach (Type type in types)
    {
        if (!typeof(IIocInstaller).IsAssignableFrom(type))
            continue;

        Installers.Add((IIocInstaller) Activator.CreateInstance(type));;
    }
}

This list of IIocInstallers is then installed when the AssemblyInstallers Install() method is called:

public void Install(IIocContainer container)
{
    foreach (IIocInstaller installer in Installers)
    {
        installer.Install(container);
    }
}

With the help of the FromAssembly class, AssemblyInstallers can be created and provided with their needed Assembly.