Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you are looking for <a href="http://martinfowler.com/articles/injection.html#UsingAServiceLocator" rel="nofollow">ServiceLocator</a> functionality, which I don't think you can achieve with spring.net by only changing the configuration. Note that the <code>ServiceLocator</code> pattern is generally discouraged from a dependency-injection point of view, because it makes your object aware of its di container.</p> <p>If you do need a <code>ServiceLocator</code> and don't mind tying your object to the Spring DI container, following <em>could be</em> a solution.</p> <p>I assume your current code is something like this:</p> <pre><code>public class CommandManager { Dictionary&lt;CommandType, Command&gt; { get; set; } // set using DI public Command GetBy(CommandType cmdKey) { return Dictionary[cmdKey]; } } </code></pre> <p>Map the enum values to names of objects in you spring config by replacing your current <code>Dictionary&lt;CommandType, Command&gt;</code> with a <code>Dictionary&lt;CommandType, string&gt;</code>. Then use the current spring context to get the desired object:</p> <pre><code>using Spring.Context; using Spring.Context.Support; public class CommandManager { Dictionary&lt;CommandType, string&gt; { get; set; } // set using DI; values are object names public Command GetBy(CommandType cmdKey) { string objName = Dictionary[cmdKey]; IApplicationContext ctx = ContextRegistry.GetContext(); return (Command)ctx.GetObject(objName); } } </code></pre> <p>Don't forget to set the scope of your command objects to <code>prototype</code>:</p> <pre class="lang-xml prettyprint-override"><code>&lt;object name="moveCommand" type="Example.Command.MoveCommand, CommandLib" scope="prototype"&gt; &lt;property name="StepSize" value="10" /&gt; &lt;/object&gt; </code></pre> <p>Now every time <code>CommandManager.GetBy(myKey)</code> is called, a new instance is created.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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