SlideShare una empresa de Scribd logo
1 de 35
Descargar para leer sin conexión
© 2017 Magento, Inc. Page | 1 ‘17
Service Layer in Magento 2
Based on Multi-Source Inventory (MSI) project
© 2017 Magento, Inc. Page | 2 ‘17
Magento Developer, Multi Source Inventory
Community Engineering Team
Valeriy Naida
© 2017 Magento, Inc. Page | 3 ‘17
Community Engineering Team
In one of our directions we have started to work with Community
• Benefit for Magento is creation of a new important feature
• Benefit for Community is an increase in knowledge about Magento 2
• Providing a set of best practices how to work with Magento 2 (some
cookbook)
© 2017 Magento, Inc. Page | 4 ‘17
Frequently Asked Questions
• Service Layer
• What is API / SPI in Magento 2
• Repository responsibility in Magento 2
…
• There is no silver bullet (Think first!)
© 2017 Magento, Inc. Page | 5
API / SPI concepts in
Magento 2
© 2017 Magento, Inc. Page | 6 ‘17
API
API - Interfaces for usage (calling) in the client
code
Located in Api subfolder or in the separate
module ApiModule
Api functional tests can be located in ApiModule
© 2017 Magento, Inc. Page | 7 ‘17
SPI
SPI - Interfaces that you should extend and
implement to customize current behavior
But NOT expected to be used (called) in the
client code directly
Located near implementation
© 2017 Magento, Inc. Page | 8 ‘17
Source Selection Algorithm
1. Place order is API
2. Algorithms are SPI
© 2017 Magento, Inc. Page | 9 ‘17
FAQ
Q: Can the API and SPI be a single interface?
A: Yes. It often happens. Do not create empty proxy (API to SPI) classes
© 2017 Magento, Inc. Page | 10 ‘17
FAQ
Q: Can I call one API from another?
A: Yes. You could create Sugar Service to avoid boilerplate code
=>
• But provide clear DocBlock
• Try to keep a single point of Customization
© 2017 Magento, Inc. Page | 11 ‘17
FAQ
Q: What about Resource Model?
A: Resource Model can be replaced by default
© 2017 Magento, Inc. Page | 12 ‘17
FAQ
Q: When is needed to add SPI?
A: Depends on the complexity (requirements) of the project
© 2017 Magento, Inc. Page | 13
Repository responsibility In
Magento 2
© 2017 Magento, Inc. Page | 14 ‘17
Facade pattern
In Magento 2 Repository is considered as an implementation of Facade
pattern which provides a simplified interface to a larger body of code
responsible for Domain Entity base management
BUT Repository is NOT new kind of “helper”
© 2017 Magento, Inc. Page | 15 ‘17
Repository under hood
Usually typical repository provides the following methods:
Could NOT be wider than methods mentioned above
© 2017 Magento, Inc. Page | 16 ‘17
Repository under hood
List of the methods could be shorter if based on the business
requirements particular entity doesn't have some of the operation(-s)
© 2017 Magento, Inc. Page | 17 ‘17
Repository under hood
Methods with their own semantic recommended to be put into some
dedicated Services
• Principle Of Least Surprise
• Backward compatibility
© 2017 Magento, Inc. Page | 18 ‘17
DDD liked services instead of Repository
Link between Source and Stock doesn’t have sense from business
perspective
© 2017 Magento, Inc. Page | 19 ‘17
Repository Implementation
© 2017 Magento, Inc. Page | 20
Useful Links
© 2017 Magento, Inc. Page | 21 ‘17
Useful Links
• Magento 2 Technical Guidelines
http://devdocs.magento.com/guides/v2.2/coding-standards/technical-
guidelines/technical-guidelines.html
• Backward compatible development
http://devdocs.magento.com/guides/v2.2/contributor-guide/backward-
compatible-development/
• Multi-Source Inventory (MSI) project
https://github.com/magento-engcom/msi/wiki
© 2017 Magento, Inc. Page | 22 ‘17
Exchange Ideas
‘17
Object Manager should
not be used as a class
dependency
Page | 24© 2017 Magento, Inc.
Generally Object Manager should not be used as a class dependency
Problems:
• Class is hard coded and can not be replaced with a different one
• Would require more work to modify the behavior by third parties
Object Manager
Page | 25© 2017 Magento, Inc.
There is no good possibility to replace Logger object only for this Class
Example - Problem
Page | 26© 2017 Magento, Inc.
Example - Solution
Page | 27© 2017 Magento, Inc.
Object Manager could be used in classes which create objects
(Factories, Builders, Pools)
When Object Manager is applicable
Page | 28© 2017 Magento, Inc.
May also be used to maintain Backwards Compatibility
When Object Manager is applicable
‘17
Composition against
Inheritance
Page | 30© 2017 Magento, Inc.
Inheritance / trait should not be used. Composition should be used
instead
Problems:
• Inheritance enforces dependency on a specific parent class which can not be
replaced in runtime
• Would require more work to modify the behavior by third parties
Inheritance
Page | 31© 2017 Magento, Inc.
Requires complete replacement of the
renderer, instead of customizing it
Example - Problem
Page | 32© 2017 Magento, Inc.
• Price Formatter may be easily
replaced by injecting another
dependency in the constructor
• Allows precise modification of
behavior
Example - Solution
Page | 33© 2017 Magento, Inc.
• Class is a Subtype of another class
• Template Method pattern (usually with Hollywood Principle: Don't call us,
we'll call you)
• You don’t need replaceability
When Inheritance / trait is applicable
Page | 34© 2017 Magento, Inc.
Provides possibility of saving entity with predefined/pre-generated id (quick
solution in waiting for the ORM)
IS NOT supposed to be replaced
‘17
How to join us? Send an email to
engcom@magento.com
@_naydav
Thank y’all!

Más contenido relacionado

La actualidad más candente

Koha onboard presentation
Koha onboard presentationKoha onboard presentation
Koha onboard presentation
Alex Buckley
 

La actualidad más candente (19)

Chernivtsi Magento Meetup&Contribution day. Miniailo.I.
Chernivtsi Magento Meetup&Contribution day. Miniailo.I. Chernivtsi Magento Meetup&Contribution day. Miniailo.I.
Chernivtsi Magento Meetup&Contribution day. Miniailo.I.
 
Something Architecture
Something ArchitectureSomething Architecture
Something Architecture
 
Multi-Source Inventory. Imagine. Las Vegas. 2018
Multi-Source Inventory. Imagine. Las Vegas. 2018Multi-Source Inventory. Imagine. Las Vegas. 2018
Multi-Source Inventory. Imagine. Las Vegas. 2018
 
Experience in Magento Community Projects
Experience in Magento Community ProjectsExperience in Magento Community Projects
Experience in Magento Community Projects
 
Mli 2017 technical backward compatibility
Mli 2017 technical backward compatibilityMli 2017 technical backward compatibility
Mli 2017 technical backward compatibility
 
Mli 2017 technical first steps to building secure Magento extensions
Mli 2017 technical first steps to building secure Magento extensionsMli 2017 technical first steps to building secure Magento extensions
Mli 2017 technical first steps to building secure Magento extensions
 
Mli 2017 technical intro to magento 2
Mli 2017 technical intro to magento 2Mli 2017 technical intro to magento 2
Mli 2017 technical intro to magento 2
 
Awesome architectures in Magento 2.3
Awesome architectures in Magento 2.3Awesome architectures in Magento 2.3
Awesome architectures in Magento 2.3
 
Magento 2.3 Schema and Data Patches
Magento 2.3 Schema and Data PatchesMagento 2.3 Schema and Data Patches
Magento 2.3 Schema and Data Patches
 
Magento 2 Declarative Schema
Magento 2 Declarative SchemaMagento 2 Declarative Schema
Magento 2 Declarative Schema
 
Mli 2017 technical powering tomorrow_2.2
Mli 2017 technical powering tomorrow_2.2Mli 2017 technical powering tomorrow_2.2
Mli 2017 technical powering tomorrow_2.2
 
Magento Technical guidelines
Magento Technical guidelinesMagento Technical guidelines
Magento Technical guidelines
 
Matteo Santagata - Is your project scaling right? The BEE case study - Codemo...
Matteo Santagata - Is your project scaling right? The BEE case study - Codemo...Matteo Santagata - Is your project scaling right? The BEE case study - Codemo...
Matteo Santagata - Is your project scaling right? The BEE case study - Codemo...
 
Magento Meetup Nagpur - Saturday, Nov 21, 2020 | Rich Content Renderers PWA, ...
Magento Meetup Nagpur - Saturday, Nov 21, 2020 | Rich Content Renderers PWA, ...Magento Meetup Nagpur - Saturday, Nov 21, 2020 | Rich Content Renderers PWA, ...
Magento Meetup Nagpur - Saturday, Nov 21, 2020 | Rich Content Renderers PWA, ...
 
Reactive Web Best Practices
Reactive Web Best PracticesReactive Web Best Practices
Reactive Web Best Practices
 
Magento Meetup Nagpur - Saturday, Sep 12, 2020 | PWA Studio, Demystifying MSI
Magento Meetup Nagpur - Saturday, Sep 12, 2020 | PWA Studio, Demystifying MSIMagento Meetup Nagpur - Saturday, Sep 12, 2020 | PWA Studio, Demystifying MSI
Magento Meetup Nagpur - Saturday, Sep 12, 2020 | PWA Studio, Demystifying MSI
 
Leveraging Dynamic Interactions on Salesforce Lightning Pages
Leveraging Dynamic Interactions on Salesforce Lightning PagesLeveraging Dynamic Interactions on Salesforce Lightning Pages
Leveraging Dynamic Interactions on Salesforce Lightning Pages
 
Magento Function Testing Framework - Intro and Overview
Magento Function Testing Framework - Intro and OverviewMagento Function Testing Framework - Intro and Overview
Magento Function Testing Framework - Intro and Overview
 
Koha onboard presentation
Koha onboard presentationKoha onboard presentation
Koha onboard presentation
 

Similar a Chernivtsi Magento Meetup&Contribution day. Naida V.

Imagine recap-devhub
Imagine recap-devhubImagine recap-devhub
Imagine recap-devhub
Magento Dev
 

Similar a Chernivtsi Magento Meetup&Contribution day. Naida V. (20)

MageConf 2017, Design API Best Practices
MageConf 2017, Design API Best PracticesMageConf 2017, Design API Best Practices
MageConf 2017, Design API Best Practices
 
How I ended up touching Magento core
How I ended up touching Magento coreHow I ended up touching Magento core
How I ended up touching Magento core
 
Magento 2 Best Practice MLUK17
Magento 2 Best Practice MLUK17Magento 2 Best Practice MLUK17
Magento 2 Best Practice MLUK17
 
Automated tests in Magento
Automated tests in MagentoAutomated tests in Magento
Automated tests in Magento
 
Testing in Magento 2
Testing in Magento 2 Testing in Magento 2
Testing in Magento 2
 
Monitoring your cache effectiveness in Magento 2
Monitoring your cache effectiveness in Magento 2Monitoring your cache effectiveness in Magento 2
Monitoring your cache effectiveness in Magento 2
 
API design best practices
API design best practicesAPI design best practices
API design best practices
 
Imagine recap-devhub
Imagine recap-devhubImagine recap-devhub
Imagine recap-devhub
 
Hyvä from a developer perspective
Hyvä from a developer perspectiveHyvä from a developer perspective
Hyvä from a developer perspective
 
The Evolution of Hosting and What's Next with Cloud
The Evolution of Hosting and What's Next with CloudThe Evolution of Hosting and What's Next with Cloud
The Evolution of Hosting and What's Next with Cloud
 
The long way from Monolith to Microservices
The long way from Monolith to MicroservicesThe long way from Monolith to Microservices
The long way from Monolith to Microservices
 
Magento Cloud - Introduction
Magento Cloud - IntroductionMagento Cloud - Introduction
Magento Cloud - Introduction
 
Backward Compatibility Developer's Guide in Magento 2
Backward Compatibility Developer's Guide in Magento 2Backward Compatibility Developer's Guide in Magento 2
Backward Compatibility Developer's Guide in Magento 2
 
A long way from Monolith to Service Isolated Architecture #MM19NL
A long way from Monolith to Service Isolated Architecture #MM19NLA long way from Monolith to Service Isolated Architecture #MM19NL
A long way from Monolith to Service Isolated Architecture #MM19NL
 
Eugene Shaksuvarov - Tuning Magento 2 for Maximum Performance
Eugene Shaksuvarov - Tuning Magento 2 for Maximum PerformanceEugene Shaksuvarov - Tuning Magento 2 for Maximum Performance
Eugene Shaksuvarov - Tuning Magento 2 for Maximum Performance
 
How to edit the core
How to edit the coreHow to edit the core
How to edit the core
 
Backward Compatibility Developer's Guide in Magento 2. #MM17CZ
Backward Compatibility Developer's Guide in Magento 2. #MM17CZBackward Compatibility Developer's Guide in Magento 2. #MM17CZ
Backward Compatibility Developer's Guide in Magento 2. #MM17CZ
 
Magento 2 ERP Integration Best Practices: Microsoft Dynamics
Magento 2 ERP Integration Best Practices: Microsoft DynamicsMagento 2 ERP Integration Best Practices: Microsoft Dynamics
Magento 2 ERP Integration Best Practices: Microsoft Dynamics
 
Mli 2017 technical m2 developer experience
Mli 2017 technical m2 developer experienceMli 2017 technical m2 developer experience
Mli 2017 technical m2 developer experience
 
Best Practices - By Lofi Dewanto
Best Practices - By Lofi DewantoBest Practices - By Lofi Dewanto
Best Practices - By Lofi Dewanto
 

Más de Elogic Magento Development

Más de Elogic Magento Development (14)

Миграция кода с Magento 1 на Magento 2
Миграция кода с Magento 1 на Magento 2Миграция кода с Magento 1 на Magento 2
Миграция кода с Magento 1 на Magento 2
 
Introduction to Magento Community
Introduction to Magento Community Introduction to Magento Community
Introduction to Magento Community
 
CQRS and Event-Sourcing in Magento2 by examples of MSI
CQRS and Event-Sourcing in Magento2 by examples of MSICQRS and Event-Sourcing in Magento2 by examples of MSI
CQRS and Event-Sourcing in Magento2 by examples of MSI
 
Chernivtsi Magento Meetup&Contribution day. V. Kublytskyi
 Chernivtsi Magento Meetup&Contribution day. V. Kublytskyi Chernivtsi Magento Meetup&Contribution day. V. Kublytskyi
Chernivtsi Magento Meetup&Contribution day. V. Kublytskyi
 
The process of a Lean Magento development
The process of a Lean Magento developmentThe process of a Lean Magento development
The process of a Lean Magento development
 
12 Ways to Improve Magento 2 Security and Performance
12 Ways to Improve Magento 2 Security and Performance12 Ways to Improve Magento 2 Security and Performance
12 Ways to Improve Magento 2 Security and Performance
 
MMnl Pavlo Okhrem
MMnl Pavlo Okhrem MMnl Pavlo Okhrem
MMnl Pavlo Okhrem
 
LIOF 2016
LIOF 2016LIOF 2016
LIOF 2016
 
Payment integration patterns в Magento2
Payment integration patterns в Magento2Payment integration patterns в Magento2
Payment integration patterns в Magento2
 
Как благодаря композеру использовать сторонние компоненты в Magento 2
Как благодаря композеру использовать сторонние компоненты в Magento 2Как благодаря композеру использовать сторонние компоненты в Magento 2
Как благодаря композеру использовать сторонние компоненты в Magento 2
 
Magento 2 - the future of eCommerce
Magento 2 - the future of eCommerceMagento 2 - the future of eCommerce
Magento 2 - the future of eCommerce
 
RequireJS і Magento 2
RequireJS і Magento 2RequireJS і Magento 2
RequireJS і Magento 2
 
Як перехід на Magento допоміг нам стати лідером
Як перехід на Magento допоміг нам стати лідеромЯк перехід на Magento допоміг нам стати лідером
Як перехід на Magento допоміг нам стати лідером
 
Как переписать модуль с Magento 1 на Magento 2
Как переписать модуль с Magento 1 на Magento 2Как переписать модуль с Magento 1 на Magento 2
Как переписать модуль с Magento 1 на Magento 2
 

Último

Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 

Último (20)

Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 

Chernivtsi Magento Meetup&Contribution day. Naida V.

  • 1. © 2017 Magento, Inc. Page | 1 ‘17 Service Layer in Magento 2 Based on Multi-Source Inventory (MSI) project
  • 2. © 2017 Magento, Inc. Page | 2 ‘17 Magento Developer, Multi Source Inventory Community Engineering Team Valeriy Naida
  • 3. © 2017 Magento, Inc. Page | 3 ‘17 Community Engineering Team In one of our directions we have started to work with Community • Benefit for Magento is creation of a new important feature • Benefit for Community is an increase in knowledge about Magento 2 • Providing a set of best practices how to work with Magento 2 (some cookbook)
  • 4. © 2017 Magento, Inc. Page | 4 ‘17 Frequently Asked Questions • Service Layer • What is API / SPI in Magento 2 • Repository responsibility in Magento 2 … • There is no silver bullet (Think first!)
  • 5. © 2017 Magento, Inc. Page | 5 API / SPI concepts in Magento 2
  • 6. © 2017 Magento, Inc. Page | 6 ‘17 API API - Interfaces for usage (calling) in the client code Located in Api subfolder or in the separate module ApiModule Api functional tests can be located in ApiModule
  • 7. © 2017 Magento, Inc. Page | 7 ‘17 SPI SPI - Interfaces that you should extend and implement to customize current behavior But NOT expected to be used (called) in the client code directly Located near implementation
  • 8. © 2017 Magento, Inc. Page | 8 ‘17 Source Selection Algorithm 1. Place order is API 2. Algorithms are SPI
  • 9. © 2017 Magento, Inc. Page | 9 ‘17 FAQ Q: Can the API and SPI be a single interface? A: Yes. It often happens. Do not create empty proxy (API to SPI) classes
  • 10. © 2017 Magento, Inc. Page | 10 ‘17 FAQ Q: Can I call one API from another? A: Yes. You could create Sugar Service to avoid boilerplate code => • But provide clear DocBlock • Try to keep a single point of Customization
  • 11. © 2017 Magento, Inc. Page | 11 ‘17 FAQ Q: What about Resource Model? A: Resource Model can be replaced by default
  • 12. © 2017 Magento, Inc. Page | 12 ‘17 FAQ Q: When is needed to add SPI? A: Depends on the complexity (requirements) of the project
  • 13. © 2017 Magento, Inc. Page | 13 Repository responsibility In Magento 2
  • 14. © 2017 Magento, Inc. Page | 14 ‘17 Facade pattern In Magento 2 Repository is considered as an implementation of Facade pattern which provides a simplified interface to a larger body of code responsible for Domain Entity base management BUT Repository is NOT new kind of “helper”
  • 15. © 2017 Magento, Inc. Page | 15 ‘17 Repository under hood Usually typical repository provides the following methods: Could NOT be wider than methods mentioned above
  • 16. © 2017 Magento, Inc. Page | 16 ‘17 Repository under hood List of the methods could be shorter if based on the business requirements particular entity doesn't have some of the operation(-s)
  • 17. © 2017 Magento, Inc. Page | 17 ‘17 Repository under hood Methods with their own semantic recommended to be put into some dedicated Services • Principle Of Least Surprise • Backward compatibility
  • 18. © 2017 Magento, Inc. Page | 18 ‘17 DDD liked services instead of Repository Link between Source and Stock doesn’t have sense from business perspective
  • 19. © 2017 Magento, Inc. Page | 19 ‘17 Repository Implementation
  • 20. © 2017 Magento, Inc. Page | 20 Useful Links
  • 21. © 2017 Magento, Inc. Page | 21 ‘17 Useful Links • Magento 2 Technical Guidelines http://devdocs.magento.com/guides/v2.2/coding-standards/technical- guidelines/technical-guidelines.html • Backward compatible development http://devdocs.magento.com/guides/v2.2/contributor-guide/backward- compatible-development/ • Multi-Source Inventory (MSI) project https://github.com/magento-engcom/msi/wiki
  • 22. © 2017 Magento, Inc. Page | 22 ‘17 Exchange Ideas
  • 23. ‘17 Object Manager should not be used as a class dependency
  • 24. Page | 24© 2017 Magento, Inc. Generally Object Manager should not be used as a class dependency Problems: • Class is hard coded and can not be replaced with a different one • Would require more work to modify the behavior by third parties Object Manager
  • 25. Page | 25© 2017 Magento, Inc. There is no good possibility to replace Logger object only for this Class Example - Problem
  • 26. Page | 26© 2017 Magento, Inc. Example - Solution
  • 27. Page | 27© 2017 Magento, Inc. Object Manager could be used in classes which create objects (Factories, Builders, Pools) When Object Manager is applicable
  • 28. Page | 28© 2017 Magento, Inc. May also be used to maintain Backwards Compatibility When Object Manager is applicable
  • 30. Page | 30© 2017 Magento, Inc. Inheritance / trait should not be used. Composition should be used instead Problems: • Inheritance enforces dependency on a specific parent class which can not be replaced in runtime • Would require more work to modify the behavior by third parties Inheritance
  • 31. Page | 31© 2017 Magento, Inc. Requires complete replacement of the renderer, instead of customizing it Example - Problem
  • 32. Page | 32© 2017 Magento, Inc. • Price Formatter may be easily replaced by injecting another dependency in the constructor • Allows precise modification of behavior Example - Solution
  • 33. Page | 33© 2017 Magento, Inc. • Class is a Subtype of another class • Template Method pattern (usually with Hollywood Principle: Don't call us, we'll call you) • You don’t need replaceability When Inheritance / trait is applicable
  • 34. Page | 34© 2017 Magento, Inc. Provides possibility of saving entity with predefined/pre-generated id (quick solution in waiting for the ORM) IS NOT supposed to be replaced
  • 35. ‘17 How to join us? Send an email to engcom@magento.com @_naydav Thank y’all!