2012-07-07 35 views
2

这应该是一个普遍的问题,我已经搜索,但还找不到任何解决方案,如果它是一个骗局,请点我适当的链接。Castle Windsor按公约注册 - 如何注册基于实体的通用知识库?

所以, 我有一个通用的存储库,以支持多种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); 
    } 
} 

注:
库位于位于Entities命名Repositories命名空间
实体,包括所有实体的Entity基类

它工作正常,但我认为它应该是一个更好的方法,更自动的方式使用注册约定,所以当我在我的项目中添加一个新实体时,Windsor会自动识别它,并为其注册存储库。还有一个问题,如何忽略Entity(基类),所以它没有在容器上注册。

关于

回答

7

你的意思是这样的吗?

container.Register(Component.For(typeof(IRepository<>)) 
      .ImplementedBy(typeof(Repository<>)) 
      .LifeStyle.Transient); 
+0

好吧,我被实体装箱.. :)谢谢 – ktutnik 2012-07-07 11:12:37

相关问题