0

In the project I'm working on I have a StructureMap registry for the main web project and another registry for my integration tests. During some of the tests I wire up the web project's registry, so that I can get objects out of the container for testing.

In one case I want to be able to replace a default concrete type from the web registry with one in the test registry.

Is this possible?

How do you do it?

Mark Rogers
  • 96,497
  • 18
  • 85
  • 138

3 Answers3

1

In a similar situation I created a NestedContainer (Container.GetNestedContainer()) and overwrote the type there. I then use the nested container to resolve the type.

That keeps the original configuration intact but allows for such overriding.

flq
  • 22,247
  • 8
  • 55
  • 77
1

Yes you can, the container will serve up which ever type is last defined (according to my tests). I theorize (but I am unsure) that my mistake was that I called the IInitializationExpression.Scan mechanism after I registered some type and it got reregistered back to it's original setting.

Mark Rogers
  • 96,497
  • 18
  • 85
  • 138
  • Can we see the code please? I need help doing EXACTLY THIS and I am super new to StructureMap – Prisoner ZERO Sep 01 '17 at 12:04
  • @PrisonerZERO - Sorry, I don't have an example because I'm not really using StructureMap anymore, this answer was more than 7 years ago. You need to make sure its the last registration that occurs for that type is the one you want. – Mark Rogers Sep 01 '17 at 14:17
  • Yeah, I dont really care for StructureMap...Im a Unity fan, myself. :-) – Prisoner ZERO Sep 01 '17 at 16:17
0

Have you tried the .With() when you use GetInstance()?

Otherwise, you also use Container.Inject().

Brett Veenstra
  • 47,674
  • 18
  • 70
  • 86