I'm dealing with one problem. Imagine this example situation. I have one module e.g. UserModule with injected IUserRepository. IUserRepository can have more implementations e.g. IUserRepositorySql and IUserRepositoryDefault.
public class UserModule : IUserModule
{
private readonly IUserRepository userRepository;
public UserModule(IUserRepository userRepository)
{
if (userRepository == null) throw new ArgumentNullException("userRepository");
this.userRepository = userRepository;
}
}
I would like to define which implementation of IUserRepository will be used in UserModule. I want to avoid Factory pattern that is considered like IoC antipattern in Marc Seeman book and I would like achieve this only with container configuration.
I'm using LightInject and it has something like named services but it can be used only on the top level. I need something like this :
var container = new ServiceContainer();
container.Register<IUserRepository, UserRepositorySql>("Sql");
container.Register<IUserRepository, UserRepositoryDefault>("Default");
container.Register<IUserModule, UserModule>();
var instance = container.GetInstance<IUserModule>("Sql");
This code should return instance of IUserModule with injected UserRepositorySql instance, but of course, it doesn't.
Do you have some soulution for this in LightInject please?
I found a feature annotation in Lightinject where I can inject some properties of an appropriate types, but I don't like this solution so much.
Do you have any experience in some other IoC containers? How do you/which feature does solve this issue?