Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>If I wasn't using a DI container, I wouldn't have to reference EntityFramework library in my MVC3 app, only my business layer which would reference my DAL/Repo layer.</p> </blockquote> <p>Yes, that's exactly the situation DI works so hard to avoid :)</p> <p>With tightly coupled code, each library may only have a few references, but these again have other references, creating a deep graph of dependencies, like this:</p> <p><img src="https://i.stack.imgur.com/6HBLN.png" alt="Deep Graph"></p> <p>Because the dependency graph is deep, it means that most libraries drag along a lot of other dependencies - e.g. in the diagram, <em>Library C</em> drags along <em>Library H, Library E, Library J, Library M, Library K</em> and <em>Library N</em>. This makes it harder to reuse each library independently from the rest - for example <a href="https://blog.ploeh.dk/2011/11/10/TDDImprovesReusability.aspx" rel="nofollow noreferrer">in unit testing</a>.</p> <p>However, in a loosely coupled application, by moving all the references to the <a href="https://blog.ploeh.dk/2011/07/28/CompositionRoot.aspx" rel="nofollow noreferrer">Composition Root</a>, the <strong>dependency graph is severely flattened</strong>:</p> <p><img src="https://i.stack.imgur.com/y2xO2.png" alt="Shallow Graph"></p> <p>As illustrated by the green color, it's now possible to reuse <em>Library C</em> without dragging along any unwanted dependencies.</p> <p>However, all that said, with many DI Containers, you don't <em>have</em> to add hard references to all required libraries. Instead, you can use <strong>late binding</strong> either in the form of convention-based assembly-scanning (preferred) or XML configuration.</p> <p>When you do that, however, you must remember to copy the assemblies to the application's bin folder, because that no longer happens automatically. Personally, I rarely find it worth that extra effort.</p> <p>A more elaborate version of this answer can be found in <a href="https://freecontent.manning.com/dependency-injection-in-net-2nd-edition-understanding-the-composition-root/" rel="nofollow noreferrer">this excerpt</a> from <a href="https://manning.com/seemann2/" rel="nofollow noreferrer">Dependency Injection in .NET, second edition</a>.</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