SlideShare une entreprise Scribd logo
1  sur  54
© 2017 Magento, Inc. Page | 1 ‘17
API Design
Best practices
Igor Miniailo
Magento Architect
© 2017 Magento, Inc. Page | 2 ‘17
© 2017 Magento, Inc. Page | 3 ‘17
© 2017 Magento, Inc. Page | 4 ‘17
Why is API so crucial?
© 2017 Magento, Inc. Page | 5 ‘17
© 2017 Magento, Inc. Page | 6 ‘17
© 2017 Magento, Inc. Page | 7 ‘17
© 2017 Magento, Inc. Page | 8 ‘17
Semantic Versioning
Version numbers are in the format
MAJOR.MINOR.PATCH, where:
– MAJOR indicates incompatible API changes
– MINOR indicates backward-compatible
functionality has been added
– PATCH indicates backward-compatible bug
fixes
© 2017 Magento, Inc. Page | 9 ‘17
The backward compatibility policy
applies to PHP code annotated with
@api
© 2017 Magento, Inc. Page | 10 ‘17
Public and Private code
© 2017 Magento, Inc. Page | 11 ‘17
Public vs Private code
Private code is not supposed to
be used by third party modules,
so, in most cases, its
modifications will only trigger
PATCH version bumps.
Changes in public code always
trigger MINOR or MAJOR
version bumps.
© 2017 Magento, Inc. Page | 12 ‘17
What examples of Public code Magento has?
• PHP Interface (marked with @api)
• PHP Class (marked with @api)
• Javascript Interface (marked with @api)
• Javascript Class (marked with @api)
• Virtual Type (marked with @api)
• URL paths
• Console commands and their arguments
• Less Variables & Mixins
• Message queue topics and their data types
• UI component declarations
• Layout handles declared by modules
• Events triggered by component (both static dynamic)
• Schema of configuration types introduced by module
• Structure of System Configuration fields used by module
© 2017 Magento, Inc. Page | 13 ‘17
API vs SPI (Extension Points)
A PHP Interface in Magento can be used several ways by the core
product and extension developers.
• As an API. is a set of interfaces and their
implementations that a module provides to other
modules
• As a Service Provider Interface (SPI). is a set of
interfaces that a module uses internally and allows their
implementation by other modules.
• As both. APIs and SPIs are not mutually exclusive.
© 2017 Magento, Inc. Page | 14 ‘17
After the code is released, both API and SPI can
be evolved in a backwards-compatible way. But
both have their specific limitations.
© 2017 Magento, Inc. Page | 15 ‘17
© 2017 Magento, Inc. Page | 16 ‘17
© 2017 Magento, Inc. Page | 17 ‘17
© 2017 Magento, Inc. Page | 18 ‘17
© 2017 Magento, Inc. Page | 19 ‘17
© 2017 Magento, Inc. Page | 20 ‘17
We do not distinguish them separately.
SPIs are annotated the same as APIs.
© 2017 Magento, Inc. Page | 21 ‘17
Who decides whether interface/class belong to API or SPI?
YOU
© 2017 Magento, Inc. Page | 22 ‘17
Dependency Rules API
If a module uses (calls) an API, it should be dependent on the MAJOR
version.
API dependency example
{
...
"require": {
"magento/module-customer": "~100.0", // (>=100.0 <101.0.0)
},
...
}
© 2017 Magento, Inc. Page | 23 ‘17
Dependency Rules SPI
If a module implements an API/SPI, it should be dependent on the
MAJOR+MINOR version.
SPI dependency example
{
...
"require": {
"magento/module-customer": "~100.0.0", // (>=100.0.0 <100.1.0)
},
...
}
© 2017 Magento, Inc. Page | 24 ‘17
http://devdocs.magento.com/guides/v2.1/release-notes/backward-
incompatible-changes-2.1.html
© 2017 Magento, Inc. Page | 25 ‘17
Prohibited Code Changes
© 2017 Magento, Inc. Page | 26 ‘17
• Interface/class removal
• Public & protected method removal
• Introduction of a method to a class or
interface
PHP - Prohibited Code Changes
© 2017 Magento, Inc. Page | 27 ‘17
MagentoCatalogApiCategoryRepositoryInterface
© 2017 Magento, Inc. Page | 28 ‘17
MagentoCatalogApiCategoryListInterface
© 2017 Magento, Inc. Page | 29 ‘17
PHP - Prohibited Code Changes
• Static function removal
• Parameter addition in public methods
• Parameter addition in protected
methods
© 2017 Magento, Inc. Page | 30 ‘17
© 2017 Magento, Inc. Page | 31 ‘17
PHP - Prohibited Code Changes
• Method argument type modification
• Modification of types of thrown
exceptions (unless a new exception is
a subtype of the old one)
• Constructor modification
© 2017 Magento, Inc. Page | 32 ‘17
class ExistingClass
{
/**
* @var NewDependencyInterface $newDependency
*/
private $newDependency;
public function __construct(
OldDependencyIntreface $oldDependency,
$oldRequiredConstructorParameter,
$oldOptinalConstructorParameter = null,
NewDependencyInterface $newDependency = null
) {
...
$this>newDependency = $newDependency ?: MagentoFrameworkAppObjectManager::getInstance()
->get(NewDependencyInterface::class);
...
}
public function existingFunction() {
// Existing functionality
...
// Use $this->newDependency wherever the new dependency is needed
...
}
}
© 2017 Magento, Inc. Page | 33 ‘17
PHP - Prohibited Code Changes
• Modifying the default values of
optional arguments in public and
protected methods
• Removing or renaming constants
© 2017 Magento, Inc. Page | 34 ‘17
The main rule is that backwards compatibility
is more important than niceness and effort of
the implementation.
© 2017 Magento, Inc. Page | 35 ‘17
Coupling Between Objects Reaches Its Limit with
a New Dependency
© 2017 Magento, Inc. Page | 36 ‘17
We MUST do continuous Refactoring!
Backward Compatibility should not be an excuse
for not doing refactoring!
© 2017 Magento, Inc. Page | 37 ‘17
Backward Compatible Fix
*it works (most of the time), but code quality
is far from good enough
© 2017 Magento, Inc. Page | 38 ‘17
Good object oriented programming in the service layer is basically
functional programming:
• constructor injection
• immutable state
• single responsibility principle
• uniform interfaces
• value objects passed across services
Bringing these concepts to an extreme leads to single-method,
immutable objects.
© 2017 Magento, Inc. Page | 39 ‘17
© 2017 Magento, Inc. Page | 40 ‘17
Ubiquitous language matters
© 2017 Magento, Inc. Page | 41 ‘17
Why to use `execute` but not __invoke?
© 2017 Magento, Inc. Page | 42 ‘17
Proposal to move towards Functional Objects
© 2017 Magento, Inc. Page | 43 ‘17
© 2017 Magento, Inc. Page | 44 ‘17
Pros
The main reason for __invoke usage proposal was elimination of
unneeded `execute` methods,
which look a bit artificial and redundant here
© 2017 Magento, Inc. Page | 45 ‘17
Cons
No autocomplete in
PHP Storm IDE
accessing via $this
© 2017 Magento, Inc. Page | 46 ‘17
Cons
© 2017 Magento, Inc. Page | 47 ‘17
Cons
Unit tests look frustrating and non intuitive
© 2017 Magento, Inc. Page | 48 ‘17
Repositories
In Magento 2 Repositories are considered as an implementation
of Facade pattern which provides a simplified interface to a larger body
of code responsible for Domain Entity management.
The main intention is to make API more readable and reduce
dependencies of business logic code on the inner workings of a
module, since most code uses the facade, thus allowing more flexibility
in developing the system.
© 2017 Magento, Inc. Page | 49 ‘17
Repositories
© 2017 Magento, Inc. Page | 50 ‘17
© 2017 Magento, Inc. Page | 51 ‘17
© 2017 Magento, Inc. Page | 52 ‘17
Good API design
Don't make your client do anything you can do
for them (this reduces the amount of boilerplate
code your client will have)
© 2017 Magento, Inc. Page | 53 ‘17
© 2017 Magento, Inc. Page | 54 ‘17
Q & A
@iminyaylo
engcom@magento.com

Contenu connexe

Tendances

Medior Software Engineer (C #,. Net) Bizz Talk 2006 Groningen
Medior Software Engineer (C #,. Net)   Bizz Talk 2006    GroningenMedior Software Engineer (C #,. Net)   Bizz Talk 2006    Groningen
Medior Software Engineer (C #,. Net) Bizz Talk 2006 Groningen
chalikars
 

Tendances (20)

Magento Multi-Source Inventory (MSI)
Magento Multi-Source Inventory (MSI)Magento Multi-Source Inventory (MSI)
Magento Multi-Source Inventory (MSI)
 
Architecture and workflow of Multi-Source Inventory
Architecture and workflow of Multi-Source InventoryArchitecture and workflow of Multi-Source Inventory
Architecture and workflow of Multi-Source Inventory
 
Backwards Compatibility Developers Guide. #MM17NL
Backwards Compatibility Developers Guide. #MM17NLBackwards Compatibility Developers Guide. #MM17NL
Backwards Compatibility Developers Guide. #MM17NL
 
Magento 2 Automated Testing via examples of Multi-Source Inventory (MSI)
Magento 2 Automated Testing via examples of Multi-Source Inventory (MSI)Magento 2 Automated Testing via examples of Multi-Source Inventory (MSI)
Magento 2 Automated Testing via examples of Multi-Source Inventory (MSI)
 
Backward Compatibility Developer's Guide Webinar
Backward Compatibility Developer's Guide WebinarBackward Compatibility Developer's Guide Webinar
Backward Compatibility Developer's Guide Webinar
 
Awesome architectures in Magento 2.3
Awesome architectures in Magento 2.3Awesome architectures in Magento 2.3
Awesome architectures in Magento 2.3
 
Valeriy Nayda - Best Practices in Magento 2. Based on Multi Source Inventory ...
Valeriy Nayda - Best Practices in Magento 2. Based on Multi Source Inventory ...Valeriy Nayda - Best Practices in Magento 2. Based on Multi Source Inventory ...
Valeriy Nayda - Best Practices in Magento 2. Based on Multi Source Inventory ...
 
Igor Miniailo - Magento 2 API Design Best Practices
Igor Miniailo - Magento 2 API Design Best PracticesIgor Miniailo - Magento 2 API Design Best Practices
Igor Miniailo - Magento 2 API Design Best Practices
 
Testing in Magento 2
Testing in Magento 2 Testing in Magento 2
Testing in Magento 2
 
Dare to Share Magento Community Engineering
Dare to Share Magento Community Engineering Dare to Share Magento Community Engineering
Dare to Share Magento Community Engineering
 
Volodymyr Kublytskyi - Develop Product, Design Platform
Volodymyr Kublytskyi - Develop Product, Design PlatformVolodymyr Kublytskyi - Develop Product, Design Platform
Volodymyr Kublytskyi - Develop Product, Design Platform
 
Jacopo Nardiello - From CI to Prod: Running Magento at scale with Kubernetes
Jacopo Nardiello - From CI to Prod: Running Magento at scale with KubernetesJacopo Nardiello - From CI to Prod: Running Magento at scale with Kubernetes
Jacopo Nardiello - From CI to Prod: Running Magento at scale with Kubernetes
 
Experience in Magento Community Projects
Experience in Magento Community ProjectsExperience in Magento Community Projects
Experience in Magento Community Projects
 
James Zetlen - PWA Studio Integration…With You
James Zetlen - PWA Studio Integration…With YouJames Zetlen - PWA Studio Integration…With You
James Zetlen - PWA Studio Integration…With You
 
Chernivtsi Magento Meetup&Contribution day. Naida V.
Chernivtsi Magento Meetup&Contribution day. Naida V.Chernivtsi Magento Meetup&Contribution day. Naida V.
Chernivtsi Magento Meetup&Contribution day. Naida V.
 
Medior Software Engineer (C #,. Net) Bizz Talk 2006 Groningen
Medior Software Engineer (C #,. Net)   Bizz Talk 2006    GroningenMedior Software Engineer (C #,. Net)   Bizz Talk 2006    Groningen
Medior Software Engineer (C #,. Net) Bizz Talk 2006 Groningen
 
Deploy and Secure Your API Gateway with NGINX: From Zero to Hero – APCJ
Deploy and Secure Your API Gateway with NGINX: From Zero to Hero – APCJDeploy and Secure Your API Gateway with NGINX: From Zero to Hero – APCJ
Deploy and Secure Your API Gateway with NGINX: From Zero to Hero – APCJ
 
API Management for GraphQL
API Management for GraphQLAPI Management for GraphQL
API Management for GraphQL
 
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
 
Apiconf - Doc Driven Development
Apiconf - Doc Driven DevelopmentApiconf - Doc Driven Development
Apiconf - Doc Driven Development
 

Similaire à MageConf 2017, Design API Best Practices

Similaire à MageConf 2017, Design API Best Practices (20)

API design best practices
API design best practicesAPI design best practices
API design best practices
 
API Design Best Practices by Igor Miniailo
API Design Best Practices by Igor MiniailoAPI Design Best Practices by Igor Miniailo
API Design Best Practices by Igor Miniailo
 
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
 
Automated tests in Magento
Automated tests in MagentoAutomated tests in Magento
Automated tests in Magento
 
Mli 2017 technical backward compatibility
Mli 2017 technical backward compatibilityMli 2017 technical backward compatibility
Mli 2017 technical backward compatibility
 
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
 
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
 
Meaningful UI Test Automation
Meaningful UI Test AutomationMeaningful UI Test Automation
Meaningful UI Test Automation
 
Magento 2 Best Practice MLUK17
Magento 2 Best Practice MLUK17Magento 2 Best Practice MLUK17
Magento 2 Best Practice MLUK17
 
Meaningful UI Test Automation
Meaningful UI Test AutomationMeaningful UI Test Automation
Meaningful UI Test Automation
 
API First - Best Practices for consistent API management
API First - Best Practices for consistent API managementAPI First - Best Practices for consistent API management
API First - Best Practices for consistent API management
 
API Contract as Code: Rapid Development with OpenAPI
API Contract as Code: Rapid Development with OpenAPIAPI Contract as Code: Rapid Development with OpenAPI
API Contract as Code: Rapid Development with OpenAPI
 
Real Time Project
Real Time ProjectReal Time Project
Real Time Project
 
Consumer Driven Contracts and Your Microservice Architecture
Consumer Driven Contracts and Your Microservice ArchitectureConsumer Driven Contracts and Your Microservice Architecture
Consumer Driven Contracts and Your Microservice Architecture
 
Language Translations for Custom Scoped Applications in ServiceNow
Language Translations for Custom Scoped Applications in ServiceNowLanguage Translations for Custom Scoped Applications in ServiceNow
Language Translations for Custom Scoped Applications in ServiceNow
 
Building Composite Application for Lotus Notes 8
Building Composite Application for Lotus Notes 8Building Composite Application for Lotus Notes 8
Building Composite Application for Lotus Notes 8
 
DPC2007 Zend Framework (Gaylord Aulke)
DPC2007 Zend Framework (Gaylord Aulke)DPC2007 Zend Framework (Gaylord Aulke)
DPC2007 Zend Framework (Gaylord Aulke)
 
Develop web APIs in PHP using middleware with Expressive (Code Europe)
Develop web APIs in PHP using middleware with Expressive (Code Europe)Develop web APIs in PHP using middleware with Expressive (Code Europe)
Develop web APIs in PHP using middleware with Expressive (Code Europe)
 
How APIs Are Driving the New Commerce Landscape
How APIs Are Driving the New Commerce LandscapeHow APIs Are Driving the New Commerce Landscape
How APIs Are Driving the New Commerce Landscape
 
Beyond Gerrit @ Gerrit User Summit 2017, London
Beyond Gerrit @ Gerrit User Summit 2017, LondonBeyond Gerrit @ Gerrit User Summit 2017, London
Beyond Gerrit @ Gerrit User Summit 2017, London
 

Dernier

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Dernier (20)

HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 

MageConf 2017, Design API Best Practices

  • 1. © 2017 Magento, Inc. Page | 1 ‘17 API Design Best practices Igor Miniailo Magento Architect
  • 2. © 2017 Magento, Inc. Page | 2 ‘17
  • 3. © 2017 Magento, Inc. Page | 3 ‘17
  • 4. © 2017 Magento, Inc. Page | 4 ‘17 Why is API so crucial?
  • 5. © 2017 Magento, Inc. Page | 5 ‘17
  • 6. © 2017 Magento, Inc. Page | 6 ‘17
  • 7. © 2017 Magento, Inc. Page | 7 ‘17
  • 8. © 2017 Magento, Inc. Page | 8 ‘17 Semantic Versioning Version numbers are in the format MAJOR.MINOR.PATCH, where: – MAJOR indicates incompatible API changes – MINOR indicates backward-compatible functionality has been added – PATCH indicates backward-compatible bug fixes
  • 9. © 2017 Magento, Inc. Page | 9 ‘17 The backward compatibility policy applies to PHP code annotated with @api
  • 10. © 2017 Magento, Inc. Page | 10 ‘17 Public and Private code
  • 11. © 2017 Magento, Inc. Page | 11 ‘17 Public vs Private code Private code is not supposed to be used by third party modules, so, in most cases, its modifications will only trigger PATCH version bumps. Changes in public code always trigger MINOR or MAJOR version bumps.
  • 12. © 2017 Magento, Inc. Page | 12 ‘17 What examples of Public code Magento has? • PHP Interface (marked with @api) • PHP Class (marked with @api) • Javascript Interface (marked with @api) • Javascript Class (marked with @api) • Virtual Type (marked with @api) • URL paths • Console commands and their arguments • Less Variables & Mixins • Message queue topics and their data types • UI component declarations • Layout handles declared by modules • Events triggered by component (both static dynamic) • Schema of configuration types introduced by module • Structure of System Configuration fields used by module
  • 13. © 2017 Magento, Inc. Page | 13 ‘17 API vs SPI (Extension Points) A PHP Interface in Magento can be used several ways by the core product and extension developers. • As an API. is a set of interfaces and their implementations that a module provides to other modules • As a Service Provider Interface (SPI). is a set of interfaces that a module uses internally and allows their implementation by other modules. • As both. APIs and SPIs are not mutually exclusive.
  • 14. © 2017 Magento, Inc. Page | 14 ‘17 After the code is released, both API and SPI can be evolved in a backwards-compatible way. But both have their specific limitations.
  • 15. © 2017 Magento, Inc. Page | 15 ‘17
  • 16. © 2017 Magento, Inc. Page | 16 ‘17
  • 17. © 2017 Magento, Inc. Page | 17 ‘17
  • 18. © 2017 Magento, Inc. Page | 18 ‘17
  • 19. © 2017 Magento, Inc. Page | 19 ‘17
  • 20. © 2017 Magento, Inc. Page | 20 ‘17 We do not distinguish them separately. SPIs are annotated the same as APIs.
  • 21. © 2017 Magento, Inc. Page | 21 ‘17 Who decides whether interface/class belong to API or SPI? YOU
  • 22. © 2017 Magento, Inc. Page | 22 ‘17 Dependency Rules API If a module uses (calls) an API, it should be dependent on the MAJOR version. API dependency example { ... "require": { "magento/module-customer": "~100.0", // (>=100.0 <101.0.0) }, ... }
  • 23. © 2017 Magento, Inc. Page | 23 ‘17 Dependency Rules SPI If a module implements an API/SPI, it should be dependent on the MAJOR+MINOR version. SPI dependency example { ... "require": { "magento/module-customer": "~100.0.0", // (>=100.0.0 <100.1.0) }, ... }
  • 24. © 2017 Magento, Inc. Page | 24 ‘17 http://devdocs.magento.com/guides/v2.1/release-notes/backward- incompatible-changes-2.1.html
  • 25. © 2017 Magento, Inc. Page | 25 ‘17 Prohibited Code Changes
  • 26. © 2017 Magento, Inc. Page | 26 ‘17 • Interface/class removal • Public & protected method removal • Introduction of a method to a class or interface PHP - Prohibited Code Changes
  • 27. © 2017 Magento, Inc. Page | 27 ‘17 MagentoCatalogApiCategoryRepositoryInterface
  • 28. © 2017 Magento, Inc. Page | 28 ‘17 MagentoCatalogApiCategoryListInterface
  • 29. © 2017 Magento, Inc. Page | 29 ‘17 PHP - Prohibited Code Changes • Static function removal • Parameter addition in public methods • Parameter addition in protected methods
  • 30. © 2017 Magento, Inc. Page | 30 ‘17
  • 31. © 2017 Magento, Inc. Page | 31 ‘17 PHP - Prohibited Code Changes • Method argument type modification • Modification of types of thrown exceptions (unless a new exception is a subtype of the old one) • Constructor modification
  • 32. © 2017 Magento, Inc. Page | 32 ‘17 class ExistingClass { /** * @var NewDependencyInterface $newDependency */ private $newDependency; public function __construct( OldDependencyIntreface $oldDependency, $oldRequiredConstructorParameter, $oldOptinalConstructorParameter = null, NewDependencyInterface $newDependency = null ) { ... $this>newDependency = $newDependency ?: MagentoFrameworkAppObjectManager::getInstance() ->get(NewDependencyInterface::class); ... } public function existingFunction() { // Existing functionality ... // Use $this->newDependency wherever the new dependency is needed ... } }
  • 33. © 2017 Magento, Inc. Page | 33 ‘17 PHP - Prohibited Code Changes • Modifying the default values of optional arguments in public and protected methods • Removing or renaming constants
  • 34. © 2017 Magento, Inc. Page | 34 ‘17 The main rule is that backwards compatibility is more important than niceness and effort of the implementation.
  • 35. © 2017 Magento, Inc. Page | 35 ‘17 Coupling Between Objects Reaches Its Limit with a New Dependency
  • 36. © 2017 Magento, Inc. Page | 36 ‘17 We MUST do continuous Refactoring! Backward Compatibility should not be an excuse for not doing refactoring!
  • 37. © 2017 Magento, Inc. Page | 37 ‘17 Backward Compatible Fix *it works (most of the time), but code quality is far from good enough
  • 38. © 2017 Magento, Inc. Page | 38 ‘17 Good object oriented programming in the service layer is basically functional programming: • constructor injection • immutable state • single responsibility principle • uniform interfaces • value objects passed across services Bringing these concepts to an extreme leads to single-method, immutable objects.
  • 39. © 2017 Magento, Inc. Page | 39 ‘17
  • 40. © 2017 Magento, Inc. Page | 40 ‘17 Ubiquitous language matters
  • 41. © 2017 Magento, Inc. Page | 41 ‘17 Why to use `execute` but not __invoke?
  • 42. © 2017 Magento, Inc. Page | 42 ‘17 Proposal to move towards Functional Objects
  • 43. © 2017 Magento, Inc. Page | 43 ‘17
  • 44. © 2017 Magento, Inc. Page | 44 ‘17 Pros The main reason for __invoke usage proposal was elimination of unneeded `execute` methods, which look a bit artificial and redundant here
  • 45. © 2017 Magento, Inc. Page | 45 ‘17 Cons No autocomplete in PHP Storm IDE accessing via $this
  • 46. © 2017 Magento, Inc. Page | 46 ‘17 Cons
  • 47. © 2017 Magento, Inc. Page | 47 ‘17 Cons Unit tests look frustrating and non intuitive
  • 48. © 2017 Magento, Inc. Page | 48 ‘17 Repositories In Magento 2 Repositories are considered as an implementation of Facade pattern which provides a simplified interface to a larger body of code responsible for Domain Entity management. The main intention is to make API more readable and reduce dependencies of business logic code on the inner workings of a module, since most code uses the facade, thus allowing more flexibility in developing the system.
  • 49. © 2017 Magento, Inc. Page | 49 ‘17 Repositories
  • 50. © 2017 Magento, Inc. Page | 50 ‘17
  • 51. © 2017 Magento, Inc. Page | 51 ‘17
  • 52. © 2017 Magento, Inc. Page | 52 ‘17 Good API design Don't make your client do anything you can do for them (this reduces the amount of boilerplate code your client will have)
  • 53. © 2017 Magento, Inc. Page | 53 ‘17
  • 54. © 2017 Magento, Inc. Page | 54 ‘17 Q & A @iminyaylo engcom@magento.com

Notes de l'éditeur

  1. We promise to be backward compatible for classes and methods annotated with @api within MINOR and PATCH updates to our components. As changes are introduced, we annotate methods with @deprecated. The methods are removed only with the next MAJOR component version. 
  2. Let’s recap what we had with Magento 1 – where everything is an extension points. All the protected mess and so on. We can’t make changes in contract – all changes suppose to extend existing contract.
  3. Tilde = Significant Release Operator