Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The Inversion of Control (IoC) and Dependency Injection (DI) patterns are all about removing dependencies from your code.</p> <p>For example, say your application has a text editor component and you want to provide spell checking. Your standard code would look something like this:</p> <pre><code>public class TextEditor { private SpellChecker checker; public TextEditor() { this.checker = new SpellChecker(); } } </code></pre> <p>What we've done here creates a dependency between the <code>TextEditor</code> and the <code>SpellChecker</code>. In an IoC scenario we would instead do something like this:</p> <pre><code>public class TextEditor { private IocSpellChecker checker; public TextEditor(IocSpellChecker checker) { this.checker = checker; } } </code></pre> <p>In the first code example we are instantiating <code>SpellChecker</code> (<code>this.checker = new SpellChecker();</code>), which means the <code>TextEditor</code> class directly depends on the <code>SpellChecker</code> class.</p> <p>In the second code example we are creating an abstraction by having the <code>SpellChecker</code> dependency class in <code>TextEditor</code> constructor signature (not initializing dependency in class). This allows us to call the dependency then pass it to the TextEditor class like so:</p> <pre><code>SpellChecker sc = new SpellChecker; // dependency TextEditor textEditor = new TextEditor(sc); </code></pre> <p>Now the client creating the <code>TextEditor</code> class has the control over which <code>SpellChecker</code> implementation to use because we're injecting the dependency to the <code>TextEditor</code> signature.</p> <p>This is just a simple example, there's <a href="http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart1.aspx" rel="noreferrer">a good series of articles</a> by Simone Busoli that explains it in greater detail.</p>
    singulars
    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. 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