Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In my opinion the number one benefit of an IoC is the ability to centralize the configuration of your dependencies.</p> <p>If you're currently using Dependency injection your code might look like this</p> <pre class="lang-cs prettyprint-override"><code>public class CustomerPresenter { public CustomerPresenter() : this(new CustomerView(), new CustomerService()) {} public CustomerPresenter(ICustomerView view, ICustomerService service) { // init view/service fields } // readonly view/service fields } </code></pre> <p>If you used a static IoC class, as opposed to the, IMHO the more confusing, configuration files, you could have something like this:</p> <pre class="lang-cs prettyprint-override"><code>public class CustomerPresenter { public CustomerPresenter() : this(IoC.Resolve&lt;ICustomerView&gt;(), IoC.Resolve&lt;ICustomerService&gt;()) {} public CustomerPresenter(ICustomerView view, ICustomerService service) { // init view/service fields } // readonly view/service fields } </code></pre> <p>Then, your Static IoC class would look like this, I'm using Unity here.</p> <pre class="lang-cs prettyprint-override"><code>public static IoC { private static readonly IUnityContainer _container; static IoC() { InitializeIoC(); } static void InitializeIoC() { _container = new UnityContainer(); _container.RegisterType&lt;ICustomerView, CustomerView&gt;(); _container.RegisterType&lt;ICustomerService, CustomerService&gt;(); // all other RegisterTypes and RegisterInstances can go here in one file. // one place to change dependencies is good. } } </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