StructureMap Exception Code 205 Missing requested Instance property
I'm trying out the latest build of StructureMap, to learn about IoC containers and the like. 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();
}
}
I then use StructureMap like this:
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'm passing a Doctor object as the first constructor param to Hosp开发者_StackOverflowital, and I'm trying to set the level
param to 23. When I run the above code I get:
Unhandled Exception: StructureMap.StructureMapException: StructureMap Exception Code: 205 Missing requested Instance property "level" for InstanceKey "5f8c4b74-a398-43f7- 91d5-cfefcdf120cf"
So it looks like I'm not setting the level
param at all. Can someone point me in the right direction - how do I set the level
param in the constructor?
Cheers. Jas.
You were very close. You accidentially used the System.Object.Equals method on the dependency expression rather than the Is Method. I would also recommend when configuring common types like string or value types (int, DateTime) to specify the constructor argument name 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);
}
}
精彩评论