2

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
  • Create an IInterfaceCollection with an implementation that gathers all you IInterface implementations and inject that Commented Jun 28, 2020 at 18:17

1 Answer 1

4

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

class Foo
{
    public Foo(IEnumerable<IInterface> implementers)
    {
        // ...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.