Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to configure IoC container by code for certain task
    text
    copied!<p>I have a function, <code>MyFunc</code>, that receives an <code>IDoer</code>. I want to pass different implementations and different initializations:</p> <pre><code>var types = new IDoer[]{typeof(Walker),typeof(Runner),typeof(Sweamer)}; var strings = new[]{"abc","xyz","zoo","cat","dog"}; foreach(var type in types) { foreach(var str in strings) { IDoer doer = container.ResolveWithParams(type, str, RandomizeInteger()); MyFunc(doer, str); } } </code></pre> <p>or even better:</p> <pre><code>var strings = new[]{"abc","xyz","zoo","cat","dog"}; foreach(var type in types) { foreach(var str in strings) { IDoer doer = container.ResolveWithParams&lt;Walker&gt;(type, str, RandomizeInteger()); MyFunc(doer, str); doer = container.ResolveWithParams&lt;Runner&gt;(type, str, RandomizeInteger()); MyFunc(doer, str); doer = container.ResolveWithParams&lt;Sweamer&gt;(type, str, RandomizeInteger()); MyFunc(doer, str); } } </code></pre> <p>for instance, <code>Walker</code>'s constructor is:</p> <pre><code>public Walker(/*lots of params...*/, string importantString, /*other params...*/, int importantInteger,/*even more params...*/) {/*...*/} </code></pre> <p><code>Runner</code>'s is:</p> <pre><code>public Runner(string importantString, /*some params...*/, int importantInteger,/*additional different set of data...*/) {/*...*/} </code></pre> <p>and <code>Sweamer</code>'s:</p> <pre><code>public Sweamer(string importantString, int importantInteger) {/*...*/} </code></pre> <p>My question is how to configure this <code>container</code> using code (no XML) ?</p> <p>I don't mind which kind of container - it's just my first steps using IoC containers, and I want to learn in general how it's being done.</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