I have an interface, for example ISomeService
. ISomeService
provides a common service, but the implementations may vary. As such, they will have different dependencies.
Consider:
interface ISomeService
{
void DoSomething();
}
class SomeServiceA : ISomeService
{
public SomeServiceA(DependencyOne one, DependencyTwo two)
{
}
public void DoSomething()
{
}
}
class SomeServiceB : ISomeService
{
public SomeServiceB(DependencyThree three)
{
}
public void DoSomething()
{
}
}
Now, the reality is I can't just add a dependency to ISomeService to my controller and be done with it. I will need to choose an implementation of IService based on what a user tells me, and I may have to create multiple copies of the service they selected.
Perhaps I'd have a class that allows me to resolve an instance of the ISomeService I need, by type:
class SomeServiceProvider
{
public T Get<T>()
where T : ISomeService
{
// uh oh, this is starting to have a bad smell... do I call the container? that's no good
}
}
So I could
class SomeController
{
SomeServiceProvider ServiceProvider { get; set; }
public SomeController(ServiceProvider provider)
{ ServiceProvider = provider; }
public void SomeAction(string serviceName)
{
ISomeService someService;
if (serviceName.Equals('SomeServiceA')) someService = ServiceProvider.Get<SomeServiceA>();
else someService = ServiceProvider.Get<SomeServiceB>();
someService.DoSomething();
}
}
It seems that Autofac's delegate factory would not be appropriate here, and I'm not sure what to do otherwise (except use a service locator). Any recommendations?