Skip to content

Fix O(n) ChangeTracker full scan in DomainEventScraper (#2476)#2484

Merged
jeremydmiller merged 2 commits intomainfrom
fix/2476-efcore-domain-event-scraper-state-filter
Apr 9, 2026
Merged

Fix O(n) ChangeTracker full scan in DomainEventScraper (#2476)#2484
jeremydmiller merged 2 commits intomainfrom
fix/2476-efcore-domain-event-scraper-state-filter

Conversation

@jeremydmiller
Copy link
Copy Markdown
Member

Summary

Fixes #2476.

DomainEventScraper<T, TEvent>.ScrapeEvents() previously called ChangeTracker.Entries() and iterated every tracked entity before filtering down to the target type. In DbContexts that track many entities this is an unnecessary O(n) scan.

  • Filter EntityState before OfType<T>() – only Added and Modified entries can have domain events, so Unchanged and Deleted entries are now skipped up-front.
  • Added Microsoft.EntityFrameworkCore.InMemory to EfCoreTests to support infrastructure-free unit tests.
  • Added DomainEventScraperStateFilterTests with two tests that verify only Added/Modified entities are considered by the scraper — including a case where an Unchanged entity has domain events artificially injected, confirming they are intentionally skipped.

Test plan

  • New unit tests in EfCoreTests/DomainEvents/DomainEventScraperStateFilterTests.cs pass without any infrastructure (InMemory provider)
  • Existing SQL Server integration tests in configuration_of_domain_events_scrapers continue to pass

🤖 Generated with Claude Code

jeremydmiller and others added 2 commits April 9, 2026 16:16
…tch inserts

Replace individual DbContext.Add calls in the Wolverine-enabled path of
PersistOutgoingAsync(Envelope[]) with a single DbContext.AddRange call,
allowing providers like Npgsql to batch INSERT statements into a single
round-trip. Adds an integration test verifying all envelopes in the batch
are correctly persisted. Fixes #2475.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ed states only (#2476)

Before this change, DomainEventScraper<T, TEvent>.ScrapeEvents() iterated
every tracked entity in the ChangeTracker regardless of state.  Domain events
can only originate from entities whose state is Added or Modified, so scanning
Unchanged and Deleted entries is wasted work.

Added Microsoft.EntityFrameworkCore.InMemory to EfCoreTests so the new unit
tests can run without SQL Server infrastructure.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@jeremydmiller jeremydmiller merged commit e4f9a0e into main Apr 9, 2026
15 of 19 checks passed
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.

ChangeTracker full scan in domain event scraping is O(n) across all tracked entities

1 participant