Skip to content

A use case is one domain operation

Accepted. This record describes the current envelope (Operation and Plan). It supersedes the first design, a sealed Result with a type-state pipeline, which docs/usecase-pattern.md still describes and the written conventions still point to.

2026-06-25 (the envelope was enforced across the platform and the Go SDK that day; the TypeScript and PHP SDKs followed the same day).

The platform’s central promise is that an event exists if, and only if, the change it describes was committed, and that every change leaves an audit record. If that depended on each handler remembering to write the event and the audit row in the same transaction, one handler would eventually forget.

The first design tried to make this a compile-time property. A sealed Result type could only be constructed by the unit-of-work’s Commit, and type-state wrappers (validated, authorized) forced validate, then authorize, then execute. Working it through showed the type-state added awkward APIs for little safety: an exported constructor let a use case build a Validated value directly, so the wrapper enforced ordering of calls but not that validation had really happened. The load-bearing invariant was the seal on the success value, not the ordering.

The word “use case” also drifted toward meaning “a database write”. A change such as “suspend a user” is a business operation that happens to write a row, an event and an audit entry, not three writes.

A use case is one domain operation, named for what the business does, expressed as a value with four phases that a runner executes in a fixed order:

  1. Validate: the command’s shape (presence, format, length). Pure; loads nothing.
  2. Authorize: resource-level access. Required. An operation that is deliberately open says so explicitly (Public); one that omits it fails closed at run time and is flagged by the uowseal analyzer.
  3. Execute: loads what the decision needs, applies the business rules, and returns either a rejection or a plan: the aggregate to save or delete, the domain event, and optionally dispatch jobs.
  4. Apply: done by the runner, not the use case. It opens one transaction and writes the aggregate, then the event, then dispatch jobs, then one audit row, and commits.

Plan is a sealed interface: its only method is unexported, so the only constructors are Save, Delete, Emit, SaveAll and Sync, and the only consumer is the runner. An operation therefore cannot reach the database except by returning a plan, and there is no path to a persisted aggregate that does not also write the event and audit row in the same transaction.

The same shape exists in the Go, TypeScript and PHP SDKs and in both application frameworks, whose contract adds optimistic locking, a declared conflict policy and client scoping. Errors carry a kind (validation, business rule, authorization, not found, conflict, internal) that maps to an HTTP status.

A closed list of infrastructure paths bypasses the envelope because wrapping them would emit recursive events: event and dispatch-job ingest, stream projection, delivery lifecycle transitions, outbox processing, token storage, seeding and scheduled-job firings. Adding to the list needs a design discussion.

Good

  • “Row written implies event and audit written” holds by construction.
  • A use case is easy to test: its decision is a value that can be inspected, with no database needed.
  • The runner does the risky work uniformly: ordering, error mapping, secret redaction in audit data and, in the frameworks, conflict retries.
  • A forgotten authorization check becomes visible: either a visible Public or a failing operation.
  • One mental model across languages.

Bad

  • More ceremony than a function that writes a row. Small operations still need a command, an event and a plan.
  • The seal depends on Go’s unexported-method idiom and on an analyzer for the cases the compiler cannot see. A determined author can still write directly to a table from a handler; the analyzer and review stop it.
  • The older write-up still exists and still shows uow.Commit; readers must follow the envelope in the SDK package documentation instead.
  • Handlers write directly and publish events afterwards. The failure this exists to prevent.
  • Database triggers writing events. Hides business meaning in the schema, and the event would be “row changed”, not “order placed”.
  • Type-state pipeline in the compiler (the first design). Rejected as above: complexity for negligible extra safety.
  • A framework-wide interceptor for transactions. Gives atomicity but not the event, audit or the authorization requirement.