I want to use dependency injection for a class that takes an interface IService in its constructor. But I have several implementations of this interface. Both implementations are needed by other classes as well so I cannot simply only initiate the one needed. I'm using Microsoft.Extensions.DependencyInjection
Here's an example that tries to show my classes
public interface IService { }
public class ServiceA : IService { ... }
public class ServiceB : IService { ... }
public class Foo
{
public Foo(IService service) { }
}
public class Bar
{
public Bar(IService service) { }
}
If I did not have several implementations I'd inject it like following:
builder.Services.AddSingleton<IService, ServiceA>();
builder.Services.AddSingleton<Foo>();
In the example I'd need Foo to be initialized with ServiceA and Bar to be initialized with ServiceB.