CQRS & Domain-Driven Design
Relevant source files
The following files were used as context for generating this wiki page:
PrestaShop utilizes Command Query Responsibility Segregation (CQRS) and Domain-Driven Design (DDD) to manage complex business logic and state transitions. This architecture separates write operations (Commands) from read operations (Queries), ensuring that the domain model focuses on business rules while the read model is optimized for data retrieval.
Sources: .ai/generated/cqrs.md, README.md
Architectural Components
The system architecture consists of several layers that isolate domain logic from infrastructure and presentation concerns.
Command Bus
The CommandBusInterface defines the contract for dispatching commands and queries within the application. It acts as a central entry point for executing domain operations.
handle($command): Dispatches a command or query to its associated handler and returns the result.
Sources: src/Core/CommandBus/CommandBusInterface.php:27-35
Domain Commands
Commands represent specific intent to change the state of the system. For example, AddProductCommand encapsulates all necessary data to create a new product within the store.
Sources: src/Core/Domain/Product/Command/AddProductCommand.php:33-40
Architecture Flow Diagram
The following diagram illustrates the flow of a request through the CQRS infrastructure.
The diagram shows the decoupling between the request initiator and the business logic execution via the Command Bus.
Sources: src/Core/CommandBus/CommandBusInterface.php, .ai/generated/cqrs.md
Domain-Driven Design (DDD) Implementation
The project implements DDD principles by grouping logic into specific domain namespaces. This structure ensures that business rules related to products, categories, or pages are isolated.
Domain Entities and Objects
Business data is encapsulated in commands that serve as Data Transfer Objects (DTOs) for the domain.
| Component | Description | Reference File |
|---|---|---|
| Command | Encapsulates the intent and data for a state change. | AddProductCommand.php |
| Command Bus | Dispatches the command to the appropriate handler. | CommandBusInterface.php |
| Domain Logic | Validates and processes business rules. | src/Core/Domain/ |
Sources: src/Core/Domain/Product/Command/AddProductCommand.php, src/Core/CommandBus/CommandBusInterface.php
CRUD Operations and State Management
The application manages resource lifecycles through standardized CRUD operations, often tested via Webservice (WS) or Back Office (BO) interfaces.
Category Management Example
State transitions for categories involve specific steps:
- Create: Invoking a POST request with XML data to the category endpoint.
- Read: Fetching details via GET to verify the current state.
- Update: Using PUT to modify existing attributes like name, description, or status.
- Delete: Removing the resource and verifying its absence.
Sources: tests/UI/campaigns/functional/WS/05_categoriesCRUD.ts:98-385
Data Flow for Category Updates
This sequence demonstrates the synchronization between API-driven state changes and their reflection in the administrative interface.
Sources: tests/UI/campaigns/functional/WS/05_categoriesCRUD.ts:273-350
Testing Infrastructure
The testing suite validates the CQRS and DDD implementation by simulating user interactions and API calls.
Functional Verification
Tests use "Fakers" to generate domain-compliant data for creating categories and pages, ensuring that the domain model handles various inputs correctly.
FakerCMSCategory: Generates random but valid category data.FakerCMSPage: Generates random page content for CMS testing.
Sources: tests/UI/campaigns/functional/BO/08_design/04_pages/01_CRUDPageCategory.ts:31-40
Result Validation Logic
| Step | Action | Expected Outcome |
|---|---|---|
| Command Dispatch | handle(Command) | Success Message or Result Object |
| XML Validation | utilsXML.isValid(response) | Returns true |
| UI Filter | boCategoriesPage.filterCategories(...) | Grid displays 1 matching element |
Sources: tests/UI/campaigns/functional/WS/05_categoriesCRUD.ts:114-118, tests/UI/campaigns/functional/BO/08_design/04_pages/01_CRUDPageCategory.ts:89-94
Summary
The PrestaShop architecture utilizes CQRS and DDD to provide a structured, testable, and maintainable codebase. The Command Bus decouples the application layer from the domain layer, while DDD ensures that business logic remains the core focus of the system. This separation allows for complex operations, such as category and product management, to be executed consistently across both the API and the administrative UI.
Sources: src/Core/CommandBus/CommandBusInterface.php, .ai/generated/cqrs.md, tests/UI/campaigns/functional/WS/05_categoriesCRUD.ts