ADR-0006: EF Core 10 value-object mapping + SaveChanges interceptors
- Status: accepted — the in-transaction domain-event dispatch (interceptor 1 below) is superseded by ADR-0018 (transactional outbox); the value-object mapping, soft-delete and auditing stand
- Date: 2026-07-14
- Amended: 2026-10-11 — concurrency at the aggregate boundary by convention, value objects nesting value objects, and converted columns for a single-value value object that needs an index (see Amendment (2026-10-11) below)
- Amended: 2026-11-09 — the code, not this record, lists which value objects are converted (see Amendment (2026-11-09) below)
Context
Section titled “Context”The persistence layer must map rich-DDD building blocks — value objects, domain events, auditing, soft-delete — without leaking EF into the domain.
Decision
Section titled “Decision”- Value objects as EF complex types (
ComplexProperty), the EF 10-recommended mapping (value semantics; not owned-entity identity).Money/EmailAddressmap this way inConfigurations/. (Amended 2026-10-11: still the default; a single-value value object that needs an index, a key orContainsbecomes a converted column instead.) - Aggregates load whole: child navigations use
AutoInclude()+ field access; there are no partial-aggregate loads. - Three
ISaveChangesInterceptors, order fixed inPersistence/DependencyInjection.cs(amended 2026-10-11: a fourth,AggregateConcurrencyInterceptor, is attached byAppDbContext.OnConfiguringafter these):- A domain-event dispatch interceptor — dispatched aggregate events inside
SaveChanges(draining to quiescence). (Superseded by ADR-0018:OutboxWriteInterceptornow takes this slot and writes each event toOutboxMessagesin the same transaction; handlers run after commit, never duringSaveChanges.) SoftDeleteInterceptor— turnsRemoveofISoftDeletableinto aDeletedAtUtcupdate, rescues cascade-deleted children; paired with the EF 10 named"SoftDeleted"query filter.AuditingInterceptor— stampsIAuditablefromTimeProvider+ICurrentUser, aggregate-root-aware (a child change stamps the root), via the entry API (post-DetectChanges).
- A domain-event dispatch interceptor — dispatched aggregate events inside
Consequences
Section titled “Consequences”- The domain stays EF-free; mapping lives entirely in Persistence configurations + interceptors.
- Soft-delete + auditing are automatic and centralized.
- Domain-event dispatch was in-transaction pre-commit; a transactional outbox was named as the production evolution for side-effecting handlers. ADR-0018 delivered it and replaced the dispatch interceptor; ADR-0023 refines the processor.
Amendment (2026-10-11): the aggregate boundary, enforced
Section titled “Amendment (2026-10-11): the aggregate boundary, enforced”Each aggregate maps Postgres xmin as its concurrency token, but the token sits on the root's row, and
EF writes that row only when a root column changes. A change to the children alone (AddLine on a
loaded invoice) reached the root's UPDATE only because AuditingInterceptor stamps IAuditable roots;
the first aggregate without IAuditable — or without a token at all — would have let two concurrent
edits of one aggregate both commit, silently.
Decision
Section titled “Decision”- Every aggregate root has a concurrency token.
AggregateConcurrencyTestswalks the model and fails on anIHasDomainEventsentity type without one, or without a plain column the promotion below can write back. - A child-only change promotes the root.
AggregateConcurrencyInterceptormarks one real, non-key, non-generated column of everyUnchangedroot with an added, modified or deleted child as modified (writing its current value back), so the root's UPDATE checks and bumps the token. A child is an entity the root reaches through a navigation, followed up through grandchildren; a foreign key to another aggregate without such a navigation promotes nothing. It is the fourth interceptor and the only one outsideDependencyInjection.cs: stateless, it is attached byAppDbContext.OnConfiguringafter the host's interceptors, so every context instance runs it. - A value object holding another value object types it as one (
Money.CurrencyandInvoice.CurrencyareCurrency, not strings) and maps as a complex type nested in a complex type; such a value object keeps a private parameterless constructor, because EF cannot bind a nested complex property through a constructor. Columns do not change: the nested property keeps its column name. - A single-value value object that needs an index, a key or
Containsis a converted column — a rule, not a new default. EF Core 10 cannot index a complex-type member (neitherHasIndex(u => u.Email.Value)nor the column name builds the model), so such a value object implementsIScalarValueObject<TSelf, TValue>(Value, and an explicitly implementedRestorethat skipsCreate's checks, as the complex-type materializer does) and is mapped through the one genericScalarValueObjectConverter<TValueObject, TValue>, modelled onStronglyTypedIdValueConverter: one line per type inAppDbContext.ConfigureConventions. The line makes every member of that type a converted column, so a type is complex or converted throughout the model, never both.EmailAddressandCurrencyimplement the interface but stay complex types until an application needs the index;ScalarValueObjectConverterTestsmaps them the converted way in a probe model.
Consequences
Section titled “Consequences”- A stale write to any aggregate is
Concurrency.Conflict(409) fromCommitAsync, whatever the root implements; the auditing stamp is no longer load-bearing for concurrency. - One extra UPDATE of the root per save that changes only children of a non-auditable aggregate — the write that carries the token.
- A converted value object is compared whole in a query (
==,Contains, ordering) but no longer reached into:s.Email.Value.StartsWith(…)stops translating to SQL. Converting a type that is already a complex type renames nothing (the column keeps its name) but is a model change, so it comes with a migration.
Amendment (2026-11-09): the code lists the converted value objects
Section titled “Amendment (2026-11-09): the code lists the converted value objects”The decision named the shipped slice's instances as if they were the rule's: Money/EmailAddress "map
this way", and the 2026-10-11 amendment says EmailAddress and Currency "stay complex types until an
application needs the index". The first feature that needs the index makes both sentences false: a
Customer with a unique e-mail converts EmailAddress, as the amendment prescribes, and the record then
contradicts the model, because an accepted record only takes amendments and nothing asked for one.
Decision
Section titled “Decision”- Value objects map as complex types by default;
AppDbContext.ConfigureConventionsis the list of converted ones, oneHaveConversion<ScalarValueObjectConverter<…>>line per type. Where this record names a type (MoneyandEmailAddressin the Decision,EmailAddressandCurrencyin the 2026-10-11 amendment), it describes the reference slice as shipped, not any later model. - Converting a type needs no edit here. It is a model change with a migration and a line in the doc of
the feature that needs the index (
.claude/rules/persistence.md).
Consequences
Section titled “Consequences”- A feature that converts a value object leaves this record true, and the reviewer reads the decision in
ConfigureConventionsand the feature doc rather than in a list here.
The documentation of Trellis.Templates 0.2.0 · release notes