inversion of control
Post on 10-May-2015
2.814 Views
Preview:
DESCRIPTION
TRANSCRIPT
www.jaftalks.com
Introduction to the
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
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)
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!
www.jaftalks.com
The problem
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?
www.jaftalks.com
Inversion Of Control
Don’t call me, I’ll call you
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?
www.jaftalks.com
What is Dependency Injection?
www.jaftalks.com
Dependency InjectionIn DI we have three ways of implementing
the same:Constructor way Setter wayMethod way
www.jaftalks.com
Constructor Injection
www.jaftalks.com
Setter Way
www.jaftalks.com
Method Way
www.jaftalks.com
DemonstrationLets See Some Code…
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
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.
www.jaftalks.com
Service Locator
www.jaftalks.com
Who will manage the Dependency Injection?
Containers !
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.
www.jaftalks.com
Container
www.jaftalks.com
Thanks For Listening. Mail: hasanjaffal@hotmail.com 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
top related