StructureMap异常代码205缺少请求的实例属性

Jas*_*ans 7 structuremap

我正在尝试最新的StructureMap构建,以了解IoC容器等.作为我的第一次测试,我有以下课程:

public class Hospital
    {
        private Person Person { get; set; }
        private int Level { get; set; }

        public Hospital(Person employee, int level)
        {
            Person = employee;
            Level = level;
        }

        public void ShowId()
        {
            Console.WriteLine(this.Level);
            this.Person.Identify();
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后我像这样使用StructureMap:

static void Main()
        {
            ObjectFactory.Configure(x =>
                                        {
                                            x.For<Person>().Use<Doctor>();
                                            x.ForConcreteType<Hospital>().Configure.Ctor<int>().Equals(23);
                                        });

            var h = ObjectFactory.GetInstance<Hospital>();

            h.ShowId();
        } 
Run Code Online (Sandbox Code Playgroud)

所以我将Doctor对象作为第一个构造函数param传递给Hospital,我正在尝试将levelparam 设置为23.当我运行上面的代码时,我得到:

未处理的异常:StructureMap.StructureMapException:StructureMap异常代码:205缺少所请求的实例属性"level"for InstanceKey"5f8c4b74-a398-43f7- 91d5-cfefcdf120cf"

所以看起来我根本就没有设置level参数.有人能指出我正确的方向 - 如何level在构造函数中设置参数?

干杯.雅.

Kev*_*evM 12

你非常接近.您在依赖表达式而不是Is方法上意外使用了System.Object.Equals方法.我还建议在配置常用类型(如字符串或值类型(int,DateTime))时指定构造函数参数名称以避免歧义.

以下是我要测试的内容:

    [TestFixture]
public class configuring_concrete_types
{
    [Test]
    public void should_set_the_configured_ctor_value_type()
    {
        const int level = 23;
        var container = new Container(x =>
        {
            x.For<Person>().Use<Doctor>();
            x.ForConcreteType<Hospital>().Configure.Ctor<int>("level").Is(level);
        });

        var hospital = container.GetInstance<Hospital>();

        hospital.Level.ShouldEqual(level);
    }
}
Run Code Online (Sandbox Code Playgroud)