Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When doing Dependency Injection the trick is to have all your classes inject their dependencies through the constructor. This way you can let the Kernel build up a complete object graph for you from the root object.</p> <p>This 'root object' however, is something that has to be resolved directly by calling <code>kernel.Get&lt;HomeController&gt;()</code> (if <code>HomeController</code> is the root object). So somewhere in your application you will have to call <code>kernel.Get</code>. It's impossible to go without.</p> <p>The trick is to minimize this to ideally a single line of code in the application and place this near the startup path. Probably close to the place where you registered your dependencies. The rest of the application stays oblivious to the use of any DI framework.</p> <p>There are even integration packages for Ninject and other containers that allow you to remove that single line of code depending on which application platform you use. ASP.NET MVC for instance has great extendibility points that allow you to plug in a <code>ControllerFactory</code> that allows you to let that factory call <code>kernel.Get</code> for you.</p> <blockquote> <p>How do we use DI without coupling to a DI framework?</p> </blockquote> <p>There will always be some coupling, but ideally this should only be the startup path. All other code should be oblivious to the use of a container. There will still be a assembly dependency, but this dependency will only exist in the startup project, and not in your business layer.</p> <blockquote> <p>look at the reason DI frameworks exists: To prevent having to do DI by hand</p> </blockquote> <p>To be more precise. DI helps making your application more maintainable. DI frameworks help making the startup path of your application (a.k.a. <a href="http://blog.ploeh.dk/2011/07/28/CompositionRoot.aspx" rel="nofollow">composition root</a>) more maintainable.</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