0% found this document useful (0 votes)
42 views19 pages

Dependency Injection: November 2012

Dependency injection is a technique for achieving inversion of control by passing dependencies (such as objects) into classes rather than having classes create their own dependencies. This improves modularity and loose coupling. Dependency injection frameworks such as Castle Windsor can automate the process of dependency injection by resolving dependencies at runtime. Castle Windsor uses dynamic proxies to help implement the proxy pattern and intercept method calls.

Uploaded by

Teja Kamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views19 pages

Dependency Injection: November 2012

Dependency injection is a technique for achieving inversion of control by passing dependencies (such as objects) into classes rather than having classes create their own dependencies. This improves modularity and loose coupling. Dependency injection frameworks such as Castle Windsor can automate the process of dependency injection by resolving dependencies at runtime. Castle Windsor uses dynamic proxies to help implement the proxy pattern and intercept method calls.

Uploaded by

Teja Kamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Dependency injection

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 from another company


What do you think, why?

Example - results

Procedural programming
They use OO language
They dont use any of OOPs advantages
State of objects
Polymorphism, inheritance
OO design

Static classes and methods

Example results 2

Global variables
Debugging

Huge system, not essential money loss risk


Not even once tested before prod
A lot of production defects
Their solution -> unit testing

What is Dependency Injection

Instead of having your objects creating a dependency


or asking a factory object to make one for them, you
pass the needed dependencies in to the constructor,
and you make it somebody else's problem (an object
further up the dependency graph, or a dependency
injector that builds the dependency graph).

How to use Dependency Injection 1

How to make it better?

How to use Dependency Injection 2

How to make it better?

Advantages of Dependency Injection

Everyone knows dependencies of the class


No hidden dependencies

Enhances OO design modularity, loose coupling


When combined with IoC you to work with interface
instead of concrete implementation and makes your
class independent on implementation

Advantages of Dependency Injection

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.

Unit testing and TDD


Stubs and mocks
Ref other Keenans

Results

Advantages
Disadvantages / implications

Constructing objects

Class1(Interface2,
Interface3)

Class2(Interface4)

Class4()

Class3(Interface5,

Class5()

Interface6)

Class6()

var class1 = New Class1(new Class2(new Class4()),


new Class3(new Class5(), new Class6()));

Factory pattern

Constructs concrete implementations


Can contain some logic, more methods etc.
Injecting factory

Vysvestlit factory a pak predavani factory konstruktorem (pokud potrebuju vytvaret objekty)
Pokud potrebuju vytvorit rozdilne objekty pokazde predavani factory konstruktorem

Dependency Injection Frameworks (Containers)

DI Container (IoC Container) != DI


Automation of Dependency Injection
Primary goal is to decouple, not inject

Ninject, Castle Windsor, Spring.NET, StructureMap,


Unity,
No DI Container

DI Container

No DI

Spaghetti Code

Service Locator AntiPattern

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

Add examples of castle in d2c


Creating a controller
Registration
Dyn proxy

Summary

Dependency injection
Factory
Containers

Questions

You might also like