Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm a little confused about what, exactly, you are trying to accomplish. If I am to assume you need to provide Smtp emailers, and you are using IoC, then you should be creating and wiring up your entire object graph with the IoC framework. By that, I mean that you would configure your IoC framework to create the SmtpClient, which it then creates the SmtpClientWrapper with, finally creating the SmtpEmailProvider with. You should not need to put any dependency-creation logic in the SmtpEmailProvider constructor.</p> <p>Here is an example with Castle Windsor, given the code you provided:</p> <pre><code>&lt;configuration&gt; &lt;component id="smtpClient" type="System.Net.Mail.SmtpClient, System"&gt; &lt;parameters&gt; &lt;Host&gt;127.0.0.1&lt;/Host&gt; &lt;Port&gt;25&lt;/Port&gt; &lt;/parameters&gt; &lt;/component&gt; &lt;component id="smtpClientWrapper" type="Naespace.SmtpClientWrapper, Assembly"&gt; &lt;parameters&gt; &lt;smtpClient&gt;${smtpClient}&lt;/smtpClient&gt; &lt;/parameters&gt; &lt;/component&gt; &lt;component id="smtpProvider" service="Namespace.IMessageProvider, Assembly" type="Namespace.SmtpEmailProvider, Assembly"&gt; &lt;parameters&gt; &lt;smtpClientWrapper&gt;${smtpClientWrapper}&lt;/smtpClientWrapper&gt; &lt;/parameters&gt; &lt;/component&gt; &lt;/configuration&gt; </code></pre> <p>With the above Windsor configuration, you can simply create your IMessageProvider like so:</p> <pre><code>public IMessageProvider LocateProviderByName(string providerName) { return IoC.Resolve&lt;IMessageProvider&gt;(providerName); } var messageProvider = LocateProviderByName("smtpProvider"); </code></pre> <p>The key point here is to use the IoC container for what it is: a dependency creation and management system that can, and should, create full object graphs for you. This alleviates the problem you have with too much dependency management logic in a constructor.</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