5

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.

QuangHao Ta
  • 76
  • 1
  • 3

1 Answers1

6

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

Ashley John
  • 2,379
  • 2
  • 21
  • 36
  • I understand what your mean. However, my project is built follow 3 layers structure. IContainer will be declare in Business Layer but I want access IContainer from DataAccess Layer. How can I do that? (I remind that DataAccess layer is under Business Layer) – QuangHao Ta Jul 06 '16 at 08:28
  • What do you mean? IContainer is declared in StructureMap, not in your business layer – erikkallen Jul 06 '16 at 08:45
  • How do you do this in initialization modules such as Global.asax.cs? If I try to do it here it gives me "too many redirects". – Peheje May 16 '18 at 12:14