SlideShare una empresa de Scribd logo
1 de 41
#MM18PL
Dependency Injection Extended
The Way to advanced Magento 2 Development
September 10th 2018 – Meet Magento PL
Andreas von Studnitz – integer_net – @avstudnitz
#MM18PL#MM18PL
Andreas von Studnitz
Co-Founder and Managing Director @ integer_net
Magento Developer, Trainer, Consultant
since 2008
Twitter: @avstudnitz
#MM18PL#MM18PL
Purpose of DI
#MM18PL#MM18PL
All dependencies in one place
Make testing easier
Better readable code
#MM18PL#MM18PL
Basics
#MM18PL#MM18PL
Constructor Injection
#MM18PL#MM18PL
How does it work?
#MM18PL#MM18PL
Classes are not instantiated
with new() in Magento 2
#MM18PL#MM18PL
The Object Manager is
used instead
#MM18PL#MM18PL
Instantiate a new class (1)
Does an object of that type
already exist?
Reuse existing object Create new object
Yes No
Get type from constructor parameter
#MM18PL#MM18PL
Constructor Injection
#MM18PL#MM18PL
The next Steps
#MM18PL#MM18PL
Injecting an Interface?
#MM18PL#MM18PL
Injecting an Interface?
#MM18PL#MM18PL
Define a Preference!
<module_directory>/etc/di.xml
#MM18PL#MM18PL
Instantiate a new class (2)
Get type from constructor parameter
Does an object of that type
already exist?
Reuse existing object Create new object
Yes No
Does a preference exist?
Yes NoUse preference
as type
#MM18PL#MM18PL
Arguments
#MM18PL#MM18PL
Scalar Arguments
#MM18PL#MM18PL
Scalar Arguments
<module_directory>/etc/di.xml
#MM18PL#MM18PL
Basic Argument Types
string
number
boolean
array
null
#MM18PL#MM18PL
Advanced Argument Types
const
object
<module_directory>/etc/di.xml
<module_directory>/etc/adminhtml/di.xml
#MM18PL#MM18PL
A typical Usecase
#MM18PL#MM18PL
A typical Usecase
<module_directory>/etc/di.xml
#MM18PL#MM18PL
A typical Usecase
<module_directory>/etc/di.xml
#MM18PL#MM18PL
A typical Usecase
#MM18PL#MM18PL
Virtual Types
#MM18PL#MM18PL
Virtual Types
Re-use an existing Class
With different Arguments
#MM18PL#MM18PL
Example
#MM18PL#MM18PL
Example
#MM18PL#MM18PL
Some advice about Virtual Types
Complexity
Naming
Add “Virtual” to identifier
Only use Virtual Types if really necessary
#MM18PL#MM18PL
DI and Blocks
#MM18PL#MM18PL
Arguments
module-catalog/view/frontend/layout/catalog_product_view.xml
module-catalog/view/frontend/templates/product/list/items.phtml
#MM18PL#MM18PL
View Models
module-catalog/view/frontend/layout/catalog_product_view.xml
module-catalog/view/frontend/templates/product/breadcrumbs.phtml
Read more: https://www.integer-net.com/view-models-in-magento-1-and-2/
(by Fabian Schmengler)
#MM18PL#MM18PL
Remarks
This only works for Magento 2.2+
The view model MUST implement
Don’t overdo it!
#MM18PL#MM18PL
DI for Blocks
Preferences
Arguments
Virtual Types
#MM18PL#MM18PL
A little Helper
#MM18PL#MM18PL
A little Helper
#MM18PL#MM18PL
A little Helper
#MM18PL#MM18PL
Conclusion
Composition over Inheritance
A mighty Toolset
Use with care
#MM18PL
“
#MM18PL
“Debugging is twice as hard as
writing the code in the first place.
Therefore, if you write the code as
cleverly as possible, you are, by
definition, not smart enough to
debug it.”
- Brian Kernighan
#MM18PL#MM18PL
Thank you!
Any Questions?
avs@integer-net.de
@avstudnitz
@integer_net
Presentation Template by SlidesCarnival

Más contenido relacionado

Similar a Dependency Injection Extended: the way to advanced Magento 2 development

Mobile Product Strategy Keynote Presentation for Mobile App Europe Conference...
Mobile Product Strategy Keynote Presentation for Mobile App Europe Conference...Mobile Product Strategy Keynote Presentation for Mobile App Europe Conference...
Mobile Product Strategy Keynote Presentation for Mobile App Europe Conference...Marc C. Lange
 
Ai for Art and Design with TensorFlow Lite - TFUG India Summit
Ai for Art and Design with TensorFlow Lite - TFUG India SummitAi for Art and Design with TensorFlow Lite - TFUG India Summit
Ai for Art and Design with TensorFlow Lite - TFUG India SummitMargaret Maynard-Reid
 
How to make friends and influence developers - @stekenwright at #TechSEO Summat
How to make friends and influence developers - @stekenwright at #TechSEO SummatHow to make friends and influence developers - @stekenwright at #TechSEO Summat
How to make friends and influence developers - @stekenwright at #TechSEO SummatRise at Seven
 
10 Marketing Automation Mistakes Draining Your Budget
10 Marketing Automation Mistakes Draining Your Budget10 Marketing Automation Mistakes Draining Your Budget
10 Marketing Automation Mistakes Draining Your BudgetProjectArmy
 
Awesome Architectures in Magento 2.3
Awesome Architectures in Magento 2.3Awesome Architectures in Magento 2.3
Awesome Architectures in Magento 2.3Riccardo Tempesta
 
Beyond Polymer - JUG Summer Camp - 2015-09-18
Beyond Polymer - JUG Summer Camp - 2015-09-18Beyond Polymer - JUG Summer Camp - 2015-09-18
Beyond Polymer - JUG Summer Camp - 2015-09-18Horacio Gonzalez
 
Jared Smith - MeasureCamp Austin 2020 Presentation
Jared Smith - MeasureCamp Austin 2020 PresentationJared Smith - MeasureCamp Austin 2020 Presentation
Jared Smith - MeasureCamp Austin 2020 PresentationJaredSmith181
 
MeetMagento NL 2018 - Riccardo Tempesta - Magento 2 under siege
MeetMagento NL 2018 - Riccardo Tempesta - Magento 2 under siegeMeetMagento NL 2018 - Riccardo Tempesta - Magento 2 under siege
MeetMagento NL 2018 - Riccardo Tempesta - Magento 2 under siegeRiccardo Tempesta
 
How to leverage the Magento ecosystem to grow your business?
How to leverage the Magento ecosystem to grow your business?How to leverage the Magento ecosystem to grow your business?
How to leverage the Magento ecosystem to grow your business?Aron Stanic
 
More Signal, Less Noise: How to Dial in Your Customer Experience
More Signal, Less Noise: How to Dial in Your Customer ExperienceMore Signal, Less Noise: How to Dial in Your Customer Experience
More Signal, Less Noise: How to Dial in Your Customer ExperienceDana DiTomaso
 
Technical Marketing: The New Normal by Michael King - #SEJSummit Atlanta
Technical Marketing: The New Normal by Michael King - #SEJSummit AtlantaTechnical Marketing: The New Normal by Michael King - #SEJSummit Atlanta
Technical Marketing: The New Normal by Michael King - #SEJSummit AtlantaSearch Engine Journal
 

Similar a Dependency Injection Extended: the way to advanced Magento 2 development (13)

Mobile Product Strategy Keynote Presentation for Mobile App Europe Conference...
Mobile Product Strategy Keynote Presentation for Mobile App Europe Conference...Mobile Product Strategy Keynote Presentation for Mobile App Europe Conference...
Mobile Product Strategy Keynote Presentation for Mobile App Europe Conference...
 
Ai for Art and Design with TensorFlow Lite - TFUG India Summit
Ai for Art and Design with TensorFlow Lite - TFUG India SummitAi for Art and Design with TensorFlow Lite - TFUG India Summit
Ai for Art and Design with TensorFlow Lite - TFUG India Summit
 
How to make friends and influence developers - @stekenwright at #TechSEO Summat
How to make friends and influence developers - @stekenwright at #TechSEO SummatHow to make friends and influence developers - @stekenwright at #TechSEO Summat
How to make friends and influence developers - @stekenwright at #TechSEO Summat
 
10 Marketing Automation Mistakes Draining Your Budget
10 Marketing Automation Mistakes Draining Your Budget10 Marketing Automation Mistakes Draining Your Budget
10 Marketing Automation Mistakes Draining Your Budget
 
Awesome Architectures in Magento 2.3
Awesome Architectures in Magento 2.3Awesome Architectures in Magento 2.3
Awesome Architectures in Magento 2.3
 
Beyond Polymer - JUG Summer Camp - 2015-09-18
Beyond Polymer - JUG Summer Camp - 2015-09-18Beyond Polymer - JUG Summer Camp - 2015-09-18
Beyond Polymer - JUG Summer Camp - 2015-09-18
 
GraphQL in Magento 2
GraphQL in Magento 2GraphQL in Magento 2
GraphQL in Magento 2
 
Jared Smith - MeasureCamp Austin 2020 Presentation
Jared Smith - MeasureCamp Austin 2020 PresentationJared Smith - MeasureCamp Austin 2020 Presentation
Jared Smith - MeasureCamp Austin 2020 Presentation
 
Magento best practices
Magento best practicesMagento best practices
Magento best practices
 
MeetMagento NL 2018 - Riccardo Tempesta - Magento 2 under siege
MeetMagento NL 2018 - Riccardo Tempesta - Magento 2 under siegeMeetMagento NL 2018 - Riccardo Tempesta - Magento 2 under siege
MeetMagento NL 2018 - Riccardo Tempesta - Magento 2 under siege
 
How to leverage the Magento ecosystem to grow your business?
How to leverage the Magento ecosystem to grow your business?How to leverage the Magento ecosystem to grow your business?
How to leverage the Magento ecosystem to grow your business?
 
More Signal, Less Noise: How to Dial in Your Customer Experience
More Signal, Less Noise: How to Dial in Your Customer ExperienceMore Signal, Less Noise: How to Dial in Your Customer Experience
More Signal, Less Noise: How to Dial in Your Customer Experience
 
Technical Marketing: The New Normal by Michael King - #SEJSummit Atlanta
Technical Marketing: The New Normal by Michael King - #SEJSummit AtlantaTechnical Marketing: The New Normal by Michael King - #SEJSummit Atlanta
Technical Marketing: The New Normal by Michael King - #SEJSummit Atlanta
 

Más de Andreas von Studnitz

Successful projects with Hyvä - The impact of Developer Happiness
Successful projects with Hyvä - The impact of Developer HappinessSuccessful projects with Hyvä - The impact of Developer Happiness
Successful projects with Hyvä - The impact of Developer HappinessAndreas von Studnitz
 
Vorstellung Hyvä: Modernes Frontend mit Magento 2
Vorstellung Hyvä: Modernes Frontend mit Magento 2Vorstellung Hyvä: Modernes Frontend mit Magento 2
Vorstellung Hyvä: Modernes Frontend mit Magento 2Andreas von Studnitz
 
Magento Audit - anonymisiert (German)
Magento Audit - anonymisiert (German)Magento Audit - anonymisiert (German)
Magento Audit - anonymisiert (German)Andreas von Studnitz
 
Magento Worst Practice (Meet Magento Poland 2016)
Magento Worst Practice (Meet Magento Poland 2016)Magento Worst Practice (Meet Magento Poland 2016)
Magento Worst Practice (Meet Magento Poland 2016)Andreas von Studnitz
 
Was kann denn schon passieren? Sicherheit in Magento-Shops
Was kann denn schon passieren? Sicherheit in Magento-ShopsWas kann denn schon passieren? Sicherheit in Magento-Shops
Was kann denn schon passieren? Sicherheit in Magento-ShopsAndreas von Studnitz
 
What could possibly go wrong? Security in Magento Shops
What could possibly go wrong? Security in Magento ShopsWhat could possibly go wrong? Security in Magento Shops
What could possibly go wrong? Security in Magento ShopsAndreas von Studnitz
 

Más de Andreas von Studnitz (7)

Successful projects with Hyvä - The impact of Developer Happiness
Successful projects with Hyvä - The impact of Developer HappinessSuccessful projects with Hyvä - The impact of Developer Happiness
Successful projects with Hyvä - The impact of Developer Happiness
 
Vorstellung Hyvä: Modernes Frontend mit Magento 2
Vorstellung Hyvä: Modernes Frontend mit Magento 2Vorstellung Hyvä: Modernes Frontend mit Magento 2
Vorstellung Hyvä: Modernes Frontend mit Magento 2
 
Code Quality with Magento 2
Code Quality with Magento 2Code Quality with Magento 2
Code Quality with Magento 2
 
Magento Audit - anonymisiert (German)
Magento Audit - anonymisiert (German)Magento Audit - anonymisiert (German)
Magento Audit - anonymisiert (German)
 
Magento Worst Practice (Meet Magento Poland 2016)
Magento Worst Practice (Meet Magento Poland 2016)Magento Worst Practice (Meet Magento Poland 2016)
Magento Worst Practice (Meet Magento Poland 2016)
 
Was kann denn schon passieren? Sicherheit in Magento-Shops
Was kann denn schon passieren? Sicherheit in Magento-ShopsWas kann denn schon passieren? Sicherheit in Magento-Shops
Was kann denn schon passieren? Sicherheit in Magento-Shops
 
What could possibly go wrong? Security in Magento Shops
What could possibly go wrong? Security in Magento ShopsWhat could possibly go wrong? Security in Magento Shops
What could possibly go wrong? Security in Magento Shops
 

Último

Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
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
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineeringssuserb3a23b
 
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
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
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
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
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
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
cpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptcpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptrcbcrtm
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 

Último (20)

Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
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
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineering
 
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
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
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...
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
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
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
cpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptcpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.ppt
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 

Dependency Injection Extended: the way to advanced Magento 2 development