这应该是一个普遍的问题,我已经搜索过但找不到任何解决方案,如果它是一个骗局,请指出适当的链接。
所以, 我有一个通用的存储库来支持几个entites,我暂时注册它如下:
public class ExpensiveServiceInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
Register<EntityA>(container);
Register<EntityB>(container);
//etc etc
//when there is a new one should register it manually
}
void Register<T>(IWindsorContainer container) where T : Entity
{
container.Register(Component.For<IRepository<T>>()
.ImplementedBy<Repository<T>>()
.LifeStyle.Transient);
}
}
注意:
存储库位于Repositories
命名空间上
位于Entities
命名空间的实体,包括所有实体的Entity
基类
它工作正常,但我认为它应该是更好的方式,按惯例使用注册的更自动的方式,所以当我在我的项目上添加一个新实体时,windsor将自动识别它,并为它注册存储库。还有一个问题,如何忽略Entity
(基类),以便它没有在容器上注册。
问候
答案 0 :(得分:7)
你的意思是这样的吗?
container.Register(Component.For(typeof(IRepository<>))
.ImplementedBy(typeof(Repository<>))
.LifeStyle.Transient);
答案 1 :(得分:0)
请尝试使用此代码
var container = new WindsorContainer();
container.Register(Component.For(typeof(ICustomGenericRepository<>))
.ImplementedBy(typeof(CustomGenericRepository<>))
.LifeStyle.Transient);