Domain Events and Domain Services
Capture business facts with domain events and services.
Capturing business facts
Not every piece of behavior fits inside a single entity. Domain Events capture meaningful business facts that have already happened (OrderPlaced), letting other parts of the system react. Domain Services hold domain logic that doesn't naturally belong to one entity or value object. This lesson shows how both keep your model expressive without bloating aggregates.
What is a Domain Event
A Domain Event is an immutable record of something that occurred in the domain, named in the past tense using the ubiquitous language: OrderPlaced, PaymentReceived, CustomerRelocated. It carries the data describing the fact plus the time it happened. It is a value object: created once, never changed.
<?php
final class OrderPlaced {
public function __construct(
public readonly string $orderId,
public readonly int $totalCents,
public readonly DateTimeImmutable $occurredOn = new DateTimeImmutable()
) {}
}
$e = new OrderPlaced('o1', 1300);
echo $e->orderId, ' @ ', $e->occurredOn->format('H:i'), PHP_EOL;
All lessons in this course
- DDD Building Blocks: Entities and Value Objects
- Aggregates, Repositories and Factories
- Domain Events and Domain Services
- Bounded Contexts and Context Mapping