2016-03-10 104 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 
相关问题