Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Chernivtsi Magento Meetup&Contribution day. Naida V.

110 views

Published on

Presentation from Chernivtsi Magento Meetup&Contribution day

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

Chernivtsi Magento Meetup&Contribution day. Naida V.

  1. 1. © 2017 Magento, Inc. Page | 1 ‘17 Service Layer in Magento 2 Based on Multi-Source Inventory (MSI) project
  2. 2. © 2017 Magento, Inc. Page | 2 ‘17 Magento Developer, Multi Source Inventory Community Engineering Team Valeriy Naida
  3. 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. 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. 5. © 2017 Magento, Inc. Page | 5 API / SPI concepts in Magento 2
  6. 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. 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. 8. © 2017 Magento, Inc. Page | 8 ‘17 Source Selection Algorithm 1. Place order is API 2. Algorithms are SPI
  9. 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. 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. 11. © 2017 Magento, Inc. Page | 11 ‘17 FAQ Q: What about Resource Model? A: Resource Model can be replaced by default
  12. 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. 13. © 2017 Magento, Inc. Page | 13 Repository responsibility In Magento 2
  14. 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. 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. 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. 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. 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. 19. © 2017 Magento, Inc. Page | 19 ‘17 Repository Implementation
  20. 20. © 2017 Magento, Inc. Page | 20 Useful Links
  21. 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. 22. © 2017 Magento, Inc. Page | 22 ‘17 Exchange Ideas
  23. 23. ‘17 Object Manager should not be used as a class dependency
  24. 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. 25. Page | 25© 2017 Magento, Inc. There is no good possibility to replace Logger object only for this Class Example - Problem
  26. 26. Page | 26© 2017 Magento, Inc. Example - Solution
  27. 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. 28. Page | 28© 2017 Magento, Inc. May also be used to maintain Backwards Compatibility When Object Manager is applicable
  29. 29. ‘17 Composition against Inheritance
  30. 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. 31. Page | 31© 2017 Magento, Inc. Requires complete replacement of the renderer, instead of customizing it Example - Problem
  32. 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. 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. 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. 35. ‘17 How to join us? Send an email to engcom@magento.com @_naydav Thank y’all!

×