这应该是一个普遍的问题,我已经搜索过但还没有找到任何解决方案,如果是骗子,请指出我适当的链接。
所以我有一个通用存储库来支持多个 entites,有一会儿我像 bellow 一样注册它:
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
基类
工作正常,但我认为这应该是一种更好的方法,更自动的方式使用按惯例注册,因此当我在我的项目上添加新实体时,温莎将自动识别它,并为它注册存储库。 还有一个问题,如何忽略Entity
(基类),使其不在容器上注册。
问候
你的意思是这样?
container.Register(Component.For(typeof(IRepository<>))
.ImplementedBy(typeof(Repository<>))
.LifeStyle.Transient);
请尝试此代码
var container = new WindsorContainer();
container.Register(Component.For(typeof(ICustomGenericRepository<>))
.ImplementedBy(typeof(CustomGenericRepository<>))
.LifeStyle.Transient);