I've run into a bit of trouble trying to use dependency injection and need help.
I have a service IService that is implemented a few different ways in my application.
ServiceA : IService { public ServiceA(IDependencyA A, IDependency B) {...} } ServiceB : IService { public ServiceB(IDependencyA A, IDependency B) {...} } ServiceC : IService { public ServiceC(IService serviceA, IService serviceB) {...} } In my startup.cs file, I will pick which one to use based on a parameter in my configuration file. Something like this:
var service = Configuration["AppConfig:Service:ID"]; switch(service) { case "A": services.AddTransient<IService, ServiceA>(); case "B": services.AddTransient<IService, ServiceB>(); case "C": // ?? } I am able to create and use service A and service B very easily. I inject their dependencies A and B before hand and they get created just fine. The problem is with the third service. I need to inject the two other services into it. My question is: what is the best to do this?
Is there a way I can create concrete implementations of service A and B but somehow use the injected dependency A and B in their constructor? Do I have to mess around with the interfaces to get this to work? Maybe I have to change the constructor of ServiceC to take in concrete implementations of A & B?
Updated: don't know if it's the best solution, but I ended up doing the following to get it to work.
... case "C": services.AddTransient<ServiceA>(); services.AddTransient<ServiceB>(); services.AddTransient<IService>(s => new ServiceC( s.GetService<ServiceA>(), s.GetService<ServiceB>() ));