Skip to content

Fix FetchForWriting with UseIdentityMapForAggregates and strongly typed IDs#4216

Merged
jeremydmiller merged 1 commit intomasterfrom
fix/4214-identity-map-strong-typed-ids
Mar 28, 2026
Merged

Fix FetchForWriting with UseIdentityMapForAggregates and strongly typed IDs#4216
jeremydmiller merged 1 commit intomasterfrom
fix/4214-identity-map-strong-typed-ids

Conversation

@jeremydmiller
Copy link
Copy Markdown
Member

Summary

  • Fix InvalidCastException: Unable to cast object of type 'Dictionary<PaymentId,Payment>' to type 'Dictionary<Guid,Payment>' when using FetchForWriting() with UseIdentityMapForAggregates = true and aggregates that have strongly typed IDs (via StronglyTypedId, Vogen, etc.)
  • The inline projection stores the aggregate in the identity map using the strong-typed key (PaymentId), but FetchForWriting tries to access it using the raw stream identity type (Guid). The fix makes StoreDocumentInItemMap gracefully skip when the existing dictionary has an incompatible key type, since the document is already present from the projection's storage path.

Closes #4214

Test plan

  • 4 new regression tests (Inline + Live × single fetch + double fetch) — all passing
  • Full ValueTypeTests suite passes (0 failures)
  • Solution compiles via nuke compile

🤖 Generated with Claude Code

…ates and strongly typed IDs

When UseIdentityMapForAggregates is true and the aggregate uses a
strongly typed ID (e.g., PaymentId wrapping Guid), the identity map
would be populated by the inline projection using the strong-typed key
(Dictionary<PaymentId, Payment>), but FetchForWriting would try to
cast it to Dictionary<Guid, Payment>, causing an InvalidCastException.

The fix makes StoreDocumentInItemMap gracefully skip storing when the
existing dictionary has an incompatible key type — the document is
already present from the inline projection's storage path.

Closes #4214

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jeremydmiller jeremydmiller merged commit 1bdafbf into master Mar 28, 2026
1 of 6 checks passed
@jeremydmiller jeremydmiller deleted the fix/4214-identity-map-strong-typed-ids branch March 28, 2026 12:22
jeremydmiller added a commit to JasperFx/polecat that referenced this pull request Mar 28, 2026
…ates and strongly typed IDs

Port of JasperFx/marten#4216. When UseIdentityMapForAggregates is true
and the aggregate uses a strongly typed ID (e.g., PaymentId wrapping
Guid), the identity map dictionary key type mismatch caused an
InvalidCastException. The fix makes StoreAggregateInIdentityMap
gracefully skip when the existing dictionary has an incompatible key
type.

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.

FetchForWriting throws InvalidCastException when using UseIdentityMapForAggregates with strongly typed IDs

1 participant