如何使用 StructureMap 来创建具有原始构造函数参数的对象的 setter 依赖项?
我有一个实现“ITestProperty”的对象“TestProperty”。 “TestProperty”采用字符串构造函数参数。这是在 StructureMap 中使用 CtorDependency 或 WithCtorArg 等内容进行配置的。
我想将“ITestProperty”的实例(用“TestProperty”实现)注入另一个类作为属性。当我尝试运行代码时,出现异常(StructureMap 错误代码 205,“缺少请求的实例属性”)。
这是重现问题的简化版本:
测试:
[Test]
public void Can_resolve_the_correct_property()
{
ObjectFactory.Initialize( x => x.AddRegistry( new TestRegistry() ) );
var instance = ObjectFactory.GetInstance<TestController>();
}
注册表设置:
public class TestRegistry : Registry
{
public TestRegistry()
{
ForRequestedType<ITestProperty>().AddInstances(
i => i.OfConcreteType<TestProperty>().WithName( "Test" )
.CtorDependency<string>( "arg" ).Is( "abc" )
);
//ForConcreteType<TestProperty>().Configure
.CtorDependency<string>( "arg" ).Is( "abc" );
ForConcreteType<TestController>().Configure
.SetterDependency( p => p.Property ).Is<TestProperty>()
.WithName( "Test" );
}
}
测试对象:
public interface ITestProperty { }
public class TestProperty : ITestProperty
{
private readonly string arg;
public TestProperty( string arg )
{
this.arg = arg;
}
public string Arg { get { return arg; } }
}
public class TestController
{
public ITestProperty Property { get; set; }
}
当我们去初始化上面的“TestController”对象时,会引发异常。是否可以使用 StructureMap 来做到这一点?假设这是可能的,我需要做什么才能让它发挥作用?
提前致谢。
I have an object "TestProperty" which implements "ITestProperty". "TestProperty" takes a string constructor argument. This is configured in StructureMap using something along the lines CtorDependency or WithCtorArg.
I want to inject the an instance of "ITestProperty" (implemented with "TestProperty") into another class as a property. When I try to run the code I get an exception (StructureMap Error Code 205, "Missing requested Instance property").
Here's a simplified version that recreates the problem:
Test:
[Test]
public void Can_resolve_the_correct_property()
{
ObjectFactory.Initialize( x => x.AddRegistry( new TestRegistry() ) );
var instance = ObjectFactory.GetInstance<TestController>();
}
Registry Setup:
public class TestRegistry : Registry
{
public TestRegistry()
{
ForRequestedType<ITestProperty>().AddInstances(
i => i.OfConcreteType<TestProperty>().WithName( "Test" )
.CtorDependency<string>( "arg" ).Is( "abc" )
);
//ForConcreteType<TestProperty>().Configure
.CtorDependency<string>( "arg" ).Is( "abc" );
ForConcreteType<TestController>().Configure
.SetterDependency( p => p.Property ).Is<TestProperty>()
.WithName( "Test" );
}
}
Test Objects:
public interface ITestProperty { }
public class TestProperty : ITestProperty
{
private readonly string arg;
public TestProperty( string arg )
{
this.arg = arg;
}
public string Arg { get { return arg; } }
}
public class TestController
{
public ITestProperty Property { get; set; }
}
When we go to initialize the "TestController" object above the exception is thrown. Is it possible to do this with StructureMap? Assuming that it is possible, what do I need to do to get it working?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有多种方法可以做到这一点,正如 Josh 提到的,如果命名实例很重要,那么您希望将其添加到注册表中:
否则,您可以这样做:
此外,这是旧的 StructureMap 语法,您可能需要更新到最新版本。这是新语法:
There are several ways to do this, as Josh mentioned if the named instance is important then you want this in your registry:
Otherwise, you can do this:
Also, this is old StructureMap syntax, you might want to update to the latest version. Here is the new syntax: