# Designing Bounded Contexts and Schema-per-Module Ownership

> Split a .NET 11 application into five isolated bounded contexts, each owning an exclusive PostgreSQL 18 schema with zero shared tables.

Kind: Walkthrough · Reviewed: 2026-12-02 · Author: Milan Jovanović
Canonical: https://modularmonolith.com/guides/module-boundaries/
Reference Repository: https://github.com/m-jovanovic/modular-monolith/tree/3c8d0cf5601d287b645c1ca59b7b3e811659ec74

A modular monolith enforces bounded contexts by pairing Clean Architecture project isolation with exclusive PostgreSQL schemas (`users`, `catalog`, `inventory`, `orders`, `shipments`) inside one deployable `.NET 11` host. Each module owns its state transitions, EF Core migrations, Outbox/Inbox tables, and Dapper projections without touching `public` or peer schemas.

## Separate descriptive catalog metadata from authoritative stock

Beginners often model a single `Product` entity with `Name`, `Price`, `StockQuantity`, and `OrderLines` inside one shared `AppDbContext`. That single class forces merchandising edits, checkout spikes, and warehouse pick-lists to contend on the same rows and deploy together forever.

Orbit splits those responsibilities by asking **which capability owns the invariant at write time**:

| Bounded Context | PostgreSQL Schema | Authoritative Invariant | Public Seam |
| --- | --- | --- | --- |
| **Users** | `users` | Account identity, OIDC/JWT mapping, RBAC permissions | `Users.IntegrationEvents` |
| **Catalog** | `catalog` | SKU lifecycle (`Draft` -> `Active` -> `Discontinued`) & pricing | `ICatalogPublicApi`, `Catalog.IntegrationEvents` |
| **Inventory** | `inventory` | On-hand stock, `FOR UPDATE` reservations, low-stock thresholds | `Inventory.IntegrationEvents` |
| **Orders** | `orders` | Purchase order state machine & `StockRejected` compensation | `Orders.IntegrationEvents` |
| **Shipments** | `shipments` | Warehouse fulfillment (`Pending` -> `Picking` -> `Packed` -> `Dispatched` -> `Delivered`) | `Shipments.IntegrationEvents` |

Catalog owns product descriptions and list prices, but **Catalog never knows how many units sit on a warehouse shelf**. When an administrator publishes a SKU, `Catalog` emits `ProductPublishedIntegrationEvent`, and `Inventory` provisions its own `StockItem` aggregate inside `inventory.stock_items`.

## Keep write transactions strictly inside one schema

When a buyer submits `POST /api/orders`, `Orders` calls `ICatalogPublicApi` in-process to verify that the requested SKUs are active and to snapshot their current unit prices. That read completes *before* `OrdersDbContext` commits to `orders.orders`, `orders.order_items`, and `orders.outbox_messages`.

No distributed transaction spans `orders` and `inventory`. Instead, `Orders` records the order in `PendingStock` status and publishes `OrderPlacedIntegrationEvent`. `Inventory` locks its own `inventory.stock_items` row and answers with either `OrderStockReservedIntegrationEvent` or `OrderStockRejectedIntegrationEvent`.

## Verify schema isolation against PostgreSQL 18

Run the integration suite from the public repository to verify that all five schemas own their own `__EFMigrationsHistory`, `outbox_messages`, and `inbox_messages` tables while `public` contains zero base tables:

```sh
dotnet test tests/Orbit.IntegrationTests/Orbit.IntegrationTests.csproj \
  --filter FullyQualifiedName~Database_EnforcesStrictSchemaPerModule_ZeroPublicTables
```

## Summary

- Assign every table to one PostgreSQL schema (`users`, `catalog`, `inventory`, `orders`, `shipments`) and keep `public` empty.
- Snapshot read-only descriptive fields at command time so historical orders stay immutable when catalog prices change.
- Coordinate cross-module workflows through versioned integration events rather than cross-schema transactions or SQL joins.

## Frequently asked questions

### Why should a modular monolith use separate PostgreSQL schemas instead of separate databases?

Separate schemas inside one PostgreSQL 18 instance enforce explicit data ownership and forbid cross-module SQL joins while keeping operational backups, connection pooling, and local Docker setup simple.

### Why does Orbit forbid placing any tables in PostgreSQL's public schema?

Forbidding tables in public prevents accidental shared-table coupling. Every EF Core DbContext, migration history table, outbox table, and Dapper SQL query must explicitly name its owning module schema.

### Can Orders join catalog.products or inventory.stock_items in a read query?

No. Orders snapshots SKU names and unit prices through ICatalogPublicApi when an order is placed and stores them in orders.order_items, keeping every read query local to the orders schema.

