Inject all implementers of an interface in .NET Core class constructors

Viewed 254

Suppose I have multiple implementations for an interface:

public void ConfigureServices(IServiceCollection services)
{
    // ...
    services.AddSingleton<IInterface, ImplementationA>();
    services.AddSingleton<IInterface, ImplementationB>();
    services.AddSingleton<IInterface, ImplementationC>();
    // ...
}

Is there any way to inject all of them directly in class constructors?

1 Answers

Simply it's possible as follows using IEnumerable generic class:

class Foo
{
    public Foo(IEnumerable<IInterface> implementers)
    {
        // ...
    }
}
Related