Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 964
QuangHao Ta Avatar asked Jul 06 '16 08:07

QuangHao Ta


1 Answers

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

like image 55
Ashley John Avatar answered Nov 15 '22 03:11

Ashley John