SlideShare a Scribd company logo
Dependency injection
THIS TALK IS LOOSELY COUPLED
Lets face it…
The OOP Family
OOP
Objects
More…
IoC
Event loops
Schedulers
More..
Dependency inversion
Dependency injection More..
What?
Dependency Object
Injection
Object to
Client
What?
public class ExampleClass
{
private Logger logger;
public ExampleClass()
{
this.logger = new Logger();
this.logger.Log(“Constructor call”);
}
}
public class ExampleClass
{
private ILogger logger;
public ExampleClass(ILogger logger)
{
this.logger = logger;
if (logger == null)
{
throw new ArgumentNullException(“logger”);
}
this.logger.Log(“Constructor call”);
}
}
Original What we aim for
Why?
Problem
Constraints
Updating ServiceA ,need to update ClassA.
Need implementation in compile time.
Not easily testable
Repetitive code for creation.
Tightly coupled
ClassA
SerivceA
ServiceB
Uses
Benefits
Benefit Description
Late Binding Services can be swapped depending on needs.
Extensibility Code can be extended and reused.
Parallel development Code can be developed in parallel.
Maintainability Clearly defined classes are easy to maintain.
Testability Unit testing is possible.
Dependency inversion principle (SOLID)
A. High-level modules should not depend on low-level modules. Both should depend on abstractions.
B. Abstractions should not depend on details. Details should depend on abstractions.
Example Dependency inversion
Dependency Injection implementations
Constructor injection
Method injection
Property injection
Constructor injection
This is the most common DI.
Injection DEPENDENCY through the class’s constructor when instantiating that class.
Should be used when the injected dependency is required for the class to function.
Solves most INJECTION goals
Method injection
Inject the dependency into a single method.
Whole class does not
Generally uncommon, usually used for edge cases.
public class ContactManager : IContactManager
{
….
public bool Save(IContactPersistence contactDatabaseService,
IContact contact)
{
if (logger == null)
{
throw new ArgumentNullException(“logger”);
}
…. // Additional business logic executed before calling the save
return contactDatabaseService.Save(contact);
}
}
Property injection
Also called Setter injection.
Used when a class has optional dependencies(Swappable).
Requires NULL checking.
Does not require adding or modifying constructors.
Why Not?
Harder to read
More entities to manage
You need to ensure dependencies can be resolved.
Certain implementations can have performance drawbacks.

More Related Content

PDF
Angular 2 observables
PDF
Services in kubernetes-KnolX .pdf
PDF
Open Source and Secure Coding Practices
PDF
Benchmarking Service Meshes - CNCF Networking WG
ODP
Gatling
ODP
Boost your App with Gatling
PDF
Introduction to MDC Logging in Scala.pdf
PPT
Angular 2 observables
Services in kubernetes-KnolX .pdf
Open Source and Secure Coding Practices
Benchmarking Service Meshes - CNCF Networking WG
Gatling
Boost your App with Gatling
Introduction to MDC Logging in Scala.pdf

What's hot (20)

PPTX
Chain of Responsibility Pattern
PDF
Load test REST APIs using gatling
PDF
Unit Testing your React / Redux app (@BucharestJS)
PDF
Graphs: Fabric of DevOps
PDF
How to go about testing in React?
PDF
Karate - Web-Service API Testing Made Simple
PDF
Hidden Dragons of CGO
PPTX
Spring Testing Features
PDF
"Technical Challenges behind Visual IDE for React Components" Tetiana Mandziuk
PDF
CD using ArgoCD(KnolX).pdf
PDF
2016 07 - CloudBridge Python library (XSEDE16)
PPTX
Karate - MoT Dallas 26-Oct-2017
PDF
API Test Automation using Karate.pdf
PDF
Karate - powerful and simple framework for REST API automation testing
PPTX
Karate for Complex Web-Service API Testing by Peter Thomas
ODP
Gatling - Stress test tool
PDF
Istio: Using nginMesh as the service proxy
PDF
Building successful business Java apps: How to deliver more, code less, and c...
PDF
Javantura v4 - JVM++ The GraalVM - Martin Toshev
PPTX
Debugging Your Debugging Tools: What to do When Your Service Mesh Goes Down
Chain of Responsibility Pattern
Load test REST APIs using gatling
Unit Testing your React / Redux app (@BucharestJS)
Graphs: Fabric of DevOps
How to go about testing in React?
Karate - Web-Service API Testing Made Simple
Hidden Dragons of CGO
Spring Testing Features
"Technical Challenges behind Visual IDE for React Components" Tetiana Mandziuk
CD using ArgoCD(KnolX).pdf
2016 07 - CloudBridge Python library (XSEDE16)
Karate - MoT Dallas 26-Oct-2017
API Test Automation using Karate.pdf
Karate - powerful and simple framework for REST API automation testing
Karate for Complex Web-Service API Testing by Peter Thomas
Gatling - Stress test tool
Istio: Using nginMesh as the service proxy
Building successful business Java apps: How to deliver more, code less, and c...
Javantura v4 - JVM++ The GraalVM - Martin Toshev
Debugging Your Debugging Tools: What to do When Your Service Mesh Goes Down
Ad

Similar to Dependency injection (20)

PPTX
Dependency Injection або Don’t call me, I’ll call you
PPTX
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
PPTX
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
PPTX
I gotta dependency on dependency injection
PDF
Dependency injection for beginners
PPTX
Clean Code Part II - Dependency Injection at SoCal Code Camp
PPTX
Cut your Dependencies with Dependency Injection for East Bay.NET User Group
PPTX
Cut your Dependencies with - Dependency Injection for South Bay.NET User Grou...
PPTX
Clean Code II - Dependency Injection
PPTX
The Dependency Injection - Sorin Damian, Software Developer@RomSoft
PPTX
Dependency Inversion Principle
PPTX
Dependency injection with unity 2.0 Dmytro Mindra Lohika
PDF
Dependency Injection in iOS
PDF
DIC To The Limit – deSymfonyDay, Barcelona 2014
PDF
Dependency Injection
PPTX
Introduction to Dependency Injection
PDF
What is Dependency Injection in Spring Boot | Edureka
PPTX
Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp
PPT
Dependency injection
ODP
Dependency injection explained (Zbigniew Lukasiak)
Dependency Injection або Don’t call me, I’ll call you
Clean Code II - Dependency Injection at SoCal Code Camp San Diego (07/27/2013)
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
I gotta dependency on dependency injection
Dependency injection for beginners
Clean Code Part II - Dependency Injection at SoCal Code Camp
Cut your Dependencies with Dependency Injection for East Bay.NET User Group
Cut your Dependencies with - Dependency Injection for South Bay.NET User Grou...
Clean Code II - Dependency Injection
The Dependency Injection - Sorin Damian, Software Developer@RomSoft
Dependency Inversion Principle
Dependency injection with unity 2.0 Dmytro Mindra Lohika
Dependency Injection in iOS
DIC To The Limit – deSymfonyDay, Barcelona 2014
Dependency Injection
Introduction to Dependency Injection
What is Dependency Injection in Spring Boot | Edureka
Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp
Dependency injection
Dependency injection explained (Zbigniew Lukasiak)
Ad

Recently uploaded (20)

PDF
Web App vs Mobile App What Should You Build First.pdf
PPTX
TLE Review Electricity (Electricity).pptx
PDF
A novel scalable deep ensemble learning framework for big data classification...
PPT
What is a Computer? Input Devices /output devices
PPTX
OMC Textile Division Presentation 2021.pptx
PPTX
observCloud-Native Containerability and monitoring.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
A comparative study of natural language inference in Swahili using monolingua...
PPTX
Tartificialntelligence_presentation.pptx
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Zenith AI: Advanced Artificial Intelligence
PPTX
The various Industrial Revolutions .pptx
PDF
Getting Started with Data Integration: FME Form 101
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PPTX
1. Introduction to Computer Programming.pptx
Web App vs Mobile App What Should You Build First.pdf
TLE Review Electricity (Electricity).pptx
A novel scalable deep ensemble learning framework for big data classification...
What is a Computer? Input Devices /output devices
OMC Textile Division Presentation 2021.pptx
observCloud-Native Containerability and monitoring.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Assigned Numbers - 2025 - Bluetooth® Document
A contest of sentiment analysis: k-nearest neighbor versus neural network
A comparative study of natural language inference in Swahili using monolingua...
Tartificialntelligence_presentation.pptx
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Zenith AI: Advanced Artificial Intelligence
The various Industrial Revolutions .pptx
Getting Started with Data Integration: FME Form 101
Univ-Connecticut-ChatGPT-Presentaion.pdf
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
1. Introduction to Computer Programming.pptx

Dependency injection

  • 1. Dependency injection THIS TALK IS LOOSELY COUPLED
  • 3. The OOP Family OOP Objects More… IoC Event loops Schedulers More.. Dependency inversion Dependency injection More..
  • 5. What? public class ExampleClass { private Logger logger; public ExampleClass() { this.logger = new Logger(); this.logger.Log(“Constructor call”); } } public class ExampleClass { private ILogger logger; public ExampleClass(ILogger logger) { this.logger = logger; if (logger == null) { throw new ArgumentNullException(“logger”); } this.logger.Log(“Constructor call”); } } Original What we aim for
  • 6. Why? Problem Constraints Updating ServiceA ,need to update ClassA. Need implementation in compile time. Not easily testable Repetitive code for creation. Tightly coupled ClassA SerivceA ServiceB Uses
  • 7. Benefits Benefit Description Late Binding Services can be swapped depending on needs. Extensibility Code can be extended and reused. Parallel development Code can be developed in parallel. Maintainability Clearly defined classes are easy to maintain. Testability Unit testing is possible.
  • 8. Dependency inversion principle (SOLID) A. High-level modules should not depend on low-level modules. Both should depend on abstractions. B. Abstractions should not depend on details. Details should depend on abstractions.
  • 10. Dependency Injection implementations Constructor injection Method injection Property injection
  • 11. Constructor injection This is the most common DI. Injection DEPENDENCY through the class’s constructor when instantiating that class. Should be used when the injected dependency is required for the class to function. Solves most INJECTION goals
  • 12. Method injection Inject the dependency into a single method. Whole class does not Generally uncommon, usually used for edge cases. public class ContactManager : IContactManager { …. public bool Save(IContactPersistence contactDatabaseService, IContact contact) { if (logger == null) { throw new ArgumentNullException(“logger”); } …. // Additional business logic executed before calling the save return contactDatabaseService.Save(contact); } }
  • 13. Property injection Also called Setter injection. Used when a class has optional dependencies(Swappable). Requires NULL checking. Does not require adding or modifying constructors.
  • 14. Why Not? Harder to read More entities to manage You need to ensure dependencies can be resolved. Certain implementations can have performance drawbacks.