059
DDD behavioral

Domain Service

Reference Wikipedia ↗
Domain Service — class diagram
Plate 059 class diagram

The Domain Service pattern encapsulates complex domain logic that doesn’t naturally belong to any specific entity or value object. It serves as a central point for operations that involve multiple entities or intricate business rules, preventing these behaviors from cluttering up the domain model. This keeps entities focused on their core data and simple behaviors, enhancing maintainability and readability.

Usage

This pattern is beneficial when dealing with transactions spanning multiple entities, calculations requiring data from several domain objects, or orchestration of complex processes within the domain. It’s often used in situations where a single entity cannot logically own or implement the required logic. Common use cases include order fulfillment processes, complex pricing calculations, financial transactions, and any significant business workflow.

Examples

  • E-commerce Order Fulfillment: Consider an e-commerce system where fulfilling an order requires updating inventory levels across multiple warehouses, processing payment via a third-party gateway, and creating shipping labels. Each of these operations involves different entities (Order, Product, Warehouse, Payment, Shipment). A OrderFulfillmentService can orchestrate these steps, ensuring atomicity and consistency without burdening the Order entity with this complex logic.

  • Banking Account Transfer: When transferring funds between bank accounts, several things need to happen: debiting from the source account, crediting to the destination account, and potentially logging the transaction. A FundsTransferService can be responsible for this operation, ensuring that both debit and credit operations succeed or fail together, thereby maintaining financial consistency. It would interact with Account entities, potentially a Transaction entity, and perhaps an AuditLog service.

Specimens

awaiting submission

No language specimens have been submitted for this pattern yet. Contribute one.