SlideShare a Scribd company logo
Magento Basics -
Dependency Injection
Abid H. Malik • 22.06.2023
Abid H. Malik
★ 20+ years experience in Software
Industry.
★ 13 Years experience in Magento
and other ecommerce platforms.
★ 6+ Years as corporate trainer and
content developer.
malikabid2006@gmail.com
Agenda
1. Dependency Injection
2. Object Manager
3. di.xml
4. Advantages of DI
Dependency Injection
What is DI?
● A design pattern that allows an object to declare its
dependencies to an external object that supplies those
dependencies.
● Dependencies are declared as Interfaces.
● Dependencies are provided as concrete classes.
What is Object Manager
● A service class in Adobe commerce / magento.
● Instantiates objects.
● Uses class constructors and di.xml to retrieve
information about the objects to be instantiated.
● Classes should not depend on Object Manager.
● Custom factories are exceptions
Dependency Types
● Injectable (singletons)
● Newable / non-injectable
○ Obtained by creating new class instance every time
○ Factories.
Injection types
● Constructor Injection
● Method Injection
● Optional Dependencies
Injection types
DI Configuration (di.xml)
● Purpose
○ Configuring dependencies
○ Overriding classes
○ Creating plugins
○ Creating virtual sub-classes
○ Defining preferences:
● Initial (app/etc/di.xml)
● Global (<moduleDir>/etc/di.xml)
● Area-specific (<moduleDir>/etc/<area>/di.xml)
Benefits of DI
● Loose coupling of code
● Easy maintenance
● Focus on what a class does.
● Flexibility and scalability
Best Practices
● Avoid Circular dependencies.
● Make sure d.xml configuration are correct.
● Avoid tight coupling of classes
● Keep number of dependencies to a minimum.
● Use interfaces to define dependencies.
● Use area-specific di.xml files.
Thank you!
● Keep Smiling.
● Send your feedback to
malikabid2006@gmail.com

More Related Content

Similar to Magento 2: Dependency Injection

[DevDay2018] Liferay DXP – A powerful Enterprise Solution - By Vy Bui, Develo...
[DevDay2018] Liferay DXP – A powerful Enterprise Solution - By Vy Bui, Develo...[DevDay2018] Liferay DXP – A powerful Enterprise Solution - By Vy Bui, Develo...
[DevDay2018] Liferay DXP – A powerful Enterprise Solution - By Vy Bui, Develo...DevDay.org
 
Leveraging Dependency Injection(DI) in Universal Applications - Tamir Dresher
Leveraging Dependency Injection(DI) in Universal Applications -  Tamir DresherLeveraging Dependency Injection(DI) in Universal Applications -  Tamir Dresher
Leveraging Dependency Injection(DI) in Universal Applications - Tamir DresherTamir Dresher
 
Dependency injection: koin vs dagger
Dependency injection: koin vs daggerDependency injection: koin vs dagger
Dependency injection: koin vs daggerMatteo Pasotti
 
Establishing and analyzing traceability between artifacts
Establishing and analyzing traceability between artifactsEstablishing and analyzing traceability between artifacts
Establishing and analyzing traceability between artifactsIBM Rational software
 
Indore MuleSoft Meetup #5 April 2022 MDynamics 65.pptx
Indore MuleSoft Meetup #5 April 2022 MDynamics 65.pptxIndore MuleSoft Meetup #5 April 2022 MDynamics 65.pptx
Indore MuleSoft Meetup #5 April 2022 MDynamics 65.pptxIndoreMulesoftMeetup
 
Android Modularization
Android ModularizationAndroid Modularization
Android ModularizationYoung-Hyuk Yoo
 
From class to architecture
From class to architectureFrom class to architecture
From class to architectureMarcin Hawraniak
 
Evolving to Cloud-Native - Anand Rao
Evolving to Cloud-Native - Anand RaoEvolving to Cloud-Native - Anand Rao
Evolving to Cloud-Native - Anand RaoVMware Tanzu
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxanguraju1
 
Website-Development-Company-in-bangalore (1).pdf
Website-Development-Company-in-bangalore (1).pdfWebsite-Development-Company-in-bangalore (1).pdf
Website-Development-Company-in-bangalore (1).pdfBigwelt Technologies
 
Presenting Data – An Alternative to the View Control
Presenting Data – An Alternative to the View ControlPresenting Data – An Alternative to the View Control
Presenting Data – An Alternative to the View ControlTeamstudio
 
kranonit S04E02 Кирил Jstor: Hacking .NET applications
kranonit S04E02 Кирил Jstor: Hacking .NET applicationskranonit S04E02 Кирил Jstor: Hacking .NET applications
kranonit S04E02 Кирил Jstor: Hacking .NET applicationsKrivoy Rog IT Community
 
Modern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design PatternsModern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design PatternsVolodymyr Voytyshyn
 
Angular.js presentation
Angular.js presentationAngular.js presentation
Angular.js presentationMoran Fine
 
Towards a Modularity Maturity Model
Towards a Modularity Maturity ModelTowards a Modularity Maturity Model
Towards a Modularity Maturity ModelGraham Charters
 
QuestMark Framework for Dhis2 Android Apps - Dhis2 symposium 2018
QuestMark Framework for Dhis2 Android Apps - Dhis2 symposium 2018QuestMark Framework for Dhis2 Android Apps - Dhis2 symposium 2018
QuestMark Framework for Dhis2 Android Apps - Dhis2 symposium 2018Nacho Foche Pérez
 
Best Angular JS training in Hyderabad, India
Best Angular JS training in Hyderabad, IndiaBest Angular JS training in Hyderabad, India
Best Angular JS training in Hyderabad, IndiaN Benchmark IT Solutions
 
SF MUG - Find the Custom Object of Your Desires
SF MUG - Find the Custom Object of Your DesiresSF MUG - Find the Custom Object of Your Desires
SF MUG - Find the Custom Object of Your DesiresJasmineChung11
 

Similar to Magento 2: Dependency Injection (20)

[DevDay2018] Liferay DXP – A powerful Enterprise Solution - By Vy Bui, Develo...
[DevDay2018] Liferay DXP – A powerful Enterprise Solution - By Vy Bui, Develo...[DevDay2018] Liferay DXP – A powerful Enterprise Solution - By Vy Bui, Develo...
[DevDay2018] Liferay DXP – A powerful Enterprise Solution - By Vy Bui, Develo...
 
Leveraging Dependency Injection(DI) in Universal Applications - Tamir Dresher
Leveraging Dependency Injection(DI) in Universal Applications -  Tamir DresherLeveraging Dependency Injection(DI) in Universal Applications -  Tamir Dresher
Leveraging Dependency Injection(DI) in Universal Applications - Tamir Dresher
 
Dependency injection: koin vs dagger
Dependency injection: koin vs daggerDependency injection: koin vs dagger
Dependency injection: koin vs dagger
 
Establishing and analyzing traceability between artifacts
Establishing and analyzing traceability between artifactsEstablishing and analyzing traceability between artifacts
Establishing and analyzing traceability between artifacts
 
Indore MuleSoft Meetup #5 April 2022 MDynamics 65.pptx
Indore MuleSoft Meetup #5 April 2022 MDynamics 65.pptxIndore MuleSoft Meetup #5 April 2022 MDynamics 65.pptx
Indore MuleSoft Meetup #5 April 2022 MDynamics 65.pptx
 
Android Modularization
Android ModularizationAndroid Modularization
Android Modularization
 
From class to architecture
From class to architectureFrom class to architecture
From class to architecture
 
Evolving to Cloud-Native - Anand Rao
Evolving to Cloud-Native - Anand RaoEvolving to Cloud-Native - Anand Rao
Evolving to Cloud-Native - Anand Rao
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
Website-Development-Company-in-bangalore (1).pdf
Website-Development-Company-in-bangalore (1).pdfWebsite-Development-Company-in-bangalore (1).pdf
Website-Development-Company-in-bangalore (1).pdf
 
Presenting Data – An Alternative to the View Control
Presenting Data – An Alternative to the View ControlPresenting Data – An Alternative to the View Control
Presenting Data – An Alternative to the View Control
 
kranonit S04E02 Кирил Jstor: Hacking .NET applications
kranonit S04E02 Кирил Jstor: Hacking .NET applicationskranonit S04E02 Кирил Jstor: Hacking .NET applications
kranonit S04E02 Кирил Jstor: Hacking .NET applications
 
Modern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design PatternsModern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design Patterns
 
Angular.js presentation
Angular.js presentationAngular.js presentation
Angular.js presentation
 
What's new in Angular 2?
What's new in Angular 2?What's new in Angular 2?
What's new in Angular 2?
 
Towards a Modularity Maturity Model
Towards a Modularity Maturity ModelTowards a Modularity Maturity Model
Towards a Modularity Maturity Model
 
QuestMark Framework for Dhis2 Android Apps - Dhis2 symposium 2018
QuestMark Framework for Dhis2 Android Apps - Dhis2 symposium 2018QuestMark Framework for Dhis2 Android Apps - Dhis2 symposium 2018
QuestMark Framework for Dhis2 Android Apps - Dhis2 symposium 2018
 
Best Angular JS training in Hyderabad, India
Best Angular JS training in Hyderabad, IndiaBest Angular JS training in Hyderabad, India
Best Angular JS training in Hyderabad, India
 
SF MUG - Find the Custom Object of Your Desires
SF MUG - Find the Custom Object of Your DesiresSF MUG - Find the Custom Object of Your Desires
SF MUG - Find the Custom Object of Your Desires
 
DDD with Behat
DDD with BehatDDD with Behat
DDD with Behat
 

More from Abid Malik

Composer Best Practices
Composer Best PracticesComposer Best Practices
Composer Best PracticesAbid Malik
 
Introduction to Xdebug
Introduction to XdebugIntroduction to Xdebug
Introduction to XdebugAbid Malik
 
Magento Docker Setup.pdf
Magento Docker Setup.pdfMagento Docker Setup.pdf
Magento Docker Setup.pdfAbid Malik
 
IDE and Toolset For Magento Development
IDE and Toolset For Magento DevelopmentIDE and Toolset For Magento Development
IDE and Toolset For Magento DevelopmentAbid Malik
 
Introduction to Magento PWA
Introduction to Magento PWAIntroduction to Magento PWA
Introduction to Magento PWAAbid Malik
 
Magento Overview
Magento  OverviewMagento  Overview
Magento OverviewAbid Malik
 

More from Abid Malik (6)

Composer Best Practices
Composer Best PracticesComposer Best Practices
Composer Best Practices
 
Introduction to Xdebug
Introduction to XdebugIntroduction to Xdebug
Introduction to Xdebug
 
Magento Docker Setup.pdf
Magento Docker Setup.pdfMagento Docker Setup.pdf
Magento Docker Setup.pdf
 
IDE and Toolset For Magento Development
IDE and Toolset For Magento DevelopmentIDE and Toolset For Magento Development
IDE and Toolset For Magento Development
 
Introduction to Magento PWA
Introduction to Magento PWAIntroduction to Magento PWA
Introduction to Magento PWA
 
Magento Overview
Magento  OverviewMagento  Overview
Magento Overview
 

Recently uploaded

Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion Clinic
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAlluxio, Inc.
 
iGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockiGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockSkilrock Technologies
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfkalichargn70th171
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareinfo611746
 
A Guideline to Gorgias to to Re:amaze Data Migration
A Guideline to Gorgias to to Re:amaze Data MigrationA Guideline to Gorgias to to Re:amaze Data Migration
A Guideline to Gorgias to to Re:amaze Data MigrationHelp Desk Migration
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Andrea Goulet
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAlluxio, Inc.
 
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Gáspár Nagy
 
APVP,apvp apvp High quality supplier safe spot transport, 98% purity
APVP,apvp apvp High quality supplier safe spot transport, 98% purityAPVP,apvp apvp High quality supplier safe spot transport, 98% purity
APVP,apvp apvp High quality supplier safe spot transport, 98% purityamy56318795
 
INGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by DesignINGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by DesignNeo4j
 
Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationWave PLM
 
Workforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdfWorkforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdfDeskTrack
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?XfilesPro
 
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdfImplementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdfVictor Lopez
 
How to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabberHow to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabbereGrabber
 
10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdf10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdfkalichargn70th171
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Krakówbim.edu.pl
 

Recently uploaded (20)

Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
iGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockiGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by Skilrock
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024
 
A Guideline to Gorgias to to Re:amaze Data Migration
A Guideline to Gorgias to to Re:amaze Data MigrationA Guideline to Gorgias to to Re:amaze Data Migration
A Guideline to Gorgias to to Re:amaze Data Migration
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
 
APVP,apvp apvp High quality supplier safe spot transport, 98% purity
APVP,apvp apvp High quality supplier safe spot transport, 98% purityAPVP,apvp apvp High quality supplier safe spot transport, 98% purity
APVP,apvp apvp High quality supplier safe spot transport, 98% purity
 
INGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by DesignINGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by Design
 
Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM Integration
 
Workforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdfWorkforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdf
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdfImplementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
 
How to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabberHow to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabber
 
10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdf10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdf
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 

Magento 2: Dependency Injection

  • 1. Magento Basics - Dependency Injection Abid H. Malik • 22.06.2023
  • 2. Abid H. Malik ★ 20+ years experience in Software Industry. ★ 13 Years experience in Magento and other ecommerce platforms. ★ 6+ Years as corporate trainer and content developer. malikabid2006@gmail.com
  • 3. Agenda 1. Dependency Injection 2. Object Manager 3. di.xml 4. Advantages of DI Dependency Injection
  • 4. What is DI? ● A design pattern that allows an object to declare its dependencies to an external object that supplies those dependencies. ● Dependencies are declared as Interfaces. ● Dependencies are provided as concrete classes.
  • 5. What is Object Manager ● A service class in Adobe commerce / magento. ● Instantiates objects. ● Uses class constructors and di.xml to retrieve information about the objects to be instantiated. ● Classes should not depend on Object Manager. ● Custom factories are exceptions
  • 6. Dependency Types ● Injectable (singletons) ● Newable / non-injectable ○ Obtained by creating new class instance every time ○ Factories.
  • 7. Injection types ● Constructor Injection ● Method Injection ● Optional Dependencies
  • 9. DI Configuration (di.xml) ● Purpose ○ Configuring dependencies ○ Overriding classes ○ Creating plugins ○ Creating virtual sub-classes ○ Defining preferences: ● Initial (app/etc/di.xml) ● Global (<moduleDir>/etc/di.xml) ● Area-specific (<moduleDir>/etc/<area>/di.xml)
  • 10. Benefits of DI ● Loose coupling of code ● Easy maintenance ● Focus on what a class does. ● Flexibility and scalability
  • 11. Best Practices ● Avoid Circular dependencies. ● Make sure d.xml configuration are correct. ● Avoid tight coupling of classes ● Keep number of dependencies to a minimum. ● Use interfaces to define dependencies. ● Use area-specific di.xml files.
  • 12. Thank you! ● Keep Smiling. ● Send your feedback to malikabid2006@gmail.com