PrestaShop Developer Conference
PrestaShop Developer Conference
Paris, France - November 6, 2024
Let's talk code, commerce and open source.

Domain services

As mentioned in CQRS principles, a Command is a representation of a single domain use case. To prevent Command Handlers from duplicating code and depending on each other, it is usually best to avoid implementing the actual logic in the Command Handler itself, and delegate it to Domain Services instead. These services are responsible for logic such as:

  1. Persisting the entities (a.k.a. ObjectModel in current prestashop architecture) data into database
  2. Validating the entities
  3. Performing various modifications of related data structures etc.

Here are some principles for implementing a Domain Service:

  1. It MUST use the existing ObjectModel for writes, as long as such class exists.
  2. If it uses any legacy service or object model, then it MUST be placed in the Adapter namespace.
  3. If it needs to perform a sql query and related ObjectModel exists, then this query MUST be delegated to the appropriate repository class which must ensure that no legacy exceptions are thrown. If the related ObjectModel already implements such method, then the repository must delegate its implementation to the ObjectModel.
    Some reusable methods are present in AbstractObjectModelRepository.
  4. If ObjectModel contains fields validation, it MUST be validated by a dedicated validator class before persisting to database (e.g. when using add/update/save methods). It ensures that legacy PrestashopException is not bubbling up and each validation error can be identified by a dedicated exception or exception code.
    Some reusable methods are present in AbstractObjectModelValidator.

Code examples

ObjectModel repositories

  1. ProductRepository
  2. ProductSupplierRepository
  3. VirtualProductFileRepository
  4. SpecificPriceRepository

ObjectModel validators

  1. ProductValidator
  2. ProductSupplierValidator
  3. VirtualProductFileValidator
  4. SpecificPriceValidator

Product domain services

  1. Product update services
  2. Virtual product update services
  3. Combination update services
  4. Combination create services

Service usage in CommandHandler examples

  1. Combination command handlers
  2. Virtual product command handlers
  3. Product stock command handlers