domain-events
Use this skill when introducing domain events, deciding how to decouple side-effects, or implementing the record-then-publish pattern in PHP apps. Triggers on questions about event naming, payloads, or cross-context ownership.
How do I install this agent skill?
npx skills add https://github.com/garrettw/php-arch-skills --skill domain-eventsIs this agent skill safe to install?
- Gen Agent Trust Hubpass
This skill is a technical guide for implementing domain events and the record-then-publish pattern in PHP applications. It provides best practices, architectural workflows, and code examples without introducing any security vulnerabilities or suspicious behaviors.
- Socketpass
No alerts
- Snykpass
Risk: LOW · No issues
What does this agent skill do?
Domain Events in PHP Applications
When to Use Domain Events (and When NOT to)
Domain Events add indirection that must justify its cost. Use them when you need eventual consistency, decoupled downstream reactions, or a history of changes. Skip them for single-action uses where direct method calls suffice.
| Use Domain Events For | Use Direct Calls For |
|---|---|
| Decoupled side effects (email, notifications, sync) | Single-step operations without downstream reactions |
| Cross-bounded-context communication | Same-context synchronous updates |
| Audit trail or event replay requirements | Stateless operations with no history needed |
| Multiple downstream triggers from one change | One downstream reaction |
Start without events. Introduce them only when coupling becomes a real maintenance or reliability problem.
System Overview
Domain Events represent facts that have already happened in the business. They decouple side effects from the primary transaction and serve as contracts between bounded contexts. This skill covers the implementation of the Record-Then-Publish pattern and the rules for defining events.
Numbered Workflows
1. Deciding to Introduce an Event
If evaluating whether a new behavior should be triggered by an event:
- Check for downstream needs. Does another bounded context need to know that a fact occurred? If yes, use an event.
- Check for non-blocking side effects. Must a side effect (like sending an email or syncing to a third party) happen without blocking the primary business transaction? If yes, use an event.
- Check for multiple triggers. Do you need to trigger multiple downstream actions from a single domain change without tightly coupling the orchestrating handler? If yes, use an event.
- If the goal is just logging or a synchronous internal call, do NOT use an event.
2. Defining the Event
If creating a new domain event class:
- Name it in the past tense. Use business terms (e.g.,
OrderPlaced,EnrollmentCompleted), not CRUD terms (StudentRecordUpdated). - Keep the payload lean. The event should carry IDs and the specific facts that changed.
- Ensure immutability. Once created, the event cannot be changed. No setters.
- Cross-Aggregate Consistency. Use events to maintain eventual consistency between aggregates. One transaction changes one aggregate and raises an event; the event triggers an update in another aggregate in a later transaction.
3. Implementing the Record-Then-Publish Pattern
If implementing an aggregate that produces events:
- Record. The domain object creates the event and holds it internally in an array of pending events. In the decider style, the domain function returns the new events instead of holding them; the handler then persists and publishes them exactly as below.
- Persist with the outbox. In one transaction, the application handler saves the aggregate and writes its released events to an outbox table. A separate relay publishes outbox entries after commit (see
reliable-messaging). - Publish only committed facts. Never publish before the commit, and never commit and then publish without an outbox when a lost event would matter. Dispatching in-process after commit is acceptable only for best-effort reactions inside the same context, where losing one is harmless.
- Reference Examples. See Event Examples for structural patterns.
4. Handling Cross-Context Ownership
If passing events between bounded contexts:
- Own and Publish in Source Context. The bounded context that owns the fact publishes a versioned contract for it (e.g.,
BillingownsSubscriptionPaidand publishesSubscriptionPaidV1). - Consume in Target Context. The consuming context consumes that published contract, not the source's domain event, and translates it into a local command (e.g.,
EntitlementsconsumesSubscriptionPaidV1and issuesGrantAccessCommand). - Place the contract where consumers can depend on it. Published, versioned contracts live in the Shared Kernel or a
Contractsnamespace. The source translates its internal event into the contract (for example, in the outbox relay); consumers depend only on the contract, never on the source context’s domain classes.
Boundaries
Always Do
- Always use the record-then-publish pattern. Never publish a domain event directly from deep within a domain object.
- Always use the ubiquitous language to name events.
Ask First
- Ask before adding an event listener that executes heavy business logic directly inline. (Prefer queuing a job or delegating to a proper handler).
Never Do
- Never pass ORM entities or framework request objects inside an event payload.
- Never allow a listener to modify the event payload.
- Never attempt to cancel or roll back the primary transaction that emitted the event from within a listener.
- Never let one context import another context’s domain classes to consume its events.
Related Skills
- reliable-messaging: transactional outbox, duplicate delivery, retries, and consumer idempotency.
- persistence transaction boundaries: commit ownership and outbox motivation.
How can the creator link this skill?
Add the canonical catalog link to the repository README so users can inspect current installs and available audits. The publishing guide covers the complete discovery path.
<a href="https://skillzs.dev/skills/garrettw/php-arch-skills/domain-events">View domain-events on skillZs</a>