dependency inversion principle: intro

14
Dependency Inversion Principle: Intro 1

Upload: tuyen

Post on 14-Feb-2016

36 views

Category:

Documents


0 download

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

Page 1: Dependency Inversion Principle: Intro

1

Dependency Inversion Principle: Intro

Page 2: Dependency Inversion Principle: Intro

DIP: definition

DEPEND ON ABSTRACTION

Page 3: Dependency Inversion Principle: Intro

DIP: definition

DO NOT DEPEND ON CONCRETE OBJECTS

Page 4: Dependency Inversion Principle: Intro

DIP: definition

IT’S CALLEDINVERSION OF

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

Page 5: Dependency Inversion Principle: Intro

DIP: definition

5

OR DEPENDENCYINJECTION

TOOOUT-

SOURCING INDIA

EFFECTS

Page 6: Dependency Inversion Principle: Intro

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

Page 7: Dependency Inversion Principle: Intro

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

Page 8: Dependency Inversion Principle: Intro

8

DIP: a diagram

Take a look at the class diagram

concrete classes depend on an abstraction

Page 9: Dependency Inversion Principle: Intro

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...

Page 10: Dependency Inversion Principle: Intro

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

Page 11: Dependency Inversion Principle: Intro

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:

Page 12: Dependency Inversion Principle: Intro

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();

Page 13: Dependency Inversion Principle: Intro

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();