2016-03-10 99 views
1

如果我註冊同一合同的兩個實現,則使用DryIoc - 如何控制在使用構造函數注入時要使用哪個實現?DryIoc - 使用構造函數注入時指定依賴關係

我看你,你可以用鑰匙或元數據登記 - 這可能(使用屬性?)注入與實施控制?或者我是否需要一個集合並找出ctor中的正確實現?

回答

1

您可以指定依賴於構造通過Made.Of強類型規格的消費,像這樣:

container.Register<SomeClient>(Made.Of(
    () => new SomeClient(Arg.Of<IDep>("service key of impl"))); 

這裏是related SO answer有更多的選擇。

歸因登記經由MEF Attributed Model支持:

[Export] 
public class SomeClient { 
    public SomeClient([Import("x")]IDep dep) {} 
} 

[Export("x", typeof(IDep))] 
public class X : IDep {} 

[Export("y", typeof(IDep))] 
public class Y : IDep {} 

// in composition root: 
using DryIoc.MefAttributedModel; 

container = new Container().WithMefAttributedModel(); 

container.RegisterExports(
    typeof(SomeClient), 
    typeof(X), 
    typeof(Y)); 

container.Resolve<SomeClient>(); // will inject X 
相關問題