Skip to content

Discover [NaturalKeySource] on static methods of self-aggregating aggregates#187

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

Discover [NaturalKeySource] on static methods of self-aggregating aggregates#187
jeremydmiller merged 1 commit intomainfrom
fix-4277-naturalkey-self-aggregate

Conversation

@jeremydmiller
Copy link
Copy Markdown
Member

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

public sealed record MyAggregate(Guid Id, [property: NaturalKey] MyKey Key)
{
    [NaturalKeySource]
    public static MyAggregate Create(MyCreated e) => new(e.Id, new MyKey(e.Key));
}

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:

  1. `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. Cleanly covers @mfmadsen's suggestion in the issue.
  2. `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.

Test coverage

New file `src/EventTests/Projections/NaturalKeySourceDiscoveryTests.cs` with three tests exercising the three discovery pathways directly against `NaturalKeyDefinition.EventMappings`:

  • instance method on aggregate (pre-existing shape)
  • static factory on self-aggregating aggregate (#4277)
  • static method on a separate projection class (property-matching fallback)

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

…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 jeremydmiller merged commit 3a4e1b9 into main Apr 22, 2026
1 check passed
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>
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.

1 participant