Inversion of Control (or IoC) can be quite confusing when it is first encountered.
ANS:
The Inversion of Control (IoC) and Dependency Injection (DI) patterns are all about removing dependencies from your code.
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:
This is just a simple example, there's a good series of articles by Simone Busoli that explains it in greater detail.
- What is it?
- What problems does it solve?
- When is it appropriate and when not?
ANS:
The Inversion of Control (IoC) and Dependency Injection (DI) patterns are all about removing dependencies from your code.
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:
public class TextEditor
{
private SpellChecker checker;
public TextEditor()
{
checker = new SpellChecker();
}
}
What we've done here is create 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 control over which SpellChecker implementation to use. We're injecting the TextEditor with the dependency.This is just a simple example, there's a good series of articles by Simone Busoli that explains it in greater detail.
Comments
Post a Comment