Dependency Injection: November 2012
Dependency Injection: November 2012
November 2012
Dependency injection
What it is
Why to use it
How to use it
How it impacts OO design
How to use it in practice
Containers
Example
Example - results
Procedural programming
They use OO language
They dont use any of OOPs advantages
State of objects
Polymorphism, inheritance
OO design
Example results 2
Global variables
Debugging
Inversion of control
Traditionally, the flow of the business logic is
determined by objects that are statically assigned to
one another. With inversion of control, the flow
depends on the object graph that is instantiated by
the assembler and is made possible by object
interactions being defined through abstractions. The
binding process is achieved through dependency
injection.
Results
Advantages
Disadvantages / implications
Constructing objects
Class1(Interface2,
Interface3)
Class2(Interface4)
Class4()
Class3(Interface5,
Class5()
Interface6)
Class6()
Factory pattern
Vysvestlit factory a pak predavani factory konstruktorem (pokud potrebuju vytvaret objekty)
Pokud potrebuju vytvorit rozdilne objekty pokazde predavani factory konstruktorem
DI Container
No DI
Spaghetti Code
DI
Manual Dependency
Injection
Enhanced Dependency
Injection
Castle Windsor
Dynamic proxy
Helps implement proxy pattern
actual creation of proxy type happens at runtime,
and you dynamically can compose your proxy
objects.
interceptor gets a IInvocation object that holds all
the information about current request, like the
MethodInfo of the method called, along with its
parameters and returned value, reference to the
proxy, as well as proxied object
Castle Windsor
Castle.Core, Castle.Windsor
Dependencies resolved at runtime
(IType)kernel.Resolve(IType);
Close d2c
Summary
Dependency injection
Factory
Containers
Questions