# Enforcing Module and SQL Schema Boundaries in CI

> Catch illegal cross-module references, public schema leaks, and cross-schema Dapper SQL queries in 5 seconds with 49 .NET architecture tests.

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

A modular monolith stays modular only if broken boundaries fail the build automatically. `Orbit.ArchitectureTests` runs 49 deterministic checks in under two seconds—verifying Clean Architecture layer dependencies, `.csproj` references, EF Core `snake_case` schema metadata, and raw Dapper SQL queries across all five modules.

## Run the 49 boundary and schema guardrails

Execute the fast inner-loop verification script (or target `Orbit.ArchitectureTests` directly):

```sh
./scripts/verify.sh --fast --skip-web
# or on Windows PowerShell:
./scripts/verify.ps1 -Fast -SkipWeb
```

## Four layers of automated protection

1. **Cross-Module Project & Namespace Isolation (`ModuleIsolationTests`)**: No module may reference another module's `Domain`, `Application`, `Presentation`, or `Infrastructure` assemblies—only `*.IntegrationEvents` and `Catalog.PublicApi` are permitted seams. Even unused illegal `<ProjectReference>` entries in `.csproj` files fail immediately.
2. **Clean Architecture Inward Dependency Rules**: `Domain` has zero infrastructure dependencies (`EntityFrameworkCore`, `Dapper`, `AspNetCore`); `Application` depends only on `Domain` and contracts; `Endpoints` implement `IEndpoint` inside `Presentation`.
3. **EF Core Metadata & Outbox/Inbox Ownership (`SchemaPerModuleTests`)**: Every module's `DbContext` must map 100% of its entities—including `outbox_messages`, `outbox_message_consumers`, `inbox_messages`, and `inbox_message_consumers`—to its exclusive lowercase `snake_case` schema (`users`, `catalog`, `inventory`, `orders`, `shipments`).
4. **Dapper Raw SQL Schema Scanner**: Scans every C# source file under `src/Modules/<Module>/` for SQL clauses (`FROM`, `JOIN`, `INTO`, `UPDATE`) and fails if any query touches `public` or a foreign module's schema.

## Summary

- Check both `.csproj` project graphs and compiled types so unused illegal references never slip into `main`.
- Inspect EF Core `IModel` metadata in unit tests to guarantee `snake_case` naming and Outbox/Inbox tables in every module schema.
- Scan raw Dapper SQL statements per module so shared PostgreSQL connection strings cannot bypass module boundaries.

## Frequently asked questions

### Why aren't C# assembly reference checks enough to protect a modular monolith?

A module with zero project references to Orders can still execute raw Dapper SQL like SELECT * FROM orders.orders over a shared connection string. Orbit scans both compiled assemblies and SQL strings across every module.

### What does Orbit verify on every EF Core DbContext?

SchemaPerModuleTests instantiates all five DbContexts and asserts a unique non-public default schema, snake_case table and column names, and schema-owned Outbox and Inbox tables.

