Skip to content

NaturalKey on self-aggregating aggregates (closes #4277)#4279

Merged
jeremydmiller merged 1 commit intomasterfrom
fix-4277-naturalkey-self-aggregate
Apr 22, 2026
Merged

NaturalKey on self-aggregating aggregates (closes #4277)#4279
jeremydmiller merged 1 commit intomasterfrom
fix-4277-naturalkey-self-aggregate

Conversation

@jeremydmiller
Copy link
Copy Markdown
Member

Closes #4277.

Summary

Adds an end-to-end regression test to `Bug_4197_fetch_for_writing_natural_key.cs` matching the exact self-aggregating shape from the issue:

```csharp
public sealed record Bug4277SelfAggregate(Guid Id, [property: NaturalKey] Bug4197AggregateKey Key)
{
[NaturalKeySource]
public static Bug4277SelfAggregate Create(Bug4197AggregateCreatedEvent e)
=> new(e.Id, new Bug4197AggregateKey(e.Key));
}
```

Before the fix, calling `FetchForWriting<Bug4277SelfAggregate, Bug4197AggregateKey>(key)` returned a null aggregate because the natural-key lookup row was never written — the discovery scan in JasperFx.Events silently skipped the static `Create` method on the aggregate type.

Upstream fix

Companion PR: JasperFx/jasperfx#187, shipping as JasperFx.Events 1.29.1. That PR:

  • Widens `discoverNaturalKey` to scan static methods on the aggregate type, not just instance methods.
  • Teaches `buildExtractor` to call a static factory on `docType` that returns `docType` and read the natural-key property off the returned aggregate.
  • Adds three JasperFx-side unit tests that exercise the three `[NaturalKeySource]` discovery pathways directly.

Dependency

This PR bumps `Directory.Packages.props` `JasperFx.Events` from `1.29.0` to `1.29.1`. CI on this branch will fail with `NU1102` until `JasperFx.Events 1.29.1` is published to NuGet.org via the upstream PR. Local verification performed against a packed dev build of `1.29.1`:

  • `Bug_4197_fetch_for_writing_natural_key` — 3/3 pass (including the new test)
  • Broader natural-key + aggregation sweep — 283/283 pass on net9.0, no regressions

🤖 Generated with Claude Code

Adds an end-to-end test to Bug_4197_fetch_for_writing_natural_key.cs matching
the exact shape from #4277: a self-aggregating record with a static
[NaturalKeySource] factory method. Before the fix in JasperFx.Events 1.29.1
this scenario silently failed — the natural-key lookup row was never written
and FetchForWriting<TDoc, TKey>(key) returned a null aggregate.

Bumps JasperFx.Events to 1.29.1 to pick up the fix.

Companion: JasperFx/jasperfx#187

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NaturalKeySource does not work for "self-aggregating" type

1 participant