我的任務是注入特定的非直接子依賴項,如果頂級依賴項是某種類型的。我已經嘗試使用子容器來實現這一點,但它不像我預期的那樣工作。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有一個錯誤。那麼它會得到解決嗎?我可以採取什麼其他策略來描述所描述的場景?