如何使用 StructureMap 来创建具有原始构造函数参数的对象的 setter 依赖项?

发布于 2024-09-24 13:42:31 字数 1605 浏览 6 评论 0原文

我有一个实现“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的海 2024-10-01 13:42:31

有多种方法可以做到这一点,正如 Josh 提到的,如果命名实例很重要,那么您希望将其添加到注册表中:

ForRequestedType<ITestProperty>().AddInstances(i => 
    i.OfConcreteType<TestProperty>().WithName("Test")
        .WithCtorArg("arg").EqualTo("abc"));

ForConcreteType<TestController>().Configure
    .SetterDependency(p => p.Property).Is(c => c
        .GetInstance<ITestProperty>("Test"));

否则,您可以这样做:

ForRequestedType<ITestProperty>().TheDefault.Is
    .OfConcreteType<TestProperty>()
    .WithCtorArg("arg").EqualTo("abc");

ForConcreteType<TestController>().Configure
    .SetterDependency(p => p.Property).IsTheDefault();

此外,这是旧的 StructureMap 语法,您可能需要更新到最新版本。这是新语法:

For<ITestProperty>().Add<TestProperty>().Named("Test")
    .Ctor<string>("arg").Is("abc");

ForConcreteType<TestController>().Configure
    .Setter(p => p.Property).Is(c => c
        .GetInstance<ITestProperty>("Test"));

There are several ways to do this, as Josh mentioned if the named instance is important then you want this in your registry:

ForRequestedType<ITestProperty>().AddInstances(i => 
    i.OfConcreteType<TestProperty>().WithName("Test")
        .WithCtorArg("arg").EqualTo("abc"));

ForConcreteType<TestController>().Configure
    .SetterDependency(p => p.Property).Is(c => c
        .GetInstance<ITestProperty>("Test"));

Otherwise, you can do this:

ForRequestedType<ITestProperty>().TheDefault.Is
    .OfConcreteType<TestProperty>()
    .WithCtorArg("arg").EqualTo("abc");

ForConcreteType<TestController>().Configure
    .SetterDependency(p => p.Property).IsTheDefault();

Also, this is old StructureMap syntax, you might want to update to the latest version. Here is the new syntax:

For<ITestProperty>().Add<TestProperty>().Named("Test")
    .Ctor<string>("arg").Is("abc");

ForConcreteType<TestController>().Configure
    .Setter(p => p.Property).Is(c => c
        .GetInstance<ITestProperty>("Test"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文