Note that there are some explanatory texts on larger screens.

plurals
  1. POStruggling with runtime setup of some castle components
    text
    copied!<p>My question (I think) can be simply described by this question:</p> <p><strong>How can I get a "Manager" class to have a list of all possible concrete implementations of a specific interface that have been registered as components injected into it so it can iterate through them?</strong> <em>(And because of annoying architecture reasons I can't use the app.config to register my components - so runtime registration only I guess)</em></p> <p><strong>EDIT:</strong><br> Ah hah! - Discovered that I can pass in my own configuration file using something like:</p> <pre><code>var container = new WindsorContainer(new XmlInterpreter("filename")); </code></pre> <p>so - I can just use the xml config route I stated below - this means that I don't have to worry about the instantiation of the child objects as they have injected dependencies themselves in my Real Life situation.</p> <p><strong>EDIT:</strong><br></p> <p><br > <br > <br ></p> <p>To try and aid your understanding of my issue though, I've set out below a simple repro showing my confusion! (Please forgive the terrible domain concept!)</p> <p>This is a simple console app that has a manager that will run an action on a list of objects that it's given.</p> <p><strong>The code:</strong> </p> <pre><code>public class Program { public static void Main() { var container = new WindsorContainer(new XmlInterpreter()); var manager = container.Resolve&lt;AnimalManager&gt;(); manager.PokeAnimals(); container.Release(manager); } } public interface IMakeNoise { void MakeNoise(); } public class Cat : IMakeNoise { public void MakeNoise() { Console.WriteLine("Meow"); } } public class Dog : IMakeNoise { public void MakeNoise() { Console.WriteLine("Woof"); } } public class AnimalManager { private readonly IList&lt;IMakeNoise&gt; noisemakers = new List&lt;IMakeNoise&gt;(); public AnimalManager(IList&lt;IMakeNoise&gt; noisemakers) { this.noisemakers = noisemakers; } public void PokeAnimals() { foreach (var noisemaker in noisemakers) { noisemaker.MakeNoise(); } } } </code></pre> <p><strong>The config file</strong> used looks like:</p> <p> </p> <pre><code>&lt;configSections&gt; &lt;section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/&gt; &lt;/configSections&gt; &lt;castle&gt; &lt;components&gt; &lt;component id="Dog" service="CastleListTest.IMakeNoise, CastleListTest" type="CastleListTest.Dog, CastleListTest" /&gt; &lt;component id="Cat" service="CastleListTest.IMakeNoise, CastleListTest" type="CastleListTest.Cat, CastleListTest" /&gt; &lt;component id="AnimalManager" service="CastleListTest.AnimalManager, CastleListTest" type="CastleListTest.AnimalManager, CastleListTest"&gt; &lt;parameters&gt; &lt;noisemakers&gt; &lt;list&gt; &lt;item&gt;${Cat}&lt;/item&gt; &lt;item&gt;${Dog}&lt;/item&gt; &lt;/list&gt; &lt;/noisemakers&gt; &lt;/parameters&gt; &lt;/component&gt; &lt;/components&gt; &lt;/castle&gt; </code></pre> <p></p> <p><strong>My Problem:</strong></p> <p>The above all works fine. However I can't define in an xml config file which kind of noisemakers I have. I won't know until runtime whether I have a cat, dog, 2 cats and a goldfish or whatever...</p> <p>The only way I can see of doing this is best described by the code below. The problem with this is that I'm having to instantiate my NoiseMakers: "Dog" and "Cat". This seems wrong, against Castle, and in my actual example, it's something I really don't want to do. So what am I doing wrong? <strong>How can I get a "Manager" class to have a list of all possible concrete implementations of a specific interface that have been registered as components injected into it so it can iterate through them?</strong></p> <pre><code> var container = new WindsorContainer(); container.AddComponent("Dog", typeof(IMakeNoise), typeof(Dog)); container.AddComponent("Cat", typeof(IMakeNoise), typeof(Cat)); container.AddComponent( "AnimalManager", typeof(AnimalManager), typeof(AnimalManager) ); IList&lt;IMakeNoise&gt; noisemakers = new List&lt;IMakeNoise&gt; {new Dog(), new Cat()}; var manager = container.Resolve&lt;AnimalManager&gt;( new Hashtable { { "noisemakers", noisemakers } } ); manager.PokeAnimals(); container.Release(manager); </code></pre>
 

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