ADR-0019: Multi-tenancy as an opt-in template toggle
- Status: accepted — tenant resolution and per-aggregate wiring superseded by ADR-0022
- Date: 2026-07-16
Context
Section titled “Context”Some solutions built from this template are multi-tenant, most are not. Baking a TenantId column and a tenant filter into every generated project would tax the majority; omitting the seam entirely would force the minority into an invasive retrofit (schema + every query + every insert).
Decision
Section titled “Decision”Ship multi-tenancy as the --use-multitenancy template toggle (default off), structured so enabling it is a scaffold-time choice rather than a refactor:
ITenantContext(Application port) — the current tenant, or null.TenantContext(Api.Common) — resolves the tenant from the principal'stenantclaim first, falling back to theX-Tenant-Idheader. Claim-first is deliberate: a header alone must never let a caller read another tenant's data. (Superseded by ADR-0022: claim-only, the header is not read, and the claim is minted from the user's persisted membership.)TenantInterceptor(Persistence) — stamps theTenantIdshadow property on newly added rows, keeping the domain model free of infrastructure concerns.- Named
"Tenant"query filter onInvoiceinAppDbContext.OnModelCreating, reusing the same named-filter pattern as"SoftDeleted"so callers can opt out explicitly withIgnoreQueryFilters(["Tenant"]). (Superseded by ADR-0022: the column, index and filter are a convention over every aggregate root, not per-aggregate wiring.)
Wiring uses the proven #if / XML-comment conditional mechanics on UseMultitenancy; the three tenant-only files are additionally excluded via sources.modifiers when the toggle is off.
Consequences
Section titled “Consequences”- Reads are isolated by the query filter and writes are stamped automatically — feature code needs no per-query tenant plumbing.
- Enabling the toggle changes the schema (a
TenantIdcolumn + index). The shippedInitialCreatemigration is generated for the default (off) shape, so a project generated with multi-tenancy on must regenerate its initial migration (./scripts/ef.sh migrations removethenadd InitialCreate) — trivial on a fresh project, and documented indocs/development.md. - The tenant-only types compile in the template repo even with the toggle off (they are self-contained), and the on-branch is compile-verified via
-p:DefineConstants=UseMultitenancy— which is how the missing design-time/fixtureITenantContextarguments were caught. - Tenant isolation is enforced at the data layer only; cross-tenant admin paths must opt out deliberately.
The documentation of Trellis.Templates 0.2.0 · release notes