creating-domain-service ยท diff
git:20260922.00e7bbe to git:20260922.7ecf42a
7 added, 17 removed. Audit A to A.
---
name: creating-domain-service
- description: Create a Domain Service when domain behavior cannot naturally belong to one entity or value object.
+ description: Create or refactor business calculations and rules spanning multiple domain objects when no single entity or value object naturally owns the behavior. Use when assessing a growing service class.
---
- # Creating a Domain Service
-
- Create a Domain Service only when business behavior belongs to the Domain but cannot naturally be owned by a single Domain model or Value Object.
-
- Place it under:
-
- App\Domain\<Capability>\Services
-
- Prefer behavior on Domain models whenever there is a clear owner.
+ # Domain services
- Do not use Domain Services as generic containers for business logic.
+ First look for a model or Value Object that naturally owns the behavior. Create `App\Domain\<Capability>\Services` only when a cohesive business operation has no such owner.
- Avoid generic classes such as:
+ For example, `OrderCancellationFee` may calculate a fee using an order and a cancellation policy. It computes a business result; `CancelOrder` authorizes, coordinates, saves, and handles follow-up work.
- OrderService
- UserService
- PaymentService
+ Avoid generic `OrderService`, `UserService`, or `PaymentService` containers. Do not extract a service merely because a method is long or used twice.
- unless the class represents a cohesive domain concept.
+ Domain services must not depend on Application, Infrastructure, or delivery code. If a business calculation genuinely needs an external capability, define its contract under `App\Domain\<Capability>\Contracts`, implement it in Infrastructure, and bind it in a Laravel service provider. Do not hide an adapter lookup behind a facade or container call.
- Domain Services must not depend on HTTP or Application Actions.
+ Use domain exceptions for rejected rules. Leave persistence coordination and transactions to Application. Direct collaboration with models in other capabilities is allowed within the same bounded context.