dependency inversion principle: intro

Post on 14-Feb-2016

51 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Dependency Inversion Principle: Intro. DIP: definition. DEPEND ON ABSTRACTION. DIP: definition. DO NOT DEPEND ON CONCRETE OBJECTS. DIP: definition. IT’S CALLED INVERSION OF CONTROL. .... .... . DIP: definition. OR DEPENDENCY INJECTION TOO. OUT-SOURCING INDIA EFFECTS. - PowerPoint PPT Presentation

TRANSCRIPT

1

Dependency Inversion Principle: Intro

DIP: definition

DEPEND ON ABSTRACTION

DIP: definition

DO NOT DEPEND ON CONCRETE OBJECTS

DIP: definition

IT’S CALLEDINVERSION OF

CONTROL ............

DIP: definition

5

OR DEPENDENCYINJECTION

TOOOUT-

SOURCING INDIA

EFFECTS

6

DIP: practical rules

No class should

derive from a

concrete class

No variable should hold a reference to a

concrete class

No method should override an

implementedmethod of any of its

base class

7

DIP: Inversion of what ??

Dep1 Dep2

interface

Dep1 Dep2

Injector

Dep2

Dep2

also high level

componentdepends on

an abstraction

concrete classes

depend on an

abstraction

new()new

()new()

high level component

depens on low level

components (concrete classes)

Consumer

before after

8

DIP: a diagram

Take a look at the class diagram

concrete classes depend on an abstraction

9

DIP: Who

This guy is not a preacher or a wizard: Martin Fowler (*)

He gave us three main styles(**) of dependency injection :

Interface Injection Setter Injection Constructor Injection

(*) but he is still waiting for a call from hollywood(**) and many other things...

10

DIP: Interface Injection (Type 1)

public interface IInjector { void InjectDependency(IDependent dependent); }

public class Injector : IInjector { private IDependent _dependent; public void InjectDependency(IDependent dependent) { _dependent = dependent; } public void DoSomething() { _dependent.DoSomethingInDependent(); } }

With this technique we define and use interfaces

Let’s define an interface to perform injection

Injector implements the interface

11

DIP: Interface Injection (Type 1)

IDependent dependency = GetCorrectDependency();

Injector injector = new Injector();injector.InjectDependency(dependency);

injector.DoSomething();

Get the correnct dependency based on config file

Create our main class and inject the dependency

the method references the dependency, so behaviour depends on the config file

<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <add key="ClassName" value="DIP.InterfaceInjection.DependentClass1" /> </appSettings></configuration>

Configuration file:

12

DIP: Setter Injection (Type 2)

public class Injector { private IDependent _dependent; public IDependent Dependent { get { return _dependent; } set { _dependent = value; } }

public void DoSomething() { Dependent.DoSomethingInDependent(); } }

Client class has a property

Create our main class and inject the dependency

IDependent dependency = GetCorrectDependency();

Injector injector = new Injector();injector.Dependent = dependency;

injector.DoSomething();

13

DIP: Constructor Injection (Type 3)

public class Injector { private IDependent _dependent; public Injector(IDependent dependent) { _dependent = dependent; }

public void DoSomething() { _dependent.DoSomethingInDependent(); } }

Client class has nothins else a constructor to inject dependency

Create our main class and passing dependencythrough constructor

IDependent dependency = GetCorrectDependency();

Injector injector = new Injector(dependency);injector.DoSomething();

top related