1
假設我有兩個接口IFirst和ISecond,它們都有幾個實現,例如, FirstClassForMe,FirstClassForYou,SecondClassForMe,SecondClassForYou。Castle Windsor 3.0具有相同名稱的不同服務 - 如何?
我想在IoC容器來註冊(無論哪個生活方式):
this.container.Register(
Component.For<IFirst>().ImplementedBy<FirstClassForMe>().Named("Me"),
Component.For<IFirst>().ImplementedBy<FirstClassForYou().Named("You"),
Component.For<ISecond>().ImplementedBy<SecondClassForMe>).Named("Me"),
Component.For<ISecond>().ImplementedBy<SecondClassForYou>().Named("You"))
我想用這個如下:
var forWho = this.GetCode();
var first = this.Container.Resolve<IFirst>(forWho);
var second = this.Container.Resolve<ISecond>(forWho);
this.Work(first, second);
但在溫莎,我得到一個ComponentRegistrationException
組件Me無法註冊。已經有一個名稱的組件。你想修改現有的組件嗎?如果不是,請確保指定一個唯一的名稱。
但是,我相信,只要對(接口;代碼)是唯一的,每個註冊都可以解決這種依賴性。根據這種情況是否有可能讓windsor工作?例如。查找所有IFirst註冊,然後按名稱選擇一個。
就我看不出服務覆蓋可以提供幫助而言,您可以詳細闡述一下嗎? –
重新閱讀後,它聽起來像是想根據運行時確定的某個「Value」來解析組件。會像這樣爲你工作:http://stackoverflow.com/questions/8941055/setting-the-name-property-during-registration-of-a-component-using-castle-windso/8954362 – PatrickSteele
我已經發布了一個有問題的答覆你提到。我現在使用的是那裏的描述,以及我在我的答案的最後一句中描述的錯誤。 –