Type Bindings in 3-rd Party Container ModulesΒΆ

Third party module classes can be used to specify bindings.

The module class should be a sub-class or an implementation of a type returned by property Type ModuleType { get; } of IoC.Configuration.DiContainer.IDiManager object used to load the configuration.

To see of how IoC.Configuration.DiContainer.IDiManager type can be specified when loading the configuration, reference Loading from Modules (loading from modules) or Specifying DI Manager (loading from configuration file).

Currently two implementations of IoC.Configuration.DiContainer.IDiManager are available on Nuget:

The module types are passed as parameters, when loaded the configuration from modules (see Loading from Modules), or in iocConfiguration/dependencyInjection/modules/module elements in XML configuration file, if the configuration is loaded from XML file (see Modules).

Note

If the native module has a public method void OnDiContainerReady(IDiContainer diContainer), IoC.Configuration will call this method, when the dependency injection is loaded. The native module can use the IDiContainer object to resolve types in type bindings.

Here is an example of Autofac module:

public class AutofacModule1 : AutofacModule
{
    public IDiContainer DiContainer { get; private set; }

    protected override void Load(ContainerBuilder builder)
    {
        base.Load(builder);

        builder.RegisterType<Interface1_Impl1>()
                .As<IInterface1>()
                .SingleInstance();
    }

    /// <summary>
    ///   The value of parameter <paramref name="diContainer" />
    ///   will be injected by <see cref="DiContainerBuilder" />.
    /// </summary>
    /// <param name="diContainer"></param>
    public void OnDiContainerReady(IDiContainer diContainer)
    {
        DiContainer = diContainer;
    }
}

Here is an example of Ninject module:

public class NinjectModule1 : NinjectModule
{
    public IDiContainer DiContainer { get; private set; }

    public override void Load()
    {
        Bind<IInterface1>().To<Interface1_Impl2>()
                           .InSingletonScope();
    }

    /// <summary>
    ///   The value of parameter <paramref name="diContainer" />
    ///   will be injected by <see cref="DiContainerBuilder" />.
    /// </summary>
    /// <param name="diContainer"></param>
    public void OnDiContainerReady(IDiContainer diContainer)
    {
        DiContainer = diContainer;
    }
}