Skip to content

Response value handlers

A returned value can represent work for a server extension rather than a client response. Register a ResponseValueHandler when that interpretation belongs to an integration; keep ordinary command logic on the command.

AddResponseValueHandler(name, factory, keys...) registers a dynamic consumer. RegisterResponseValueHandler[T] additionally restricts applicability to exact concrete T or values implementing interface T; its CanHandle predicate still decides at runtime. Factories run only after Handle, never during Build or Validate, and return borrowed instances. Resources own disposal.

The methods are:

  • CanHandle(CommandContext, any) bool determines applicability.
  • Handle(context.Context, *Invocation, any) (Result[NoResponse], error) consumes a matching value and contributes a verdict.
  • Optional UpdateContext(context.Context, *Invocation, any) error writes context values before response selection.

Every matching handler runs in registration order. This is additive dispatch, not first-match selection. Shared instances must support concurrent calls; do not capture a request scope in a long-lived shared consumer.

  1. Admit leaves through any registered return policies, then flatten explicit outcome graphs in declaration order. Ordinary typed nil leaves disappear; admitted server-consumed typed nils fail. Scalar zeros remain.
  2. Run matching context updaters before selecting a response.
  3. Reserve an explicit Respond response or select at most one unhandled Values leaf.
  4. Reevaluate consumer predicates with the selected response installed.
  5. Preflight ambiguity, required consumption, and built-in controls before executing effects.

A predicate may depend on the selected response. With Values, declaration order matters: put the response before an effect that becomes consumable only when that response exists. Respond reserves the response explicitly.

AddReturnAdmission(name, ReturnAdmission{...}) supplies copied exact-type catalog metadata through Types, an optional runtime Check, or a dynamic Check with no Types. Build never calls Check. A static claim compiles a raw command return to ResponseNone unless the command has an explicit contract; dynamic classification leaves metadata unknown.

Admission runs before every context updater and consumer. ServerConsumedReturn requires a real consumer and rejects typed nil rather than silently dropping it. Ordinary nil responses keep their existing behavior. Respond values and built-in controls bypass admission; an explicit response remains a client response. Exact pointer and value types are separate claims, and ordinary slices are never guessed to be event collections.

Check validates/classifies only: it must not perform external effects. All leaves must pass before any consumer runs.

Only singular validation.Result and authorization.Decision are built-in plain controls. A returned command result DTO or validation collection remains an ordinary payload unless you explicitly adopt it with Control or register a consumer.

Dynamic consumers do not establish an unconditional no-response contract. Use Outcome[R], WithResponseType, or WithNoResponse when tooling or typed callers need an enforceable contract. See response examples for the return grammar.

Events-as-values require a real consumer. The compiled ExampleRespond records an event in memory; it is not Chronicle persistence, append, or a transaction. The optional Chronicle integration recognizes its registered events and supplies the transaction consumer.