2012-07-07 34 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

相關問題