Discover [NaturalKeySource] on static methods of self-aggregating aggregates#187
Merged
jeremydmiller merged 1 commit intomainfrom Apr 22, 2026
Merged
Conversation
…regates (#4277)
The NaturalKey discovery scan in JasperFxAggregationProjectionBase only picked
up instance methods on the aggregate type, and separately picked up static
methods on a distinct projection class. For self-aggregating aggregates
(records / classes where the projection and the aggregate are the same type)
with a canonical static factory such as
public static TDoc Create(TEvent e) => new TDoc(...);
the [NaturalKeySource] attribute was silently ignored and the natural-key
lookup row was never written. See JasperFx/marten#4277.
Two small changes in JasperFxAggregationProjectionBase:
* discoverNaturalKey: include BindingFlags.Static alongside
BindingFlags.Instance when scanning docType. Static methods on the
aggregate are now candidates regardless of whether the projection is
a separate class.
* buildExtractor: add a branch for static methods on docType that
return docType. Invoke the factory with the raw event payload and
read the natural-key property off the returned aggregate, matching
the semantics of the existing instance-method path.
Unit coverage in src/EventTests/Projections/NaturalKeySourceDiscoveryTests.cs
exercises all three pathways:
- instance method on aggregate (pre-existing shape)
- static factory on self-aggregating aggregate (#4277)
- static method on a separate projection class (property-matching fallback)
Bumps JasperFx.Events to 1.29.1.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
jeremydmiller
added a commit
to JasperFx/marten
that referenced
this pull request
Apr 22, 2026
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses JasperFx/marten#4277.
Problem
For self-aggregating aggregates — records or classes where the projection and the aggregate are the same type — the `[NaturalKeySource]` attribute was silently ignored when placed on a static factory such as
The discovery scan in `JasperFxAggregationProjectionBase.discoverNaturalKey` only picked up instance methods on the aggregate type, and a second scan only ran for static methods on a *different* projection class. When the projection and the aggregate were the same type the second scan was skipped, so the natural-key lookup row was never written and `FetchForWriting<TDoc, TKey>(key)` returned a null aggregate.
Fix
Two small changes:
Test coverage
New file `src/EventTests/Projections/NaturalKeySourceDiscoveryTests.cs` with three tests exercising the three discovery pathways directly against `NaturalKeyDefinition.EventMappings`:
All three pass on net8.0 / net9.0 / net10.0. Full `EventTests` suite (222 tests) stays green.
Version
`JasperFx.Events` bumped to 1.29.1 (patch bump — bug fix, no API changes).
Companion PR
A Marten-side companion PR adds an end-to-end regression test using the exact shape from the issue, matching `Bug_4197_fetch_for_writing_natural_key.cs`.
🤖 Generated with Claude Code