Note that there are some explanatory texts on larger screens.

plurals
  1. POCircular dependencies in StructureMap - can they be broken with property injection?
    text
    copied!<p>I've got the simplest kind of circular dependency in structuremap - class A relies on class B in its constructor, and class B relies on class A in its constructor. To break the dependency, I made class B take class A as a property, rather than a constructor argument, but structuremap still complains.</p> <p>I've seen circular dependencies broken using this method in other DI frameworks - is this a problem with Structuremap or am I doing something wrong?</p> <p><b>Edit:</b> I should mention that class B's property is an array of class A instances, wired up like this:</p> <pre><code>x.For&lt;IB&gt;().Singleton().Use&lt;B&gt;().Setter(y =&gt; y.ArrayOfA).IsTheDefault(); </code></pre> <p>Just to clarify, I want the following sequence of events to occur:</p> <ul> <li>Construct an instance of B, "b"</li> <li>Construct an instance of A, "a", injecting "b" into its constructor</li> <li>Set "b.ArrayOfA" to ["a"]</li> </ul> <p>And I want all this to happen using autowiring, if possible... </p> <p><b>Edit 2:</b> Here's a simplified example that uses explicit wiring up:</p> <pre><code>interface ILoader { } interface ILoaderManager { } class Loader : ILoader { public Loader(ILoaderManager lm) { } } class LoaderManager : ILoaderManager { public ILoader Loader { get; set; } // Was an array, but same circular dependency appears here } ObjectFactory.Configure ( x =&gt; { x.For&lt;ILoader&gt;.Singleton().Use&lt;Loader&gt;(); x.For&lt;ILoaderManager&gt;().Singleton().Use&lt;LoaderManager&gt;().OnCreation((c, a) =&gt; a.Loader = c.GetInstance&lt;ILoader&gt;()); } ); </code></pre> <p>Validating the configuration causes "Bidirectional Dependency Problem detected with RequestedType: IocTest2.ILoader..."</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload