Dependency Injection
What, Why and How
Ravish Chhabra
DesignRequirements
 Maintainability :- It is the quality of a software system that determines how
easily and how efficiently you can update it.
 Testability :- It enables you to effectively test individual parts of the system. In
short, how easy is to test the system. Test driven development is use to write Unit
test before writing any code to implement to improve the quality of applications.
 Flexibility and Extensibility :- How easily the system can adapt to work in
different ways is flexibility and add new features is extensibility
Definition and Types
 Coupling :- It states the dependency of the system. How dependent are sub
systems in the system.
 Tight Coupling
 Loose Coupling
 Tight Coupling :- Strong or direct dependency between components.
 Loose Coupling :- Loose or indirect dependency between components.
Tight Coupling
CustomerRepository class directly
depends upon Database class.
Loose Coupling
CustomerRepository class depends
upon IDatabase interface
DependencyInjection
 WHAT
 A Software Design Pattern that follows Dependency inversion principle and
implement Inversion of Control for the resolution of dependencies at run-
time or compile time.
 WHY
 The “D” is Dependency Injection (in SOLID principles of design) used to
achieve Loose Coupling in the system.
Principles
 Dependency Inversion Principle:- A software design principle that provide
us guidelines to loosely couple the system.
1. High level modules should not depend on low level modules or
subsystems. Both should depend on Abstractions.
2. Abstraction should not depend on details but vice versa.
 Inversion Of Control (IOC) :- It is the mechanism that is used to implement
the Dependency Inversion principle.
Abstraction is introduced in the system to make the high level modules to
depend on abstraction rather than the sub systems themselves.
Tightly Coupled System
Forcefully you have to write to
Event Logs only.
IOC Mechanism implemented
By adding an interface( INotify )
High level module LogMonitor
using abstraction( INotify )
Still, Concrete class is initialized
To achieve further level of
Decoupling, DI is used.
 Dependency Injection injects the concrete implementation into a class ( by moving
it out of dependent class) that is using abstraction.
 Major ways of implementing DI
 Constructor Injection - pass the object of concrete class into the constructor of
dependent class.
Method Injection - pass the object of the concrete class into the method the
dependent class which is actually invoking the action.
Property Injection - pass the object of the concrete class via a setter property
that was exposed by the dependent class.
Dependency Injection Continued..
Constructor Injection
Object of concrete class
(INotify Type) will be passed into
Constructor parameter
Object of concrete class passed to
Constructor of LogMonitor
Method Injection
Object of concrete class
(INotify Type) will be passed
into Method parameter
Object of concrete class passed as
Method parameter
Other Key Concepts Of DI
 Dependency injection injects the dependencies of a class at runtime.
 DI Containers: To automatically inject dependencies we use a Dependency Injection(DI)
container. We can also inject the dependencies manually but using a DI container provides
the following benefits
 Automatic Dependency Resolution When dependendencies are managed by the
container there are less chances of error. Suppose if our application has a lot of
dependencies then injecting those dependencies is also difficult to manage if we are
injecting them without a DI container
 Decouples the client from the dependency – It saves the effort and work spent by client
to inject the dependency.
 StructureMap
 UnityContainer
 Autofac
 Ninject
 Castle Windsor
 Spring .Net
Popular DI Containers For .NET
DI Containers Example-Unity Container
Dependency configured using Container
and is resolved at run time
Pros and Cons
 PROS
 Reduced Dependencies.
 Increased Testability and Maintainability – Inject mock implementations for testing.
 More Reusable and Extensible Code – External pluggable and configurable components.
 Run Time configuration for Dependency Injection
 CONS
 Increased code complexity.
 Can complicate debugging while development.
`
Dependency injection and inversion

Dependency injection and inversion

  • 1.
    Dependency Injection What, Whyand How Ravish Chhabra
  • 2.
    DesignRequirements  Maintainability :-It is the quality of a software system that determines how easily and how efficiently you can update it.  Testability :- It enables you to effectively test individual parts of the system. In short, how easy is to test the system. Test driven development is use to write Unit test before writing any code to implement to improve the quality of applications.  Flexibility and Extensibility :- How easily the system can adapt to work in different ways is flexibility and add new features is extensibility
  • 3.
    Definition and Types Coupling :- It states the dependency of the system. How dependent are sub systems in the system.  Tight Coupling  Loose Coupling  Tight Coupling :- Strong or direct dependency between components.  Loose Coupling :- Loose or indirect dependency between components.
  • 4.
    Tight Coupling CustomerRepository classdirectly depends upon Database class.
  • 5.
    Loose Coupling CustomerRepository classdepends upon IDatabase interface
  • 6.
    DependencyInjection  WHAT  ASoftware Design Pattern that follows Dependency inversion principle and implement Inversion of Control for the resolution of dependencies at run- time or compile time.  WHY  The “D” is Dependency Injection (in SOLID principles of design) used to achieve Loose Coupling in the system.
  • 7.
    Principles  Dependency InversionPrinciple:- A software design principle that provide us guidelines to loosely couple the system. 1. High level modules should not depend on low level modules or subsystems. Both should depend on Abstractions. 2. Abstraction should not depend on details but vice versa.  Inversion Of Control (IOC) :- It is the mechanism that is used to implement the Dependency Inversion principle. Abstraction is introduced in the system to make the high level modules to depend on abstraction rather than the sub systems themselves.
  • 8.
    Tightly Coupled System Forcefullyyou have to write to Event Logs only.
  • 9.
    IOC Mechanism implemented Byadding an interface( INotify ) High level module LogMonitor using abstraction( INotify ) Still, Concrete class is initialized To achieve further level of Decoupling, DI is used.
  • 10.
     Dependency Injectioninjects the concrete implementation into a class ( by moving it out of dependent class) that is using abstraction.  Major ways of implementing DI  Constructor Injection - pass the object of concrete class into the constructor of dependent class. Method Injection - pass the object of the concrete class into the method the dependent class which is actually invoking the action. Property Injection - pass the object of the concrete class via a setter property that was exposed by the dependent class. Dependency Injection Continued..
  • 11.
    Constructor Injection Object ofconcrete class (INotify Type) will be passed into Constructor parameter Object of concrete class passed to Constructor of LogMonitor
  • 12.
    Method Injection Object ofconcrete class (INotify Type) will be passed into Method parameter Object of concrete class passed as Method parameter
  • 13.
    Other Key ConceptsOf DI  Dependency injection injects the dependencies of a class at runtime.  DI Containers: To automatically inject dependencies we use a Dependency Injection(DI) container. We can also inject the dependencies manually but using a DI container provides the following benefits  Automatic Dependency Resolution When dependendencies are managed by the container there are less chances of error. Suppose if our application has a lot of dependencies then injecting those dependencies is also difficult to manage if we are injecting them without a DI container  Decouples the client from the dependency – It saves the effort and work spent by client to inject the dependency.
  • 14.
     StructureMap  UnityContainer Autofac  Ninject  Castle Windsor  Spring .Net Popular DI Containers For .NET
  • 15.
    DI Containers Example-UnityContainer Dependency configured using Container and is resolved at run time
  • 16.
    Pros and Cons PROS  Reduced Dependencies.  Increased Testability and Maintainability – Inject mock implementations for testing.  More Reusable and Extensible Code – External pluggable and configurable components.  Run Time configuration for Dependency Injection  CONS  Increased code complexity.  Can complicate debugging while development. `