StructureMap 205 exception code Missing property of requested instance

I am testing the latest build of StructureMap to find out about IoC containers, etc. As my first test, I have the following class:

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(); } } 

Then I use StructureMap as follows:

 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(); } 

So, I pass the Doctor object as the first constructor parameter to the hospital, and I try to set the level parameter to 23. When I run the above code, I get:

Unhandled exception: StructureMap.StructureMapException: StructureMap exception code: 205 The requested instance property "Level" for InstanceKey "5f8c4b74-a398-43f7- 91d5-cfefcdf120cf" is missing

So it looks like I'm not setting the level parameter at all. Can someone point me in the right direction - how to set the level parameter in the constructor?

Greetings. Jac.

+7
structuremap
source share
1 answer

You were very close. You accidentally used the System.Object.Equals method in a dependency expression, and not in the Is method. I would also recommend when setting up generic types, such as string or value types (int, DateTime), to specify the name of a constructor argument to avoid ambiguity.

Here is my test with what you are looking for:

  [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); } } 
+12
source share

All Articles