3
我想配置替代构造函数参数,当一个请求的类型被注入到几个类中时。在以前的StructureMap版本中,它看起来很像第一个DSL example on the document page,但我很难弄清楚如何使用新语法来配置它。StructureMap 2.5注册表语法
我现在得到的是一个具有一个具体实现的接口,但我需要构造函数参数根据它被注入的对象进行更改。例如:
interface IInterface{}
class Concrete : IInterface
{
public Concrete(string param) {}
}
class ConsumerOne
{
public ConsumerOne(IInterface i) {} // Concrete(param) to be "One"
}
class ConsumerTwo
{
public ConsumerTwo(IInterface i) {} // Concrete(param) to be "Two"
}
class MyRegistry : Registry
{
public MyRegistry()
{
For<IInterface>()
.Use<Concrete>
.Ctor<string>("param")
.Is(/* "One" if being injected into ConsumerOne,
"Two" if being injected into ConsumerTwo */);
}
}
我想我也许可以用.AddInstance(x => {})
为此,For<IInterface>()
后,但我有麻烦发现如何做到这一点。任何帮助或建议,将不胜感激!
您应该使用添加而不是用于IInterface注册,因为它不是您正在注册的默认设置。通过制定一项公约可以解决潜在的缺点。 – PHeiberg 2010-09-28 11:18:12