Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm sorry to plug <a href="http://simpleinjector.codeplex.com" rel="nofollow">Simple Injector</a> here, but I couldn't help noticing that you're struggling with something that is child's play in Simple Injector. In Simple Injector you can do what you want in two lines of code:</p> <pre class="lang-cs prettyprint-override"><code>// using SimpleInjector; // using SimpleInjector.Extensions; var container = new Container(); container.RegisterManyForOpenGeneric( typeof(ICommandHandler&lt;&gt;), assembly); container.RegisterSingleDecorator( typeof(ICommandHandler&lt;&gt;), typeof(FactoryCommandHandler&lt;&gt;)); </code></pre> <p>These two simple lines ensure the following:</p> <ul> <li>The supplied assembly is searched for concrete implementations of <code>ICommandHandler&lt;T&gt;</code>.</li> <li>If a concrete implementations has defines the <code>ICommandHandler&lt;T&gt;</code> interface multiple times, it is registered for each closed-generic version of that interface.</li> <li>The <code>FactoryCommandHandler&lt;T&gt;</code> is registered to be wrapped around <code>ICommandHandler&lt;T&gt;</code> implementations. For each closed-generic version of <code>ICommandHandler&lt;T&gt;</code> a single instance of that generic <code>FactoryCommandHandler&lt;T&gt;</code> is returned.</li> <li>A <code>Func&lt;ICommandHandler&lt;T&gt;&gt;</code> is injected into that <code>FactoryCommandHandler&lt;T&gt;</code> that allows creating the decoratee (the instance that is wrapped). This effectively delays the creation of that instance. </li> <li>The injected factory will preserve the lifestyle of the decoratee.</li> </ul> <p>The <code>FactoryCommandHandler&lt;T&gt;</code> only depends on a <code>Func&lt;T&gt;</code> which is a singleton. The <code>FactoryCommandHandler&lt;T&gt;</code> can therefore be registered itself as a singleton (what happens in the registration above). If it depends on dependencies of other lifestyles, it might be better to register it as <em>transient</em>.</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