SlideShare a Scribd company logo
Juhi Rathi
https://www.drupal.org/u/juhi-rathi
Dependency Injection
● What is Dependency Injection
● Why Dependency Injection
● Pros of DI
● Types for Dependency Injection
● DI in Drupal 8
● How and where does it all happen
● Implementation in Drupal 8
Agenda
What is Dependency Injection
In simple terms, Dependency Injection is a design pattern that
helps avoid hard-coded dependencies for some piece of code or
software.
The dependencies can be changed at run time as well as compile
time. We can use Dependency Injection to write modular,
testable and maintainable code
Dependency Injection in Drupal 8
Why Dependency Injection
Pros of DI
● Decoupling
● Cleaner Code
● Reusable & flexible code
● Testable code
● Constructor Injection
● Setter Injection
● Interface Injection
Types of Dependency Injection
Constructor Injection
● If the dependency is required by the class and cannot work
without it, by using constructor injection we guarantee that
the required dependencies are present.
● Since the constructor is only ever called when instantiating
our object we can be sure that the dependency can’t be
changed or altered during the object lifetime.
Constructor Injection
/ Constructor
public function __construct(Service service) {
// Save the reference to the passed-in service inside this client
this.service = service;
}
Constructor Injection Drawback
These above two points make Constructor Injection extremely
useful, however there is also a few drawbacks that make it
unsuitable for all scenarios:
● Since all dependencies are required, it’s not suitable when
optional dependencies are needed.
● While using class inheritance trying to extend and override
the constructor becomes difficult.
Setter Injection
● With Setter Injection the dependencies are provided to our
class after it has been instantiated using setter methods.
● Allows for optional dependencies and adding new
dependencies is as easy as adding a new setter method.
// Setter method
public void setService(Service service) {
// Save the reference to the passed-in service inside this client
this.service = service;
}
Interface Injection
The dependency provides an injector method that will inject the
dependency into any client passed to it. Clients must implement an
interface that exposes a setter method that accepts the dependency.
// Service setter interface.
public interface ServiceSetter {
public void setService(Service service);
}
DI in Drupal 8
● Drupal 8 introduces the concept of services to decouple
reusable functionality and makes these services pluggable
and replaceable by registering them with a service
container.
● Dependency injection is the preferred method for accessing
and using services in Drupal 8 and should be used whenever
possible. Rather than calling out to the global services
container, services are instead passed as arguments to a
constructor or injected via setter methods.
How and where does it all happen
Drupal Kernel: core/lib/Drupal/Core/DrupalKernel.php
Core Services are defined in: core/core.services.yml
All symfony dependencies related data added in :
core/lib/Drupal/Core/DependencyInjection/..
Implementation in Drupal 8
Dependency Injection in Drupal 8
Reference URLs
To get all service list :
https://api.drupal.org/api/drupal/core%21core.services.yml/8.2.x
To search any service:
https://api.drupal.org/api/drupal/services
For Concepts :
https://www.youtube.com/watch?v=0SdBVmxuCZg
THANKS!
QUESTIONS?

More Related Content

PDF
Building blocks of Angular
PDF
Dagger2 Intro
PPTX
Android Dagger2
PPTX
Android Dagger 2
PPTX
Dependency injection using dagger2
PPTX
Spring Testing Features
PPTX
Dependency injection and inversion
PPTX
Consumer-driven contracts: avoid microservices integration hell! (LondonCD - ...
Building blocks of Angular
Dagger2 Intro
Android Dagger2
Android Dagger 2
Dependency injection using dagger2
Spring Testing Features
Dependency injection and inversion
Consumer-driven contracts: avoid microservices integration hell! (LondonCD - ...

What's hot (15)

PDF
User Forms & API integration
PPTX
Implementing the Adapter Design Pattern
PPTX
Dagger2
PPTX
Adapter Design Pattern
PDF
Microservices: Consumer Driven Contracts in Practice
PPTX
Consumer-driven contracts with Pact and PHP
PDF
Model View Presenter
PPTX
AngularJS: Service, factory & provider
PPTX
Dependency injection: koin vs dagger
ODP
Routing & Navigating Pages in Angular 2
PPTX
Consumer Driven Contracts for microservices
PPTX
Dependency injection and Why It Matters to Testers
PDF
Consumer contract testing
PDF
Dependency injection for beginners
PDF
User Forms & API integration
Implementing the Adapter Design Pattern
Dagger2
Adapter Design Pattern
Microservices: Consumer Driven Contracts in Practice
Consumer-driven contracts with Pact and PHP
Model View Presenter
AngularJS: Service, factory & provider
Dependency injection: koin vs dagger
Routing & Navigating Pages in Angular 2
Consumer Driven Contracts for microservices
Dependency injection and Why It Matters to Testers
Consumer contract testing
Dependency injection for beginners
Ad

Similar to Dependency Injection in Drupal 8 (20)

PPTX
Dependency injection in Drupal 8 : DrupalCon NOLA
PDF
Dependency injection Drupal Camp Wrocław 2014
PDF
DIC To The Limit – deSymfonyDay, Barcelona 2014
PDF
Dependency Injection in Drupal 8
PDF
Dependency injection in Drupal 8
ODP
Dependency injection explained (Zbigniew Lukasiak)
PDF
Final dependency presentation.odp
PPTX
Dependency injection with Symfony 2
PPT
Dependency injection with PHP
PDF
Drupal 8 Vocab Lesson
PPTX
Services, dependency injection and containers
ODP
Dependency Injection, Zend Framework and Symfony Container
PPTX
Dependency Inversion Principle
KEY
Zend Di in ZF 2.0
PDF
Dependency Injection
PDF
Dependency injection
PDF
What is Dependency Injection in Spring Boot | Edureka
PPT
Future Proofing Your Drupal Skills
PDF
Dependency Injection @ AngularJS
PDF
Dependency Injection
Dependency injection in Drupal 8 : DrupalCon NOLA
Dependency injection Drupal Camp Wrocław 2014
DIC To The Limit – deSymfonyDay, Barcelona 2014
Dependency Injection in Drupal 8
Dependency injection in Drupal 8
Dependency injection explained (Zbigniew Lukasiak)
Final dependency presentation.odp
Dependency injection with Symfony 2
Dependency injection with PHP
Drupal 8 Vocab Lesson
Services, dependency injection and containers
Dependency Injection, Zend Framework and Symfony Container
Dependency Inversion Principle
Zend Di in ZF 2.0
Dependency Injection
Dependency injection
What is Dependency Injection in Spring Boot | Edureka
Future Proofing Your Drupal Skills
Dependency Injection @ AngularJS
Dependency Injection
Ad

More from valuebound (20)

PDF
Scaling Drupal for High Traffic Websites
PDF
Drupal 7 to Drupal 10 Migration A Fintech Strategic Blueprint (1).pdf
PDF
How to Use DDEV to Streamline Your Drupal Development Process.
PDF
How to Use AWS to Automate Your IT Operation| Valuebound
PDF
How to Use Firebase to Send Push Notifications to React Native and Node.js Apps
PDF
Mastering Drupal Theming
PDF
The Benefits of Cloud Engineering
PDF
Cloud Computing
PDF
The Future of Cloud Engineering: Emerging Trends and Technologies to Watch in...
PDF
Deep dive into ChatGPT
PDF
Content Creation Solution | Valuebound
PPTX
Road ahead for Drupal 8 contributed projects
PPTX
Chatbot with RASA | Valuebound
PDF
Drupal and Artificial Intelligence for Personalization
PPTX
Drupal growth in last year | Valuebound
PPTX
BE NEW TO THE WORLD "BRAVE FROM CHROME"
PPTX
Event loop in browser
PPTX
The Basics of MongoDB
PPTX
React JS: A Secret Preview
PPTX
An Overview of Field Collection Views Module
Scaling Drupal for High Traffic Websites
Drupal 7 to Drupal 10 Migration A Fintech Strategic Blueprint (1).pdf
How to Use DDEV to Streamline Your Drupal Development Process.
How to Use AWS to Automate Your IT Operation| Valuebound
How to Use Firebase to Send Push Notifications to React Native and Node.js Apps
Mastering Drupal Theming
The Benefits of Cloud Engineering
Cloud Computing
The Future of Cloud Engineering: Emerging Trends and Technologies to Watch in...
Deep dive into ChatGPT
Content Creation Solution | Valuebound
Road ahead for Drupal 8 contributed projects
Chatbot with RASA | Valuebound
Drupal and Artificial Intelligence for Personalization
Drupal growth in last year | Valuebound
BE NEW TO THE WORLD "BRAVE FROM CHROME"
Event loop in browser
The Basics of MongoDB
React JS: A Secret Preview
An Overview of Field Collection Views Module

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Big Data Technologies - Introduction.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPT
Teaching material agriculture food technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
KodekX | Application Modernization Development
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Network Security Unit 5.pdf for BCA BBA.
NewMind AI Weekly Chronicles - August'25 Week I
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Big Data Technologies - Introduction.pptx
The AUB Centre for AI in Media Proposal.docx
Unlocking AI with Model Context Protocol (MCP)
Teaching material agriculture food technology
Encapsulation_ Review paper, used for researhc scholars
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Mobile App Security Testing_ A Comprehensive Guide.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
KodekX | Application Modernization Development
MYSQL Presentation for SQL database connectivity
20250228 LYD VKU AI Blended-Learning.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
How UI/UX Design Impacts User Retention in Mobile Apps.pdf

Dependency Injection in Drupal 8

  • 2. ● What is Dependency Injection ● Why Dependency Injection ● Pros of DI ● Types for Dependency Injection ● DI in Drupal 8 ● How and where does it all happen ● Implementation in Drupal 8 Agenda
  • 3. What is Dependency Injection In simple terms, Dependency Injection is a design pattern that helps avoid hard-coded dependencies for some piece of code or software. The dependencies can be changed at run time as well as compile time. We can use Dependency Injection to write modular, testable and maintainable code
  • 6. Pros of DI ● Decoupling ● Cleaner Code ● Reusable & flexible code ● Testable code
  • 7. ● Constructor Injection ● Setter Injection ● Interface Injection Types of Dependency Injection
  • 8. Constructor Injection ● If the dependency is required by the class and cannot work without it, by using constructor injection we guarantee that the required dependencies are present. ● Since the constructor is only ever called when instantiating our object we can be sure that the dependency can’t be changed or altered during the object lifetime.
  • 9. Constructor Injection / Constructor public function __construct(Service service) { // Save the reference to the passed-in service inside this client this.service = service; }
  • 10. Constructor Injection Drawback These above two points make Constructor Injection extremely useful, however there is also a few drawbacks that make it unsuitable for all scenarios: ● Since all dependencies are required, it’s not suitable when optional dependencies are needed. ● While using class inheritance trying to extend and override the constructor becomes difficult.
  • 11. Setter Injection ● With Setter Injection the dependencies are provided to our class after it has been instantiated using setter methods. ● Allows for optional dependencies and adding new dependencies is as easy as adding a new setter method. // Setter method public void setService(Service service) { // Save the reference to the passed-in service inside this client this.service = service; }
  • 12. Interface Injection The dependency provides an injector method that will inject the dependency into any client passed to it. Clients must implement an interface that exposes a setter method that accepts the dependency. // Service setter interface. public interface ServiceSetter { public void setService(Service service); }
  • 13. DI in Drupal 8 ● Drupal 8 introduces the concept of services to decouple reusable functionality and makes these services pluggable and replaceable by registering them with a service container. ● Dependency injection is the preferred method for accessing and using services in Drupal 8 and should be used whenever possible. Rather than calling out to the global services container, services are instead passed as arguments to a constructor or injected via setter methods.
  • 14. How and where does it all happen Drupal Kernel: core/lib/Drupal/Core/DrupalKernel.php Core Services are defined in: core/core.services.yml All symfony dependencies related data added in : core/lib/Drupal/Core/DependencyInjection/..
  • 17. Reference URLs To get all service list : https://api.drupal.org/api/drupal/core%21core.services.yml/8.2.x To search any service: https://api.drupal.org/api/drupal/services For Concepts : https://www.youtube.com/watch?v=0SdBVmxuCZg