1

下面的测试最后失败Assert()并显示此消息:预期:“arg2”但原为:“arg1”

似乎在第一次通过时 - 类型化工厂会记住这一点并从第二次调用中"arg1"忽略。"arg2"为什么?

public class E
{
    public string Arg { get; set; }

    public E(string arg)
    {
        Arg = arg;
    }
}

public interface IEFactory
{
    E Create(string arg);
}

[Test]
public void TypedFactoryWorksAsExpected()
{
    var windsor = new WindsorContainer();
    windsor.Kernel.AddFacility<TypedFactoryFacility>();
    windsor.Register(
        Component.For<E>(), 
        Component.For<IEFactory>().AsFactory());

    var factory = windsor.Resolve<IEFactory>();
    var e1 = factory.Create("arg1");
    var e2 = factory.Create("arg2");

    Assert.AreEqual("arg1", e1.Arg);

    // This line fails with  message: 
    // 'Expected: "arg2" But was: "arg1".'
    Assert.AreEqual("arg2", e2.Arg);
}
4

1 回答 1

1

对于 Windsor 组件,默认情况下是单例。您E是单身人士,因为您没有指定其他任何内容。第二个调用不会忽略参数,而是重用前一个实例,因为它是一个单例。

您必须将其定义E为瞬态:

windsor.Register(Component.For<E>().LifeStyle.Transient);
于 2012-08-09T14:31:24.700 回答