Inversion of control

21
www.jaftalks.com Introduction to the Inversion of Control

description

Inversion of control design pattern and its implementation, dependency injection and service locator.

Transcript of Inversion of control

Page 1: Inversion of control

www.jaftalks.com

Introduction to the

Inversion of Control

Page 2: Inversion of control

www.jaftalks.com

AgendaWhat is a Dependency?Example Application High Level Architecture Inversion of ControlDependency InjectionService LocatorDependency Injection Pros/Cons Unity ContainerQuestions

Page 3: Inversion of control

www.jaftalks.com

What is a Dependency?Some common dependencies include:

Application Layers Data Access Layer & Databases Business Layer

External services & Components Web Services Third Party Components

.NET Framework Components File Objects (File.Delete(…), Directory.Exists(…)) Web Objects (HttpContext, Session, Request, etc)

Page 4: Inversion of control

www.jaftalks.com

Database

Data Access Layer

Which Depends OnBusiness Logic Layer

Which Depends On

User Interface

Depends on

Dependencies at a Very High Level

CI Server

BROKEN BUILD!

Page 5: Inversion of control

www.jaftalks.com

The problem

Page 6: Inversion of control

www.jaftalks.com

Code is tightly coupledDifficult to isolate when testingDifficult to maintain

If I change ComponentX how do I know what else it will affect? Did I break anything? If tests are in place they can be your safety net

What problems do dependencies create?

Page 7: Inversion of control

www.jaftalks.com

Inversion Of Control

Don’t call me, I’ll call you

Page 8: Inversion of control

www.jaftalks.com

Dependency Injection is an implementation of IoC

The ability to supply (inject) an external dependency into a software component.

Types of Dependency Injection: Constructor (Most popular)Setter Method

What is Dependency Injection?

Page 9: Inversion of control

www.jaftalks.com

What is Dependency Injection?

Page 10: Inversion of control

www.jaftalks.com

Dependency InjectionIn DI we have three ways of implementing

the same:Constructor way Setter wayMethod way

Page 11: Inversion of control

www.jaftalks.com

Constructor Injection

Page 12: Inversion of control

www.jaftalks.com

Setter Way

Page 13: Inversion of control

www.jaftalks.com

Method Way

Page 14: Inversion of control

www.jaftalks.com

DemonstrationLets See Some Code…

Page 15: Inversion of control

www.jaftalks.com

ProsLoosely CoupledIncreases Testability (A LOT!)Separates components cleanlyAllows for use of Inversion of Control Container

ConsIncreases code complexitySome Jr. Developers find it difficult to understand

at FirstCan Complicate Debugging at FirstComplicates following Code Flow

Dependency Injection Pros & Cons

Page 16: Inversion of control

www.jaftalks.com

Service Locator Form of the Inversion of Control pattern It allows classes to locate specific services they

are interested in without needing to know who implements the service.

Page 17: Inversion of control

www.jaftalks.com

Service Locator

Page 18: Inversion of control

www.jaftalks.com

Who will manage the Dependency Injection?

Containers !

Page 19: Inversion of control

www.jaftalks.com

ContainerA container is an abstraction

responsible for object management, instantiation and configuration.

you can configure the objects using the container rather than writing client code like factory patterns to implement object management.  

Page 20: Inversion of control

www.jaftalks.com

Container

Page 21: Inversion of control

www.jaftalks.com

Thanks For Listening. Mail: [email protected] Blog: http://www.jaftalks.com/ Twitter: http://twitter.com/jaftalks

“You should be pragmatic with any pattern,it's not necessary to be applicable for every situation”

Jaffal Hasan

Practical