gpt4 book ai didi

inversion-of-control - StructureMap - 覆盖命名实例的构造函数参数

转载 作者:行者123 更新时间:2023-12-04 08:21:36 25 4
gpt4 key购买 nike

您能否覆盖命名实例的构造函数参数,似乎您只能为默认实例执行此操作。

我想要做:

ObjectFactory.With("name").EqualTo("Matt").GetNamedInstance<IActivity>("soccer"); 

最佳答案

在 .With 之后使用时,GetInstance 的行为类似于 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)));
}

}
}

关于inversion-of-control - StructureMap - 覆盖命名实例的构造函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/439156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com