SlideShare a Scribd company logo
1 of 17
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

More Related Content

What's hot (20)

Introducing Clean Architecture
Introducing Clean ArchitectureIntroducing Clean Architecture
Introducing Clean Architecture
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Dependency injection - the right way
Dependency injection - the right wayDependency injection - the right way
Dependency injection - the right way
 
Spring User Guide
Spring User GuideSpring User Guide
Spring User Guide
 
C# Constructors
C# ConstructorsC# Constructors
C# Constructors
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
Solid NodeJS with TypeScript, Jest & NestJS
Solid NodeJS with TypeScript, Jest & NestJSSolid NodeJS with TypeScript, Jest & NestJS
Solid NodeJS with TypeScript, Jest & NestJS
 
Typescript Fundamentals
Typescript FundamentalsTypescript Fundamentals
Typescript Fundamentals
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
interface in c#
interface in c#interface in c#
interface in c#
 
Introduction To Dotnet
Introduction To DotnetIntroduction To Dotnet
Introduction To Dotnet
 
C# Tutorial
C# Tutorial C# Tutorial
C# Tutorial
 
Linq to sql
Linq to sqlLinq to sql
Linq to sql
 
Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 
SOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principlesSOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principles
 
Dependency Inversion Principle
Dependency Inversion PrincipleDependency Inversion Principle
Dependency Inversion Principle
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 

Viewers also liked

Dependency injection
Dependency injectionDependency injection
Dependency injectionhousecor
 
Dependency Injection and Autofac
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofacmeghantaylor
 
Dependency Injection in .NET
Dependency Injection in .NETDependency Injection in .NET
Dependency Injection in .NETRemik Koczapski
 
Il pattern mvvm come strutturare al meglio il vostro progetto
Il pattern mvvm come strutturare al meglio il vostro progettoIl pattern mvvm come strutturare al meglio il vostro progetto
Il pattern mvvm come strutturare al meglio il vostro progettoMicrosoft Mobile Developer
 
Spring: usarlo conviene, ma usalo bene!
Spring: usarlo conviene, ma usalo bene!Spring: usarlo conviene, ma usalo bene!
Spring: usarlo conviene, ma usalo bene!benfante
 
Inversion of Control @ CD2008
Inversion of Control @ CD2008Inversion of Control @ CD2008
Inversion of Control @ CD2008Mauro Servienti
 
Workshop - cqrs brief introduction
Workshop - cqrs brief introductionWorkshop - cqrs brief introduction
Workshop - cqrs brief introductionFrancesco Garavaglia
 
Design Pattern Architetturali - Dependency Injection
Design Pattern Architetturali - Dependency InjectionDesign Pattern Architetturali - Dependency Injection
Design Pattern Architetturali - Dependency InjectionRiccardo Cardin
 

Viewers also liked (12)

Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Dependency injection
Dependency injectionDependency injection
Dependency injection
 
Dependency Injection and Autofac
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofac
 
Dependency Injection in .NET
Dependency Injection in .NETDependency Injection in .NET
Dependency Injection in .NET
 
Il pattern mvvm come strutturare al meglio il vostro progetto
Il pattern mvvm come strutturare al meglio il vostro progettoIl pattern mvvm come strutturare al meglio il vostro progetto
Il pattern mvvm come strutturare al meglio il vostro progetto
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Spring: usarlo conviene, ma usalo bene!
Spring: usarlo conviene, ma usalo bene!Spring: usarlo conviene, ma usalo bene!
Spring: usarlo conviene, ma usalo bene!
 
Inversion of Control @ CD2008
Inversion of Control @ CD2008Inversion of Control @ CD2008
Inversion of Control @ CD2008
 
IOC in Unity
IOC in Unity  IOC in Unity
IOC in Unity
 
IOC in unity
IOC in unityIOC in unity
IOC in unity
 
Workshop - cqrs brief introduction
Workshop - cqrs brief introductionWorkshop - cqrs brief introduction
Workshop - cqrs brief introduction
 
Design Pattern Architetturali - Dependency Injection
Design Pattern Architetturali - Dependency InjectionDesign Pattern Architetturali - Dependency Injection
Design Pattern Architetturali - Dependency Injection
 

Similar to Dependency injection and inversion

Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring FrameworkASG
 
Test Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, MockingTest Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, Mockingmrjawright
 
Dependency Injection & IoC
Dependency Injection & IoCDependency Injection & IoC
Dependency Injection & IoCDennis Loktionov
 
Design patterns fast track
Design patterns fast trackDesign patterns fast track
Design patterns fast trackBinu Bhasuran
 
Dependency Injection in .NET
Dependency Injection in .NETDependency Injection in .NET
Dependency Injection in .NETssusere19c741
 
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy
 
20080531 Intro To Dependency Injection & Inversion Of Control
20080531 Intro To Dependency Injection & Inversion Of Control20080531 Intro To Dependency Injection & Inversion Of Control
20080531 Intro To Dependency Injection & Inversion Of Controldonnfelker
 
Dev ops and safety critical systems
Dev ops and safety critical systemsDev ops and safety critical systems
Dev ops and safety critical systemsLen Bass
 
Mock Objects, Design and Dependency Inversion Principle
Mock Objects, Design and Dependency Inversion PrincipleMock Objects, Design and Dependency Inversion Principle
Mock Objects, Design and Dependency Inversion PrincipleP Heinonen
 
Software Engineering Past Papers (Short Questions)
Software Engineering Past Papers (Short Questions)Software Engineering Past Papers (Short Questions)
Software Engineering Past Papers (Short Questions)MuhammadTalha436
 
Mobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesMobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesQamar Abbas
 
Over view of software artitecture
Over view of software artitectureOver view of software artitecture
Over view of software artitectureABDEL RAHMAN KARIM
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing FundamentalsRichard Paul
 
Take Your Web Development To The Next Level With These Top 2 Libraries
Take Your Web Development To The Next Level With These Top 2 LibrariesTake Your Web Development To The Next Level With These Top 2 Libraries
Take Your Web Development To The Next Level With These Top 2 LibrariesPolyxer Systems
 
I gotta dependency on dependency injection
I gotta dependency on dependency injectionI gotta dependency on dependency injection
I gotta dependency on dependency injectionmhenroid
 
SOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionSOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionKnoldus Inc.
 

Similar to Dependency injection and inversion (20)

Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Test Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, MockingTest Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, Mocking
 
Dependency Injection & IoC
Dependency Injection & IoCDependency Injection & IoC
Dependency Injection & IoC
 
Design patterns fast track
Design patterns fast trackDesign patterns fast track
Design patterns fast track
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Dependency Injection in .NET
Dependency Injection in .NETDependency Injection in .NET
Dependency Injection in .NET
 
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
 
Swiz DAO
Swiz DAOSwiz DAO
Swiz DAO
 
20080531 Intro To Dependency Injection & Inversion Of Control
20080531 Intro To Dependency Injection & Inversion Of Control20080531 Intro To Dependency Injection & Inversion Of Control
20080531 Intro To Dependency Injection & Inversion Of Control
 
Dev ops and safety critical systems
Dev ops and safety critical systemsDev ops and safety critical systems
Dev ops and safety critical systems
 
Mock Objects, Design and Dependency Inversion Principle
Mock Objects, Design and Dependency Inversion PrincipleMock Objects, Design and Dependency Inversion Principle
Mock Objects, Design and Dependency Inversion Principle
 
Integration testing.
Integration testing. Integration testing.
Integration testing.
 
Software Engineering Past Papers (Short Questions)
Software Engineering Past Papers (Short Questions)Software Engineering Past Papers (Short Questions)
Software Engineering Past Papers (Short Questions)
 
Mobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesMobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelines
 
Over view of software artitecture
Over view of software artitectureOver view of software artitecture
Over view of software artitecture
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
 
Take Your Web Development To The Next Level With These Top 2 Libraries
Take Your Web Development To The Next Level With These Top 2 LibrariesTake Your Web Development To The Next Level With These Top 2 Libraries
Take Your Web Development To The Next Level With These Top 2 Libraries
 
I gotta dependency on dependency injection
I gotta dependency on dependency injectionI gotta dependency on dependency injection
I gotta dependency on dependency injection
 
SOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionSOLID Design Principles for Test Automaion
SOLID Design Principles for Test Automaion
 

Recently uploaded

Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROmotivationalword821
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 

Recently uploaded (20)

Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTRO
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 

Dependency injection and inversion

  • 1. Dependency Injection What, Why and 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 class directly depends upon Database class.
  • 5. Loose Coupling CustomerRepository class depends upon IDatabase interface
  • 6. 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.
  • 7. 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.
  • 8. Tightly Coupled System Forcefully you have to write to Event Logs only.
  • 9. 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.
  • 10.  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..
  • 11. Constructor Injection Object of concrete class (INotify Type) will be passed into Constructor parameter Object of concrete class passed to Constructor of LogMonitor
  • 12. Method Injection Object of concrete class (INotify Type) will be passed into Method parameter Object of concrete class passed as Method parameter
  • 13. 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.
  • 14.  StructureMap  UnityContainer  Autofac  Ninject  Castle Windsor  Spring .Net Popular DI Containers For .NET
  • 15. DI Containers Example-Unity Container 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. `