dependency injection and inversion

17
Dependency Injection What, Why and How Ravish Chhabra

Upload: chhabraravish23

Post on 12-Apr-2017

232 views

Category:

Software


10 download

TRANSCRIPT

Page 1: Dependency injection and inversion

Dependency InjectionWhat, Why and How

Ravish Chhabra

Page 2: Dependency injection and inversion

Design Requirements 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

Page 3: Dependency injection and inversion

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.

Page 4: Dependency injection and inversion

Tight Coupling

CustomerRepository class directly depends upon Database class.

Page 5: Dependency injection and inversion

Loose Coupling

CustomerRepository class depends upon IDatabase interface

Page 6: Dependency injection and inversion

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

Page 7: Dependency injection and inversion

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.

Page 8: Dependency injection and inversion

Tightly Coupled System

Forcefully you have to write to Event Logs only.

Page 9: Dependency injection and inversion

IOC Mechanism implementedBy adding an interface( INotify )

High level module LogMonitorusing abstraction( INotify )

Still, Concrete class is initializedTo achieve further level of Decoupling, DI is used.

Page 10: Dependency injection and inversion

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

Page 11: Dependency injection and inversion

Constructor Injection

Object of concrete class (INotify Type) will be passed into Constructor parameter

Object of concrete class passed to Constructor of LogMonitor

Page 12: Dependency injection and inversion

Method Injection

Object of concrete class (INotify Type) will be passed into Method parameter

Object of concrete class passed as Method parameter

Page 13: Dependency injection and inversion

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.

Page 14: Dependency injection and inversion

StructureMap UnityContainer Autofac Ninject Castle Windsor Spring .Net

Popular DI Containers For .NET

Page 15: Dependency injection and inversion

DI Containers Example-Unity Container

Dependency configured using Containerand is resolved at run time

Page 16: Dependency injection and inversion

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.

`

Page 17: Dependency injection and inversion

Thank You