Magento is layered software. Layered application design offers many advantages such as separation of business logic from presentation logic to simplify the customization process.
Magento 2 Architecture is divided into the following layers:
- Presentation layer
- Service layer
- Domain layer
- Persistence layer
The presentation layer is the top layer of the four layers. It contains both view elements(layouts, blocks, templates) and controllers.
The Presentation layer components call the Service layer.
The service layer is a bridge between the presentation layer and the domain layer. This is implemented using service contracts, which are defined using PHP interfaces.
Service contracts include:
- Controllers (initiated by actions of users of the storefront)
- Web services (SOAP and REST API calls)
- Third-party Magento modules using service contracts
The Domain layer contains the business logic layer of a Magento module. It does not contain database or resource-specific information. Its primary functions include:
- Defining the common Magento data objects, or models, that contain business logic.
- (Optionally) Including the implementation of service contracts, although not their definition.
Magento uses an active record pattern strategy to save data. In Magento, the model object contains a resource model that maps an object to one or more database rows. A Resource Model is responsible for performing functions such as:
- Executing all CRUD (create, read, update, delete) requests.
- Performing additional business logic. For example, a resource model could perform data validation, start processes before or after data is saved, or perform other database operations.