StructureMap-覆盖命名实例的构造函数参数 [英] StructureMap - Override constructor arguments for a named instance
本文介绍了StructureMap-覆盖命名实例的构造函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您可以为命名实例覆盖构造函数参数吗,看来您只能为默认实例执行此操作.
Can you override the constructor arguments for a named instance, it seems you can only do it for a default instance.
我想这样做:
ObjectFactory.With("name").EqualTo("Matt").GetNamedInstance<IActivity>("soccer");
推荐答案
在.With
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)));
}
}
}
这篇关于StructureMap-覆盖命名实例的构造函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文