2014-10-30 42 views
1

我的任務是注入特定的非直接子依賴項,如果頂級依賴項是某種類型的。我已經嘗試使用子容器來實現這一點,但它不像我預期的那樣工作。WindsorCastle子容器解決父依賴關係

我有下一個類層次結構:

PRMController: ApiController 
    -> PManager: IManager 
     -> Repository: IRepository 
      -> RepositoryConnection: IRepositoryConnection 
        RepositoryConnection() { /*use default config name*/ } 
        RepositoryConnection(string configName) 

溫莎容器旁邊都有登記:

container.Register(Classes.FromAssemblyInThisApplication() 
      Pick() 
      .Configure(c => 
      { 
       c.Named(Guid.NewGuid().ToString()); 
       c.IsFallback(); 
      }) 
      .WithService.DefaultInterfaces() 
      .LifestyleTransient() 
      ); 

var childContainer = new WindsorContainer() 
     .Register(
      Component.For<IRepositoryConnection>().ImplementedBy<RepositoryConnection>() 
       .DependsOn(Dependency.OnValue("configName", "PRM")) 
       .LifestyleTransient() 
      ); 
     container.AddChildContainer(childContainer); 

現在對於PRM控制器應該用 「PRM」 配置名稱。因此,子容器用於解析PRM控制器。但RepositoryConnection已解決使用默認配置。

childContainer.Resolve<IRepositoryConnection>(); //as expected, resolves from child container registrations 
childContainer.Resolve<PRMController>(); //NOT as expected, the underlying repositoryConnection is from parent container 

有人可以請說明一下嗎? 我認爲如果windsor存在,即使從父容器中註冊的類型的子容器內解析,它也會優先使用子容器依賴項。 如果這是不可行的,如果有人能帶領我走向正確的方向,我將不勝感激。

更新: 根據this thread有一個錯誤。那麼它會得到解決嗎?我可以採取什麼其他策略來描述所描述的場景?

回答

1

已經通過IHandleSelector實現了我想要的,因此在HasOpinion中檢查了服務以及當前Url是否爲PRMControllerSelectHandler返回所需的類型IRepositoryConnection

我最好堅持使用子容器,就像在控制器工廠一樣,我確切地知道它是哪個控制器,而使用句柄選擇器時,我需要從Request.Url解析它。

不幸的是,在Windsor 3.2和3.3中,孩子的集裝箱路線似乎不像預期的那樣(至少如我所料)。