I am working on an outdated application, and my current goal is to establish some kind of integration test, which also uses a structure map. So far, I have not had problems using SM for unit testing and production code for almost a year. But, unfortunately, we are forced to use setter injection, because our BOs are a real dependent beast (many of these objects have circular dependencies on others)
Our training for this was to use a two-stage approach: whether we first created all these objects without input properties (which cause these circular dependencies), and after the chambers create these objects (introduce dependency objects through their properties), when they were already created. (See Examples of Class A, B, and Logic that have circular dependencies)
This has worked so far β When we run, suppose that three integration tests after each other than the first one βturns greenβ, the other test throws execptions when the structmap tries to create instances (in the GetManagerInstances methods).
I assume that SM saves its config file.
Definitions
internal interface IM { } internal interface ILogic { } internal interface IMA : IM { } internal interface IMB : IM { } internal class A : IMA { public A() { Console.WriteLine("A"); } public IMB BInstance { get; set; } } internal class B : IMB { public B() { Console.WriteLine("B"); } public IMA AInstance { get; set; } public ILogic Logic { get; set; } } internal class Logic : ILogic { public Logic() { Console.WriteLine("Logic"); } public IMA AInstance { get; set; } }
Initialization
private static IContainer _container; while (true) { Initialize(); } internal static void Initialize() { _container = new Container(c => { c.For<ILogic>().Singleton().Use<Logic>(); c.For<IMB>().Singleton().Use<B>(); c.For<IMA>().Singleton().Use<A>(); });
Result
StructureMap exception code: 295 A bi-directional dependency problem was detected using RequestedType: StructureMapTests.IMA, Name: 237b6fb1-7dee-4f09-b663-c33bb793afc6, ConcreteType: StructureMapTests.A. BuildStack: 1.) RequestedType: StructureMapTests.IMA, Name: 237b6fb1-7dee-4f09-b663-c33bb793afc6, ConcreteType: StructureMapTests.A 2.) RequestedType: StructureMapTests.IMB, Name: ad692281-cd27fdccdc4cdcdc : StructureMapTests.B Exception on creation *
Analysis
Output for the first test run:
B
Logic β Everything is OK
Output for the second test run:
B
Instance Exception
B
Exception in the construction of Logic
B β ERROR REPORT
Please, help: (
EDIT I am targeting the 4.0.Net framework and using StructureMap release 2.6.2