您是否可以覆盖命名实例的构造函数参数,似乎您只能为默认实例执行此操作。
我想这样做:
ObjectFactory.With("name").EqualTo("Matt").GetNamedInstance<IActivity>("soccer");
答案 0 :(得分:2)
GetInstance在.With
之后使用时的行为类似于GetNamedInstance
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using StructureMap;
namespace StructureMapWith
{
[TestFixture]
public class Class1
{
public interface IFooParent
{
IFoo Foo { get; set; }
}
public interface IFoo
{
}
public class FooParentLeft : IFooParent
{
public IFoo Foo { get; set; }
public FooParentLeft(IFoo foo)
{
Foo = foo;
}
}
public class FooParentRight : IFooParent
{
public IFoo Foo { get; set; }
public FooParentRight()
{
}
public FooParentRight(IFoo foo)
{
Foo = foo;
}
}
public class Left : IFoo { }
public class Right : IFoo { }
[Test]
public void See_what_with_does_more()
{
ObjectFactory.Initialize(c =>
{
c.ForRequestedType()
.AddInstances(i =>
{
i.OfConcreteType().WithName("Left");
i.OfConcreteType().WithName("Right");
});
c.ForRequestedType()
.AddInstances(i =>
{
i.OfConcreteType().WithName("Left");
i.OfConcreteType().WithName(
"Right");
});
});
var returned = ObjectFactory.With(typeof (IFoo), new Right())
.With(typeof (IFooParent), new FooParentRight())
.GetInstance("Right");
Assert.That(returned, Is.TypeOf(typeof(FooParentRight)));
Assert.That(returned.Foo, Is.TypeOf(typeof (Right)));
}
}
}