ADR-BW-009: Use local CQRS handlers and the established Mediator
Reuse CQRS and Mediator.SourceGenerator for in-process request handling without treating local messages as service integration contracts.
| Field | Value |
|---|---|
| Status | Proposed |
| Date | 2026-11-07 |
| Owners | Architecture Enabling TeamTeamArchitecture Enabling TeamEnabling team facilitating BookWorm's architecture governance, ADRs, arc42 documentation, engineering standards, testing...Open docs |
| Affected Resources | BookWormDomainv1.0.0BookWormCore e-commerce domain for the BookWorm online bookstore, coordinating catalog discovery, customer baskets, order fulfil...Ownerarchitecture-enabling-teamDiagramRepositoryOpen docs |
| Related ADRs | ADR-BW-011: Reuse request validation and structured HTTP errorsDecision Recordv1.0.0ADR-BW-011: Reuse request validation and structured HTTP errorsValidate applicable requests before handlers and expose predictable HTTP problem responses without leaking internal exce...Ownerarchitecture-enabling-teamOpen docs, ADR-BW-016: Verify behavior, contracts, and boundaries in separate layersDecision Recordv1.0.0ADR-BW-016: Verify behavior, contracts, and boundaries in separate layersCombine focused business tests, consumer/provider contracts, architecture checks, schema validation, and high-value user...Ownerarchitecture-enabling-teamOpen docs |
Context & Problem Statement
HTTP and gRPC endpoints require consistent validation, logging, tracing, and transaction boundaries. Placing business logic directly inside endpoint definitions duplicates cross-cutting plumbing and complicates unit testing, while sharing internal command classes across services leaks private implementation details.
Decision Drivers
- Testability: Unit-test commands and queries independently of HTTP/gRPC transport.
- Zero reflection overhead: Use compile-time source generation (
Mediator.SourceGenerator) over runtime reflection. - Consistent pipelines: Apply uniform validation, logging, and transaction behaviors.
Options Considered
| Option | Pros | Cons |
|---|---|---|
Vertical-slice CQRS handlers via Mediator.SourceGenerator | Compile-time dispatch, clean testing, uniform pipeline behaviors | Contributors must follow the vertical-slice handler convention |
Alternative runtime-reflection mediator (MediatR) | Familiar to some developers | Runtime reflection overhead and licensing/ecosystem divergence |
| Inline logic in minimal API endpoints | Fewer files per endpoint | Bloated endpoints, duplicated validation, harder unit testing |
| Share internal CQRS commands across services | Avoids contract mapping | Couples service deployments and breaks bounded-context isolation |
Decision & Consequences
Chosen Option: Organize in-process commands and queries as small, service-local vertical-slice handlers dispatched through Mediator.SourceGenerator. Internal commands and queries remain private to their owning service and are never exposed as cross-service integration contracts.
- Positive: Clean separation between transport endpoints and business logic; fast compile-time dispatch.
- Negative: Contributors must learn and adhere to the repository’s vertical-slice conventions.
Compliance Criteria
- New backend endpoints must delegate to service-local CQRS handlers via
Mediator.SourceGenerator(do not addMediatR). - Internal command/query types must not be referenced by other services.
- Cross-cutting validation and logging must live in shared pipeline behaviors rather than repeated per handler.