---
title: Domain principles and boundaries
summary: The guiding principles BookWorm uses to keep ownership, contracts, and change propagation consistent across its bounded contexts.
version: 1.0.0
type: guides
order: 3
badges:
  - content: Architecture
    backgroundColor: green
    textColor: green
---

BookWorm enforces strict bounded-context isolation so every business fact has one authoritative owner, every derived view can be rebuilt safely, and every cross-service interaction uses an explicit versioned contract. These principles prevent shared-database coupling and keep operational work (scheduling and email delivery) cleanly separated from core commerce state.

## Context and motivation

As a microservices platform grows, reading or mutating another service's database table directly is the fastest way to create hidden coupling and data corruption. BookWorm eliminates shared-table shortcuts by pairing clear domain ownership rules with automated verification in CI (`ArchUnitNET.TUnit`, Pact consumer/provider tests, `buf breaking`, and `turbo boundaries`).

## How it works

This diagram summarizes the four boundary rules governing data ownership and cross-context communication in BookWorm.

<MermaidFileLoader file="mermaids/03-domain-principles-and-boundaries-diagram-1.mmd" />

### Four guiding principles

- **One authoritative source of truth:** Each business record belongs to one bounded context—books to [[domain|catalog]], baskets to [[domain|shopping]], accepted orders to [[domain|ordering]], and individual reviews to [[domain|reviews]].
- **Derived views are disposable and rebuildable:** Catalog rating summaries, Qdrant vector indexes, FusionCache entries, and Marten `OrderSummary` views exist for fast reads and search. They never replace authoritative records and can be regenerated if they drift.
- **Explicit contracts over shared storage:** Services never read or write another context's database. Cross-context communication uses versioned REST (`/api/v1/*`), gRPC (`book.v1`, `basket.v1`), MCP (`/mcp`), A2A gRPC, or CloudEvents Kafka topics.
- **Operational work never mutates business state:** Scheduled jobs (`ProcessVectorBatchJob`, `HeartbeatJob`) and email delivery (`Notification` with MailKit resilience and Kafka DLQs) never alter authoritative `Order` or `Book` business status.

### Ownership matrix

| Bounded context                | Authoritative ownership                                                                        | Explicitly out of scope                                                     |
| :----------------------------- | :--------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------------- |
| **[[domain\|catalog]]**        | Book metadata, taxonomies, pricing, availability, cover blobs, and derived search/rating views | Individual customer reviews, baskets, and accepted order snapshots          |
| **[[domain\|shopping]]**       | Customer-scoped mutable baskets (`Redis`) and pre-order shopping intent                        | Accepted orders, order lifecycle state, and product master data             |
| **[[domain\|ordering]]**       | `Buyer` profiles, `Order` records, purchase-time item snapshots, and lifecycle transitions     | Mutable baskets, live catalog descriptions, and external payment settlement |
| **[[domain\|reviews]]**        | Individual `Feedback` documents (`Cosmos DB`) and AI feedback summaries                        | Authoritative book metadata and Catalog's derived rating summary            |
| **[[domain\|conversational]]** | AG-UI session state (`chatting-blob`), routing decisions, and agent orchestration              | Catalog book records, baskets, billing, and authoritative orders            |
| **[[domain\|platform]]**       | Quartz schedules (`schedulerdb`), notification idempotency (`Redis`), and SMTP submission      | Business records owned by Catalog, Shopping, Ordering, or Reviews           |

### Automated boundary enforcement

BookWorm validates these principles automatically in CI rather than relying on manual code review alone:

- **Backend architecture rules:** `tests/BookWorm.ArchTests` (`ArchUnitNET.TUnit`) checks cross-service dependency isolation, domain entity/value-object rules, and handler conventions.
- **Asynchronous message contracts:** Pact.NET consumer and provider tests (`tests/pacts/`) verify Kafka CloudEvents compatibility across producer/consumer pairs.
- **gRPC compatibility:** `buf lint` and `buf breaking --against lock.binpb` protect `book.v1` and `basket.v1` Protobuf schemas.
- **Frontend layer boundaries:** `bun run boundaries` (`turbo boundaries` in `src/Clients`) enforces the 4-tier dependency graph (`config` → `foundation` → `domain` → `app`).

### Three-question pre-change boundary gate

Before implementing any cross-context feature, answer three questions:

1. Which bounded context owns the authoritative state?
2. Which explicit versioned contract carries the interaction?
3. Is every downstream copy classified as derived (rebuildable) rather than authoritative?

## Design trade-offs

| Decision                            | Benefits                                                                    | Costs and limits                                                                                                  |
| :---------------------------------- | :-------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------- |
| **No cross-service database reads** | Services deploy and change schemas independently without breaking neighbors | Cross-context displays require gRPC enrichment (Basket → Catalog) or asynchronous projections (Reviews → Catalog) |
| **Anti-pattern prohibition**        | Prevents hidden coupling, status conflation, and non-rebuildable caches     | Requires upfront contract design and Pact/Buf updates when schemas evolve                                         |

### Anti-patterns prohibited in BookWorm

- Querying another service's PostgreSQL, Cosmos DB, or Redis store directly.
- Treating a rating summary, Qdrant vector, or cache entry as authoritative truth.
- Treating Finance saga timeout or SMTP acceptance as proof of `Order` completion or customer inbox delivery.
- Embedding business-state mutations inside Quartz jobs or Notification workers.

## Alternatives considered

- **Shared relational database with cross-schema joins:** Reduces initial query boilerplate but couples every service release to a single schema and removes independent scaling (Cosmos DB for Reviews, Redis for Basket).

## Related documentation

- [[doc|development/001-developer-onboarding]] — High-level contributor guide.
- [[doc|development/000-local-quickstart]] — End-to-end local quickstart tutorial.
- [[domain|bookworm]] — Context map and service inventory.
- [[doc|arc42/12-glossary]] — Canonical terms and ownership notes.
- ADR-BW-001: Capability-aligned boundaries — Architectural decision record for bounded-context decomposition.
