Skip to content

Fix inaccurate comment in SaveEntitiesAsync#988

Open
rhodi2436 wants to merge 3 commits intodotnet:mainfrom
rhodi2436:fix/update-SaveEntitiesAsync-comment
Open

Fix inaccurate comment in SaveEntitiesAsync#988
rhodi2436 wants to merge 3 commits intodotnet:mainfrom
rhodi2436:fix/update-SaveEntitiesAsync-comment

Conversation

@rhodi2436
Copy link
Copy Markdown

The original comment claimed that dispatching domain events AFTER SaveChanges (option B) would result in multiple transactions. This was true before TransactionBehavior was introduced, but since TransactionBehavior wraps the entire command handler in an explicit transaction, both orderings now result in a single database transaction.

Update the comment to accurately describe the real trade-off between the two approaches, and note that the explicit transaction uses ReadCommitted isolation (see BeginTransactionAsync), which means handlers in option B can observe already-flushed state via SQL queries within the same transaction.

The original comment claimed that dispatching domain events AFTER SaveChanges
(option B) would result in multiple transactions. This was true before
TransactionBehavior was introduced, but since TransactionBehavior wraps the
entire command handler in an explicit transaction, both orderings now result in
a single database transaction.

Update the comment to accurately describe the real trade-off between the two
approaches, and note that the explicit transaction uses ReadCommitted isolation
(see BeginTransactionAsync), which means handlers in option B can observe
already-flushed state via SQL queries within the same transaction.
Copilot AI review requested due to automatic review settings May 9, 2026 08:58
@rhodi2436
Copy link
Copy Markdown
Author

@dotnet-policy-service agree

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the SaveEntitiesAsync documentation in Ordering’s EF Core DbContext to reflect the current transactional behavior now that MediatR commands are wrapped in an explicit transaction via TransactionBehavior.

Changes:

  • Replaces the outdated “multiple transactions” explanation with an updated trade-off discussion for dispatching domain events before vs. after SaveChanges.
  • Notes the explicit transaction and mentions ReadCommitted isolation and what handlers can observe in option B.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Ordering.Infrastructure/OrderingContext.cs
Comment thread src/Ordering.Infrastructure/OrderingContext.cs Outdated
rhodi2436 and others added 2 commits May 9, 2026 17:15
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/Ordering.Infrastructure/OrderingContext.cs:69

  • The comment says the SaveChangesAsync call "will be committed", but when SaveEntitiesAsync runs under TransactionBehavior the changes are only flushed to the database and are not committed until CommitTransactionAsync commits the explicit transaction. Consider rewording this to avoid implying the data is committed at this point.
        // After executing this line all the changes (from the Command Handler and Domain Event Handlers) 
        // performed through the DbContext will be committed
        _ = await base.SaveChangesAsync(cancellationToken);

// Domain event handlers run while changes are still pending in the ChangeTracker.
// Handlers may call SaveEntitiesAsync themselves, producing multiple intermediate flushes,
// all within the same explicit transaction. The final SaveChanges below flushes any remaining changes.
// Requires client-side ID generation (e.g. HiLo) if handlers depend on DB-generated IDs.
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.

2 participants