Sunday, October 24, 2010

What is Inversion of Control (loC)


The Inversion of Control (and also Dependency Injection) patterns concerned with removing dependencies from your code.

public class TextEditor
{
private SpellChecker checker;
public TextEditor()
{
checker = new SpellChecker();
}
}

In the above code sample we have created a dependency between the TextEditor and the SpellChecker. in an ioC scenario we would instead do something like this.

public class TextEditor
{
private ISpellChecker checker;
public TextEditor(ISpellChecker checker)
{
this.checker=checker;
}
}

Now, the client creating the TextEditor class has the total control over which SpellChecker implementation to use. We're injecting the TextEditor with the dependency.






No comments:

Post a Comment