2013-04-21 138 views
0

我寫了基於PRISM MVVM應用。 那時我學習了PRISM,我有一個關於UnityContainer的技術問題。使用依賴注入的具體實例 - MS團結

有什麼辦法,而我用container.Resolve注入具體的實例?

我會嘗試通過例子來解釋。

允許註冊下一類型:

var container = new UnityContainer(); 
container 
    .RegisterType(typeof(ISomeClass), typeof(SomeClass)) 

// with string 
container 
    .RegisterType(typeof(IExample), typeof(Example), "SpecificExampleInstance") 
// without string 
container 
    .RegisterType(typeof(IExample), typeof(Example)); 

SomeClass構造得到IExample作爲輸入參數。

現在我要解決的SomeClass實例,但告訴「容器」注入到SomeClass構造IExample的實例 - 「SpecificExampleInstance」(在其登記在上面的代碼中3行一個),而不是IExample - 無弦(其中registerd在上面的代碼4行一個 - 不字符串)

我hoaping我的問題不夠清楚,如果不是請讓我知道,我會嘗試改變配方。

感謝

回答

0

一種選擇是使用Dependency屬性:

public class SomeClass 
{ 
    public SomeClass([Dependency("SpecificExampleInstance")] IExample myExample) 
    { 
     // work with the service here 
    } 
}