Castle Windsor Property Injection不适用于动态参数

时间:2014-11-26 04:22:01

标签: castle-windsor nservicebus

我们想要使用NserviceBus Saga,为了做到这一点,你需要无参数构造函数用于你的传奇。注入我们关注的唯一另一种方法是使用看起来不起作用的属性注入。任何帮助和指导将不胜感激。

我已在下方发布示例代码,以显示问题。

class Program
{
    static void Main(string[] args)
    {
        var container = new WindsorContainer();

        container.Register(Component.For<ClassWithDynamicProperty>()
            .DynamicParameters((r, k) =>{ k["testString"] = "test"; }) );

        container.Register(
            Component.For<TestClassWithPropertyInjection>());

        container.Register(
            Component.For<TestClassWithConstructorInjection>());

        var class1 = container.Resolve<TestClassWithConstructorInjection>();
        var class2 = container.Resolve<TestClassWithPropertyInjection>();

        Debug.Assert(class1.DynamicClass == null);
        Debug.Assert(class2.ClassWithDynamicProperty == null);

    }
}

internal class TestClassWithPropertyInjection
{
    public TestClassWithPropertyInjection()
    {

    }

    public ClassWithDynamicProperty ClassWithDynamicProperty { get; set; }
}

internal class TestClassWithConstructorInjection
{
    private readonly ClassWithDynamicProperty _classWithDynamicProperty;

    public TestClassWithConstructorInjection(ClassWithDynamicProperty classWithDynamicProperty)
    {
        _classWithDynamicProperty = classWithDynamicProperty;
    }

    public ClassWithDynamicProperty DynamicClass { get { return _classWithDynamicProperty; }  }

}

public class ClassWithDynamicProperty
{
    public string TestString { get; private set; }

    public ClassWithDynamicProperty(string testString)
    {
        TestString = testString;
    }
}

0 个答案:

没有答案