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.

Valeriy Nayda - Best Practices in Magento 2. Based on Multi Source Inventory (msi) project

160 views

Published on

Best Practices in Magento 2 Development using MSI project as an example by Valeriy Nayda at Khmelnytskyi Magento Meetup.

Published in: Internet
  • Be the first to comment

Valeriy Nayda - Best Practices in Magento 2. Based on Multi Source Inventory (msi) project

  1. 1. Page | 1© 2017 Magento, Inc. Khmelnytskyi Magento Meetup Best practices in Magento 2 Based on Multi-Source Inventory (MSI) project
  2. 2. Page | 2© 2017 Magento, Inc. Khmelnytskyi Magento Meetup Valeriy Naida Magento Developer, Multi Source Inventory Community Engineering Team
  3. 3. Page | 3© 2017 Magento, Inc. Khmelnytskyi Magento Meetup In one of our directions we have started to work with Community • Benefit for Magento is creation of new important feature • Benefit for Community is an increase in knowledge about Magento 2 • Provide a set of best practices how to work with Magento 2 (some cookbook) Community Engineering Team
  4. 4. Page | 4© 2017 Magento, Inc. Khmelnytskyi Magento Meetup • What is API / SPI in Magento 2 (more practice) • Repository responsibility in Magento 2 • Use Composition instead of Inheritance / Traits • Object Manager should not be used as a class dependency … • There is no silver bullet (Think first!) Frequently asked questions
  5. 5. Khmelnytskyi Magento Meetup API / SPI concepts in Magento 2
  6. 6. Page | 6© 2017 Magento, Inc. Khmelnytskyi Magento Meetup API - Interfaces for usage (calling) in the client code Located in Api subfolder or in the separate module ApiModule API
  7. 7. Page | 7© 2017 Magento, Inc. Khmelnytskyi Magento Meetup 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 SPI
  8. 8. Page | 8© 2017 Magento, Inc. Khmelnytskyi Magento Meetup Q: Can the API and SPI be a single interface? A: Yes. It often happens. Do not create empty proxy (API to SPI) classes FAQ
  9. 9. Page | 9© 2017 Magento, Inc. Khmelnytskyi Magento Meetup 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 FAQ
  10. 10. Page | 10© 2017 Magento, Inc. Khmelnytskyi Magento Meetup Q: What about Resource Model? A: TBD Q: When is needed to add SPI? A: TBD Q: Why not introduce the @spi annotation? A: TBD FAQ
  11. 11. Page | 11© 2017 Magento, Inc. Khmelnytskyi Magento Meetup 1. Main logic is API 2. Steps (strategies) are SPI Algorithm example
  12. 12. Khmelnytskyi Magento Meetup Repository responsibility In Magento 2
  13. 13. Page | 13© 2017 Magento, Inc. Khmelnytskyi Magento Meetup 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” Facade pattern
  14. 14. Page | 14© 2017 Magento, Inc. Khmelnytskyi Magento Meetup Repository under hood Usually typical repository provides the following methods: Could NOT be wider than methods mentioned above
  15. 15. Page | 15© 2017 Magento, Inc. Khmelnytskyi Magento Meetup 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)
  16. 16. Page | 16© 2017 Magento, Inc. Khmelnytskyi Magento Meetup Repository under hood Methods with their own semantic recommended to be put into some dedicated Services • Principle Of Least Surprise • Backward compatibility
  17. 17. Page | 17© 2017 Magento, Inc. Khmelnytskyi Magento Meetup DDD liked services instead of Repository Link between Source and Stock doesn’t have sense from business side
  18. 18. Page | 18© 2017 Magento, Inc. Khmelnytskyi Magento Meetup Repository Implementation
  19. 19. Page | 19© 2017 Magento, Inc. Khmelnytskyi Magento Meetup 1. Repository could be considered as an API - Interface for usage (calling) in the business logic 2. Separate class-commands to which Repository proxies initial call (like, Get Save GetList Delete) could be considered as SPI Repository: API vs SPI segregation
  20. 20. Khmelnytskyi Magento Meetup Object Manager should not be used as a class dependency
  21. 21. Page | 21© 2017 Magento, Inc. Khmelnytskyi Magento Meetup 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
  22. 22. Page | 22© 2017 Magento, Inc. Khmelnytskyi Magento Meetup There is no good possibility to replace Logger object only for this Class Example - Problem
  23. 23. Page | 23© 2017 Magento, Inc. Khmelnytskyi Magento Meetup Example - Solution
  24. 24. Page | 24© 2017 Magento, Inc. Khmelnytskyi Magento Meetup Object Manager could be used in classes which create objects (Factories, Builders, Pools) When Object Manager is applicable
  25. 25. Page | 25© 2017 Magento, Inc. Khmelnytskyi Magento Meetup May also be used to maintain Backwards Compatibility When Object Manager is applicable
  26. 26. Khmelnytskyi Magento Meetup Composition against Inheritance
  27. 27. Page | 27© 2017 Magento, Inc. Khmelnytskyi Magento Meetup 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
  28. 28. Page | 28© 2017 Magento, Inc. Khmelnytskyi Magento Meetup Requires complete replacement of the renderer, instead of customizing it Example - Problem
  29. 29. Page | 29© 2017 Magento, Inc. Khmelnytskyi Magento Meetup • Price Formatter may be easily replaced by injecting another dependency in the constructor • Allows precise modification of behavior Example - Solution
  30. 30. Page | 30© 2017 Magento, Inc. Khmelnytskyi Magento Meetup • 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
  31. 31. Page | 31© 2017 Magento, Inc. Khmelnytskyi Magento Meetup Provides possibility of saving entity with predefined/pre-generated id (quick solution in waiting for the ORM) IS NOT supposed to be replaced
  32. 32. Khmelnytskyi Magento Meetup Useful Links
  33. 33. Page | 33© 2017 Magento, Inc. Khmelnytskyi Magento Meetup • 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 Useful Links
  34. 34. Page | 34© 2017 Magento, Inc. Khmelnytskyi Magento Meetup Exchange Ideas
  35. 35. Khmelnytskyi Magento Meetup How to join us? Send an email to engcom@magento.com @_naydav Thank y’all!

×