StructureMap Generic Class Registration with Constructor
I want to register my generic class with its constructor. Like this:
ObjectFactory.Initialize(
action =>
{
action.SelectConstructor<IRepository<>>(() => new Repository<>(LifetimeManage开发者_如何学编程r.Current));
});
But I don't how can I do this?
Note: LifetimeManager.Current is static propery. If I don't care about constructor, I can write this, action.For(typeof(IRepository<>)).Use(typeof(Repository<>));
But today I need to give Repository class UnitOfWork parameter via DI.
Thanks.
I think that this would work for you:
action => action.For<IUnitOfWork>().Use(() => LifetimeManager.Current));
action => action.For(typeof (IRepository<>)).Use(typeof (Repository<>)));
This is provided that the Repository constructor has a parameter of IUnitOfWork. Just make sure that all parameters on the Repository class' constructor are registered in the container and you should be good to go.
If you don't have multiple constructors on the Repository class and you want to choose a specific one there should be no need to use the SelectConstructor-method.
精彩评论