QuangHao Ta
QuangHao Ta

Reputation: 76

Get existing container in StructureMap 4.0

I have got a problem with StructureMap 4.0. When working with StructureMap 3.0, I use ObjectFactory to configure all instances and it's very easy to get instance through its own interface anywhere inside the solution. For example:

  1. at the global.cs, I declare like below: ObjectFactory.Initialize(x => { x.For().Use(); }

  2. at the other class, for instance, Family.cs, it's easy to get instance of Peson: var person = ObjectFactory.GetInstance();

However, I have got problem after updating StructureMap to 4.0. It uses Container instead of ObjectFactory and I don't find any way to get existing container.I mean this code is wrong var person = container.GetInstance();

If I declare Container as a static variable in global.cs then I can access it from Family.cs class. I don't like this way. I want Container work like ObjectFactory by fetching existing container that is declared in global.cs but I don't know how to do it.

Upvotes: 5

Views: 3034

Answers (1)

Ashley John
Ashley John

Reputation: 2453

The way to do this is to take IContainer as a constructor argument in your class. See this for more info StructureMap

Upvotes: 6

Related Questions