Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 770b476

Browse files
authored
Merge pull request #3421 from magento-architects/kh_tech-vision
Added service contract technical guidelines
2 parents c4fade1 + 0327977 commit 770b476

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed

_data/toc/coding-standards.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pages:
3535
url: /coding-standards/technical-guidelines.html
3636

3737
- label: Technical vision
38+
url: /coding-standards/technical-vision/index.html
3839
children:
3940

4041
- label: Web Api
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
group: coding-standards
3+
title: Magento technical vision
4+
---
5+
6+
The Magento technical vision is a collection of documents that describe the desired state of the Magento platform.
7+
8+
Each individual technical vision document relates to a set of modules that arelogically grouped together. Individual documents typically describe a component's place in the system, its architecture, extension scenarios. and a list of invariants that must be preserved at all times during component refactoring or extension to ensure consistency.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
group: coding-standards
3+
title: Magento technical vision
4+
---
5+
6+
The Magento technical vision is a collection of documents that describe the desired state of the Magento platform.
7+
8+
Each individual technical vision document relates to a set of modules that arelogically grouped together. Individual documents typically describe a component's place in the system, its architecture, extension scenarios. and a list of invariants that must be preserved at all times during component refactoring or extension to ensure consistency.

guides/v2.2/coding-standards/technical-guidelines.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,77 @@ class View extends Template
532532

533533
### 6.4. Service Contracts (Application) layer
534534

535-
We are reviewing this section and will publish it soon.
535+
6.4.1. Location of API interfaces
536+
537+
6.4.1.1. Service contract interfaces SHOULD be placed in separate API modules. The other modules will depend on the API module, and implementations could be easily swapped via `di.xml`. API module namess must end with the `Api` suffix. For example, if a module is named `MyModule`, its APIs SHOULD be declared in a module named `MyModuleApi`.
538+
539+
6.4.1.2. Service interfaces that should be exposed as web APIs MUST be placed under the `MyModuleApi/Api` directory. Service data interfaces MUST be placed under `MyModuleApi/Api/Data`. Directories under `MyModuleApi/Api` SHOULD NOT be nested.
540+
541+
6.4.1.3. All other APIs, including explicit extension points such as Chain or Composite implementations, MUST be placed under `MyModuleApi/Model`.
542+
543+
6.4.2. Service Interface Structure
544+
545+
6.4.2.1. Methods that have similar names MUST serve similar purposes across different services, but they still MAY have different signatures.
546+
547+
6.4.2.2. Service contracts SHOULD NOT be used for read scenarios on the storefront. Instead, GraphQL SHOULD be used for storefront scenarios. Check out [web API technical vision]({{ page.baseurl }}/coding-standards/technical-vision/webapi.html) for more details.
548+
549+
6.4.2.3. Each service interface SHOULD declare a single public method. An interface name SHOULD reflect the task or action to be performed. For example, `Magento\InventoryApi\Api\StockSourceLinksDeleteInterface::execute(array $links)`. The only exception is a Repository API, which MAY be added for convenience and MUST be limited to singular CRUD operations and `getList($searchCriteria)`.
550+
551+
6.4.3. Service Method Signature
552+
553+
6.4.3.1. Strict typing is enforced for Service and Data interfaces located under `MyCompany/MyModuleApi/Api`. Only the following types are allowed:
554+
555+
* Scalar types: `string` (including Date and DateTime); `int`; `float`; `boolean`
556+
557+
* Data interfaces
558+
559+
* One-dimensional indexed arrays of scalars or data interfaces: for example `string[]`, `\MyCompany\MyModuleApi\Api\Data\SomeInterface[]`. Hash maps (associative arrays) are not supported.
560+
561+
* Nullable scalars or data interfaces: for example `string|null`. Using just `null` is prohibited.
562+
563+
* `void`
564+
565+
6.4.3.2. Service contracts SHOULD support batch data processing. For example, an entity persisting method SHOULD accept an array of entities to persist instead of a single entity. Customizations implemented through plugins SHOULD be adjusted respectively.
566+
567+
6.4.3.3. Batch retrieval operations MUST accept `SearchCriteriaInterface` and return `SearchResultInterface` to support pagination.
568+
569+
6.4.3.4. Batch operations that modify state MUST accept an array of entities and return a response object that contains:
570+
571+
* An array of successfully processed items
572+
573+
* An array of items with retriable errors
574+
575+
* An array of items with non-retriable errors
576+
577+
6.4.3.5. Batch operations that modify state SHOULD be implemented in the most performant manner and SHOULD NOT load modified entities to generate response.
578+
579+
6.4.3.6. Asynchronous invocation of the command services SHOULD be supported by the web API framework.
580+
581+
6.4.3.7. Operation UUID MAY be provided by the client during service invocation. UUID MUST allow the client to get the operation status information.
582+
583+
6.4.3.8. Data objects returned by service contracts SHOULD be fully loaded to ensure consistency.
584+
585+
6.4.4. Service Implementation
586+
587+
6.4.4.1. Service data interfaces SHOULD extend from `Magento\Framework\Api\ExtensibleDataInterface`. The only exception is when extensibility is not desired, such as in case of value-objects.
588+
589+
6.4.4.2. Extensible data interfaces MUST NOT form hierarchies. If interface `MyInterface` extends `ExtensibleDataInterface`, there must be no interfaces extending `MyInterface`. Otherwise, a list of extension attributes will be shared for all extensible interfaces in the hierarchy.
590+
591+
6.4.4.3. Service implementations and plugins MUST NOT rely on storage-specific integrity features, such as foreign key constraints.
592+
593+
6.4.4.4. Replacement strategy SHOULD be used to persist main entity fields/attributes, child entities, and relation links.
594+
595+
6.4.4.5. During update operations, web APIs using the`PATCH` HTTP method and all action controllers that accept entities SHOULD pre-load them first, then merge the request data, and provide the full data to the service.
596+
597+
6.4.4.6. If a service method needs to modify the argument, the original argument object MUST NOT be modified and its copy SHOULD be modified instead.
598+
599+
6.4.4.7. Services SHOULD NOT apply ACL rules to methods or returned data.
600+
601+
6.4.4.8. If a store has multiple scopes (websites, stores), then each call MUST persist an entity in a single scope only. If an entity needs to be saved in multiple scopes, then multiple calls SHOULD be made.
602+
603+
6.4.4.9. Service contracts SHOULD NOT apply presentation layer formatting to the returned data.
604+
605+
6.4.4.10. Service data interfaces MUST NOT contain any business logic. They SHOULD represent a container of data that is transferable over the wire. All the business logic SHOULD be moved to services.
536606

537607
## 7. Configuration
538608

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../v2.1/coding-standards/technical-vision/index.md
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../v2.2/coding-standards/technical-vision/index.md

0 commit comments

Comments
 (0)