Skip to content

✨ feat: an aggregate saves the members it changed, never its whole row - #11

Merged
edgarmesquita merged 2 commits into
mainfrom
feat/save-changed-members
Nov 18, 2026
Merged

edgarmesquita merged 2 commits into
mainfrom
feat/save-changed-members

Conversation

@edgarmesquita

Copy link
Copy Markdown
Contributor

Closes #10. Part of eQuantic/equantic-space#760.

OpenSpec change: core-aggregate-saves-changed-members.

What changes

AggregateRepository.ModifyAsync used to stage the aggregate with the native repository's whole-row ModifyAsync(item). Every column went back as the aggregate mapped it, so a request that loaded a row, changed one thing and saved it undid whatever another request had changed in the same row in between. It also put back the default for a column the aggregate never carries.

Now:

  • The repository remembers what each aggregate it loads (GetAsync) or adds (AddAsync) maps to. ModifyAsync writes only the DataModel members whose mapped value changed since, through IPartialModifyRepository (eQuantic.Core.Data 7.1, on every native provider).
  • A save that changed nothing stages nothing. The aggregate is still tracked, so its events still reach the outbox.
  • Values are compared by content: a JsonNode or a byte array changed in place counts, a date's kind and an offset's offset count. A value that cannot be compared counts as changed, so a doubt costs a write and never loses a change. [Unmapped] members and navigations are never named.
  • Without a baseline (an aggregate the repository did not load in its scope), or over a repository without member-scoped writes (the Entity Framework family), the whole-row write stays. DomainPersistenceOptions.RefuseWholeRowWrites, set through the new AddDomainPersistence(options => …), turns both into an InvalidOperationException.

Requirements implemented

  • A save writes the members the aggregate changed. Two scopes load one row, one changes the customer, the other the status, and the stored row keeps both.
  • A save that changed nothing writes nothing.
  • Whole-row writes are refused on request.

Version

A minor (7.2.0), as the maintainer decided: no signature changes, and the new option is off by default. A consumer that relied on a save writing back the whole row, its updatedAt included when nothing else changed, sees the new behaviour.

Tests

  • AggregateSavesTests (17) and DomainPersistenceRegistrationTests (2): which members each save names, in-place changes, a failed stage keeping the change for the next save, the fallback and the refusal.
  • eQuantic.Core.Persistence.Engine.Tests (new, net10.0 since eQuantic.Core.Data.PostgreSql 7.1 is net10.0 only): the race, a column the aggregate does not carry, an unchanged save after another request wrote the row, a JSON member changed in place, and the refusal. They run on PostgreSQL 17 in a container. CI requires them on Linux (CORE_DDD_ENGINE_TESTS=required), and Windows runners ignore them because they cannot run the image.
  • Mutation checks: writing the whole row again fails 12 of the unit tests and 3 of the engine tests (race, not-carried column, unchanged save). Keeping the JSON node and the byte array by reference fails the in-place tests.

AggregateRepository remembers what each aggregate it loads or adds maps to,
and ModifyAsync writes only the DataModel members whose mapped value changed
since, through IPartialModifyRepository. A save that changed nothing writes
nothing. An aggregate it did not load, or a native repository without
member-scoped writes, keeps the whole-row write unless
DomainPersistenceOptions.RefuseWholeRowWrites is on.

Proven on PostgreSQL in a container: two saves of one row keep both
changes. CI requires those tests on Linux (CORE_DDD_ENGINE_TESTS).

Closes #10

@edgarmesquita edgarmesquita left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secondary review, round 1 (Antigravity, Gemini 4 Argon Medium), because Copilot cannot be requested on this repository.

Two findings, both fixed in ff79d72. Nothing dismissed.

public Task ModifyAsync(TAggregate aggregate, CancellationToken cancellationToken = default)
public async Task ModifyAsync(TAggregate aggregate, CancellationToken cancellationToken = default)
{
_tracker.Track(aggregate);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[medium] ModifyAsync tracked the aggregate before deciding whether to write it, so a save refused by RefuseWholeRowWrites (or a stage that threw) left the aggregate tracked, and a later commit would publish its events for a change never written.

Fixed in ff79d72: Add, Modify and Remove track the aggregate once its write is staged, and a save with nothing to write still tracks it. Two tests hold it, and both fail with tracking first.

case JsonNode node:
return node.DeepClone();
case JsonElement element:
return new Serialized(element.GetRawText());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[medium] A default JsonElement has no raw text, and its GetRawText() ran outside the try, so every save of a DataModel with an unset JsonElement member threw. JsonElement and JsonDocument also compared by raw text, unlike JsonNode.

Fixed in ff79d72: a default JsonElement is no value, and both are kept as a JsonNode, compared by content. Three tests hold it.

@edgarmesquita edgarmesquita left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Secondary review, round 2 (Antigravity, Gemini 4 Argon Medium), because Copilot cannot be requested on this repository.

No findings. The review closes here.

@edgarmesquita
edgarmesquita merged commit 0cd1570 into main Nov 18, 2026
6 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 7.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An aggregate saves the members it changed, never its whole row

2 participants