-
Notifications
You must be signed in to change notification settings - Fork 248
When should an interface go into the Model directory and when should it go in the Api directory?
In Magento we want to decouple modules by layers of N-tier architecture to provide better possibilities of replaceability or switch-ability of some parts of the system (i.e., substitute out of the box implementation with custom one, change UI, disable UI at all if we want to deploy headless integration and so on ) currently, we see the next 4 parts:
- API
- Implementation of Business Logic for these API
- Admin Ui
- Frontend UI
Here you can read more about Modularity Desirable State in Magento 2.
The main requirement we must to follow, applying this design concept is that
All the modules should depend on the API module, while implementations could be easily swapped via di.xml
in most of Magento modules where Service Layer is already established (Customer, Catalog etc) you will notice that all the services and interfaces under the Api
namespace are the services which supposed to be accessible via Web API (SOAP, REST) and we applied the same logic when started to decompose MSI modules as well.
But along with Interfaces and Services which supposed to be accessible via Web API there are still extension points which don’t. For example, in MSI we applied new Validation approach having ValidationChain
extension point filled with different validation checks, each of which should be a subtype of validation interface
\Magento\InventoryApi\Model\SourceItemValidatorInterface
.
Even external modules could add particular validation to the chain. For example, Default Stock - Default Source validations are implemented by InventoryCatalog
module, but not Inventory
.
But along with that SourceItemValidatorInterface
is not going to be reachable via Web API, so clients of the system which just USE
the system interfaces - no need to know about some validation which happens under the hood, just modules which need to add own validation logic should know about this interface.
So that we decided to segregate these kind of interfaces and put:
- all the services and DTOs accessible via Web API under the
Api
namespace - all the extension points which are not accessible via Web API under the
Model
namespace.
Sometimes you would even find class
in API module under the Model
namespace , for example,
\Magento\InventoryApi\Model\StockValidatorChain
we did this because this class is an extension point via DI for other modules , like this
<type name="Magento\InventoryApi\Model\StockValidatorChain">
<arguments>
<argument name="validators" xsi:type="array">
<item name="name" xsi:type="object">Magento\Inventory\Model\Stock\Validator\NameValidator</item>
</argument>
</arguments>
</type>
if we left StockValidatorChain
in the Magento\Inventory
that would mean, that 3rd party developer who would like to fully substitute Inventory implementation still have to keep original Inventory
module in the codebase
but now, having all the Web APIs and extension points located under the roof on InventoryApi
- non of the other modules need to depend on implementation Magento\Inventory
, so that it could be easily substituted by another one and totally eliminated from the specific deployment
Multi-Source Inventory developed by Magento 2 Community
- Technical Vision. Catalog Inventory
- Installation Guide
- List of Inventory APIs and their legacy analogs
- MSI Roadmap
- Known Issues in Order Lifecycle
- MSI User Guide
- 2.3 LIVE User Guide
- MSI Release Notes and Installation
- Overview
- Get Started with MSI
- MSI features and processes
- Global and Product Settings
- Configure Source Selection Algorithm
- Create Sources
- Create Stock
- Assign Inventory and Product Notifications
- Configure MSI backorders
- MSI Import and Export Product Data
- Mass Action Tool
- Shipment and Order Management
- CLI reference
- Reports and MSI
- MSI FAQs
- DevDocs Documentation
- Manage Inventory Management Modules (install/upgrade info)
- Inventory Management
- Reservations
- Inventory CLI reference
- Inventory API reference
- Inventory In-Store Pickup API reference
- Order Processing with Inventory Management
- Managing sources
- Managing stocks
- Link and unlink stocks and sources
- Manage source items
- Perform bulk actions
- Manage Low-Quantity Notifications
- Check salable quantities
- Manage source selection algorithms
- User Stories
- Support of Store Pickup for MSI
- Product list assignment per Source
- Source assignment per Product
- Stocks to Sales Channel Mapping
- Adapt Product Import/Export to support multi Sourcing
- Introduce SourceCode attribute for Source and SourceItem entities
- Assign Source Selector for Processing of Returns Credit Memo
- User Scenarios:
- Technical Designs:
- Module Structure in MSI
- When should an interface go into the Model directory and when should it go in the Api directory?
- Source and Stock Item configuration Design and DB structure
- Stock and Source Configuration design
- Open Technical Questions
- Inconsistent saving of Stock Data
- Source API
- Source WebAPI
- Sources to Sales Channels mapping
- Service Contracts MSI
- Salable Quantity Calculation and Mechanism of Reservations
- StockItem indexation
- Web API and How To cover them with Functional Testing
- Source Selection Algorithms
- Validation of Domain Entities
- PHP 7 Syntax usage for Magento contribution
- The first step towards pre generated IDs. And how this will improve your Integration tests
- The Concept of Default Source and Domain Driven Design
- Extension Point of Product Import/Export
- Source Selection Algorithm
- SourceItem Entity Extension
- Design Document for changing SerializerInterface
- Stock Management for Order Cancelation
- Admin UI
- MFTF Extension Tests
- Weekly MSI Demos
- Tutorials