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 (20)

PDF
How I ended up contributing to Magento core
Alessandro Ronchi
 
PPTX
Magento Technical guidelines
Elogic Magento Development
 
PDF
Chernivtsi Magento Meetup&Contribution day. Naida V.
Elogic Magento Development
 
PPTX
MidwestPHP - Getting Started with Magento 2
Mathew Beane
 
PPTX
API design best practices
Igor Miniailo
 
PPTX
Backward Compatibility Developer's Guide in Magento 2. #MM17CZ
Igor Miniailo
 
PDF
Magento 2 - Replacing God with Dependency Injection
John Knowles
 
ODP
Dependency injection explained (Zbigniew Lukasiak)
Ontico
 
PPTX
Backward Compatibility Developer's Guide in Magento 2
Igor Miniailo
 
PPTX
Valeriy Nayda - Best Practices in Magento 2. Based on Multi Source Inventory ...
Atwix
 
PDF
Magento 2 Backend Development Essentials
BarnyShergold1
 
PPTX
API Design Best Practices by Igor Miniailo
Magecom UK Limited
 
PPTX
Backward Compatibility Developer's Guide Webinar
Igor Miniailo
 
PPTX
Know Why Magento Development Services Are The Best For Ecommerce Business.pptx
Agento Support
 
PPTX
Multi-Source Inventory. Imagine. Las Vegas. 2018
Igor Miniailo
 
PDF
CQRS and Event-Sourcing in Magento2 by examples of MSI
Elogic Magento Development
 
PPTX
CQRS and Event-Sourcing in Magento2 by examples of MSI
Igor Miniailo
 
PDF
Fundamentals of Extending Magento 2 - php[world] 2015
David Alger
 
PDF
Intro to Magento 2: Let's build a Module!
David Stillson
 
PDF
Oleksii Korshenko - Magento 2 Backwards Compatible Policy
Meet Magento Italy
 
How I ended up contributing to Magento core
Alessandro Ronchi
 
Magento Technical guidelines
Elogic Magento Development
 
Chernivtsi Magento Meetup&Contribution day. Naida V.
Elogic Magento Development
 
MidwestPHP - Getting Started with Magento 2
Mathew Beane
 
API design best practices
Igor Miniailo
 
Backward Compatibility Developer's Guide in Magento 2. #MM17CZ
Igor Miniailo
 
Magento 2 - Replacing God with Dependency Injection
John Knowles
 
Dependency injection explained (Zbigniew Lukasiak)
Ontico
 
Backward Compatibility Developer's Guide in Magento 2
Igor Miniailo
 
Valeriy Nayda - Best Practices in Magento 2. Based on Multi Source Inventory ...
Atwix
 
Magento 2 Backend Development Essentials
BarnyShergold1
 
API Design Best Practices by Igor Miniailo
Magecom UK Limited
 
Backward Compatibility Developer's Guide Webinar
Igor Miniailo
 
Know Why Magento Development Services Are The Best For Ecommerce Business.pptx
Agento Support
 
Multi-Source Inventory. Imagine. Las Vegas. 2018
Igor Miniailo
 
CQRS and Event-Sourcing in Magento2 by examples of MSI
Elogic Magento Development
 
CQRS and Event-Sourcing in Magento2 by examples of MSI
Igor Miniailo
 
Fundamentals of Extending Magento 2 - php[world] 2015
David Alger
 
Intro to Magento 2: Let's build a Module!
David Stillson
 
Oleksii Korshenko - Magento 2 Backwards Compatible Policy
Meet Magento Italy
 

More from Abid Malik (6)

PDF
Composer Best Practices
Abid Malik
 
PDF
Introduction to Xdebug
Abid Malik
 
PDF
Magento Docker Setup.pdf
Abid Malik
 
PDF
IDE and Toolset For Magento Development
Abid Malik
 
PDF
Introduction to Magento PWA
Abid Malik
 
PPTX
Magento Overview
Abid Malik
 
Composer Best Practices
Abid Malik
 
Introduction to Xdebug
Abid Malik
 
Magento Docker Setup.pdf
Abid Malik
 
IDE and Toolset For Magento Development
Abid Malik
 
Introduction to Magento PWA
Abid Malik
 
Magento Overview
Abid Malik
 
Ad

Recently uploaded (20)

PPTX
From spreadsheets and delays to real-time control
SatishKumar2651
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
ERP Consulting Services and Solutions by Contetra Pvt Ltd
jayjani123
 
PPTX
Prompt Like a Pro. Leveraging Salesforce Data to Power AI Workflows.pptx
Dele Amefo
 
PDF
Ready Layer One: Intro to the Model Context Protocol
mmckenna1
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PPTX
prodad heroglyph crack 2.0.214.2 Full Free Download
cracked shares
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PDF
Salesforce Experience Cloud Consultant.pdf
VALiNTRY360
 
PPTX
BB FlashBack Pro 5.61.0.4843 With Crack Free Download
cracked shares
 
PPTX
Get Started with Maestro: Agent, Robot, and Human in Action – Session 5 of 5
klpathrudu
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
Everything you need to know about pricing & licensing Microsoft 365 Copilot f...
Q-Advise
 
PDF
Is Framer the Future of AI Powered No-Code Development?
Isla Pandora
 
PDF
Best Web development company in india 2025
Greenusys
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PPTX
Smart Doctor Appointment Booking option in odoo.pptx
AxisTechnolabs
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
From spreadsheets and delays to real-time control
SatishKumar2651
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
ERP Consulting Services and Solutions by Contetra Pvt Ltd
jayjani123
 
Prompt Like a Pro. Leveraging Salesforce Data to Power AI Workflows.pptx
Dele Amefo
 
Ready Layer One: Intro to the Model Context Protocol
mmckenna1
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
prodad heroglyph crack 2.0.214.2 Full Free Download
cracked shares
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Salesforce Experience Cloud Consultant.pdf
VALiNTRY360
 
BB FlashBack Pro 5.61.0.4843 With Crack Free Download
cracked shares
 
Get Started with Maestro: Agent, Robot, and Human in Action – Session 5 of 5
klpathrudu
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Everything you need to know about pricing & licensing Microsoft 365 Copilot f...
Q-Advise
 
Is Framer the Future of AI Powered No-Code Development?
Isla Pandora
 
Best Web development company in india 2025
Greenusys
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Smart Doctor Appointment Booking option in odoo.pptx
AxisTechnolabs
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Ad

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. [email protected]
  • 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 [email protected]