An introduction to the concepts of inversion of control and dependency injection with a .NET / C# perspective. This was a presentation for the Christchurch .NET User Group
1. The Zen of Inversion of
Control
Christopher Fairbairn
ARANZ Medical Ltd
christopher@christec.co.nz
www.christec.co.nz/blog/
2. A definition?
“Inversion of Control, or IoC, is an
abstract principle describing an
aspect of some software
architecture designs in which the
flow of control of a system is
inverted in comparison to the
traditional architecture of
software libraries.”
www.christec.co.nz/blog/
4. An improved definition?
“Inversion of Control is a style of
software construction where
reusable, generic code controls the
execution of problem-specific code
with the strong connotation that
the reusable code and the problem-
specific code are developed
independently”
www.christec.co.nz/blog/
5. An improved definition?
“High-level modules should not
depend on low-level modules. Both
should depend on abstractions.”
Robert C. Martin .
www.christec.co.nz/blog/
6. A worked example
http://www.code-magazine.com/Article.aspx?quickid=0903101
Jon Stonecash
CODE magazine
March/April issue
www.christec.co.nz/blog/
7. Code Demo
Developing throw away code
“It’s ok, this will never go into
production use…”.
www.christec.co.nz/blog/
8. Code Demo
Making it more flexible
“It’s only one additional
parameter. No harm done…”.
www.christec.co.nz/blog/
9. A scalability problem?
•Can we only process files older than 5 days?
•Can we delete files rather than listing them?
•I want PDF files as well as XLS files.
“I can’t keep doing this for
every change in requirements…”.
www.christec.co.nz/blog/
10. The Core Logic
Enumerate over
each file
Compare to filter
condition
Process files that
match
www.christec.co.nz/blog/
11. The Project Specific Logic
What is the What to do
“filter” with files
condition? that match?
www.christec.co.nz/blog/
12. Delegates
“A delegate is a type defining a
method signature, so that delegate
instances can hold and invoke a
method or list of methods that
match its signature.”
C# Language Pocket Reference
www.christec.co.nz/blog/
13. Delegates
An original C# 1.0 feature
Define
delegate int CalculationHandler(int a, int b);
Construct
CalculationHandler ch = new
CalculationHandler(AddNumbers);
Invoke
int result = ch(1, 2);
www.christec.co.nz/blog/
14. Delegates
An original C# 1.0 feature
Define
delegate int CalculationHandler(int a, int b);
Construct
CalculationHandler ch = AddNumbers;
Invoke
int result = ch(1, 2);
www.christec.co.nz/blog/
15. Delegates
C# 2.0 introduced Anonymous Delegates
Define
delegate int CalculationHandler(int a, int b);
Construct
CalculationHandler ch =
delegate(int a, int b){ return a + b; };
Invoke
int result = ch(1, 2);
www.christec.co.nz/blog/
16. Code Demo
Inverting Control
“My secret plan to allow my
workmates to do the hard work”
www.christec.co.nz/blog/
17. Delegates
C# 3.0 introduced Lambda Expressions
Define
delegate int CalculationHandler(int a, int b);
Construct
CalculationHandler ch = (a, b) => a + b;
Invoke
int result = ch(1, 2);
www.christec.co.nz/blog/
18. .NET 3.5 BCL
The .NET 3.5 BCL introduces some common
delegate types
namespace System {
delegate void Action<T>(T obj);
delegate TResult Func<T, TResult>(T arg);
}
www.christec.co.nz/blog/
19. Code Demo
Reducing the amount of code
“The less I need to type the
happier I’ll be…”.
www.christec.co.nz/blog/
20. What have we achieved?
•Separation of Concerns
•Inversion of Control
“But I’ve also made it harder for
my clients to use…”.
www.christec.co.nz/blog/
21. IoC Containers
“Dependency Injection refers to the
process of automatically supplying an external
dependency to a software component.
It is a specific form of inversion of control
where the concept being inverted is the
process of obtaining the needed dependency.”
www.christec.co.nz/blog/
22. IoC Containers
•Hashtable of interface vs
implementing type
•In its simplest form basically
Dictionary<Type, Object>
•Lifecycle management etc
www.christec.co.nz/blog/
23. Using Ninject
•Adding objects to the container
•XML, Fluent, Database, Code…
• Bind<IFileFilter>().To<SomeClass>();
“IoC containers perform two
basic functions: putting
dependencies in and getting fully
constructed objects out…”.
www.christec.co.nz/blog/
24. Using Ninject
•Instantiating objects via the Container
•Constructor or Property Injection
•IFileFilter filter = kernel.Get<IFileFilter>();
“IoC containers perform two
basic functions: putting
dependencies in and getting fully
constructed objects out…”.
www.christec.co.nz/blog/
25. Code Demo
Dependency Injection with
Ninject
“Hiding the complexity with
another layer of indirection…”.
www.christec.co.nz/blog/
26. IoC Advantages
•Ability to quickly reconfigure / repurpose
•Reduces hard dependencies between modules
•Centralises configuration
www.christec.co.nz/blog/
27. A bridge too far?
•As with any technique this can be taken to
ill-logical extremes and beyond
•There are trade offs and gains with
•Flexability
•Maintainability
•Understandability
www.christec.co.nz/blog/
28. Find out more
Inversion of Control Containers and
the Dependency Injection Pattern -
Martin Fowler
http://martinfowler.com/articles/injection.html
Loosen Up: Tame Your Dependencies for More
Flexible Apps – James Kovacs
http://msdn.microsoft.com/en-us/magazine/cc337885.aspx
www.christec.co.nz/blog/