Skip to content

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)

The persistence layer must map rich-DDD building blocks — value objects, domain events, auditing, soft-delete — without leaking EF into the domain.

  • Value objects as EF complex types (ComplexProperty), the EF 10-recommended mapping (value semantics; not owned-entity identity). Money/EmailAddress map this way in Configurations/. (Amended 2026-10-11: still the default; a single-value value object that needs an index, a key or Contains becomes a converted column instead.)
  • Aggregates load whole: child navigations use AutoInclude() + field access; there are no partial-aggregate loads.
  • Three ISaveChangesInterceptors, order fixed in Persistence/DependencyInjection.cs (amended 2026-10-11: a fourth, AggregateConcurrencyInterceptor, is attached by AppDbContext.OnConfiguring after these):
    1. A domain-event dispatch interceptor — dispatched aggregate events inside SaveChanges (draining to quiescence). (Superseded by ADR-0018: OutboxWriteInterceptor now takes this slot and writes each event to OutboxMessages in the same transaction; handlers run after commit, never during SaveChanges.)
    2. SoftDeleteInterceptor — turns Remove of ISoftDeletable into a DeletedAtUtc update, rescues cascade-deleted children; paired with the EF 10 named "SoftDeleted" query filter.
    3. AuditingInterceptor — stamps IAuditable from TimeProvider + ICurrentUser, aggregate-root-aware (a child change stamps the root), via the entry API (post-DetectChanges).
  • 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.

  • Every aggregate root has a concurrency token. AggregateConcurrencyTests walks the model and fails on an IHasDomainEvents entity type without one, or without a plain column the promotion below can write back.
  • A child-only change promotes the root. AggregateConcurrencyInterceptor marks one real, non-key, non-generated column of every Unchanged root 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 outside DependencyInjection.cs: stateless, it is attached by AppDbContext.OnConfiguring after the host's interceptors, so every context instance runs it.
  • A value object holding another value object types it as one (Money.Currency and Invoice.Currency are Currency, 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 Contains is a converted column — a rule, not a new default. EF Core 10 cannot index a complex-type member (neither HasIndex(u => u.Email.Value) nor the column name builds the model), so such a value object implements IScalarValueObject<TSelf, TValue> (Value, and an explicitly implemented Restore that skips Create's checks, as the complex-type materializer does) and is mapped through the one generic ScalarValueObjectConverter<TValueObject, TValue>, modelled on StronglyTypedIdValueConverter: one line per type in AppDbContext.ConfigureConventions. The line makes every member of that type a converted column, so a type is complex or converted throughout the model, never both. EmailAddress and Currency implement the interface but stay complex types until an application needs the index; ScalarValueObjectConverterTests maps them the converted way in a probe model.
  • A stale write to any aggregate is Concurrency.Conflict (409) from CommitAsync, 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.

  • Value objects map as complex types by default; AppDbContext.ConfigureConventions is the list of converted ones, one HaveConversion<ScalarValueObjectConverter<…>> line per type. Where this record names a type (Money and EmailAddress in the Decision, EmailAddress and Currency in 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).
  • A feature that converts a value object leaves this record true, and the reviewer reads the decision in ConfigureConventions and the feature doc rather than in a list here.

The documentation of Trellis.Templates 0.2.0 · release notes