Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think a lot of times people get confused about the difference between <em>dependency injection</em> and a dependency injection <em>framework</em> (or a <em>container</em> as it is often called).</p> <p>Dependency injection is a very simple concept. Instead of this code:</p> <pre><code>public class A { private B b; public A() { this.b = new B(); // A *depends on* B } public void DoSomeStuff() { // Do something with B here } } public static void Main(string[] args) { A a = new A(); a.DoSomeStuff(); } </code></pre> <p>you write code like this:</p> <pre><code>public class A { private B b; public A(B b) { // A now takes its dependencies as arguments this.b = b; // look ma, no "new"! } public void DoSomeStuff() { // Do something with B here } } public static void Main(string[] args) { B b = new B(); // B is constructed here instead A a = new A(b); a.DoSomeStuff(); } </code></pre> <p><strong>And that's it.</strong> Seriously. This gives you a ton of advantages. Two important ones are the ability to control functionality from a central place (the <code>Main()</code> function) instead of spreading it throughout your program, and the ability to more easily test each class in isolation (because you can pass mocks or other faked objects into its constructor instead of a real value).</p> <p>The drawback, of course, is that you now have one mega-function that knows about all the classes used by your program. That's what DI frameworks can help with. But if you're having trouble understanding why this approach is valuable, I'd recommend starting with manual dependency injection first, so you can better appreciate what the various frameworks out there can do for you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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