Skip to content
Kevin Bost edited this page Dec 21, 2018 · 3 revisions

Third Party Containers

If you would prefer to use an alternate dependency injection (DI) container, you can easily swap out the built-in AutoDI container with a different one. You can find full examples using many popular DI containers here.

Because AutoDI is built on top of Microsoft.Extensions.DependencyInjection.Abstractions, any popular DI container that already provides support the Microsoft.Extensions.DependencyInjection.Abstractions can easily be added. For this example we will use Microsoft.Extensions.DependencyInjection. To register a different container, you must register an implementation of IServiceProviderFactory<IServiceCollection> with the AutoDI.IApplicationBuilder. The last registered implementation will be used as the DI container.

To do this, we must register as part of the initialization (using a setup method).

using AutoDI;
using Microsoft.Extensions.DependencyInjection;

[SetupMethod]
public static void SetupDI(IApplicationBuilder builder)
{
    builder.ConfigureServices(services => services
            .AddSingleton<IServiceProviderFactory<IServiceCollection>, DefaultServiceProviderFactory>());
}

This example uses DefaultServiceProviderFactory, the Microsoft.Extensions.DependencyInjection implementation, but you can use any popular library that provides an implementation of IServiceProviderFactory<IServiceCollection>. For other DI containers you will need to create a new implementation.