Fix inaccurate comment in SaveEntitiesAsync#988
Open
rhodi2436 wants to merge 3 commits intodotnet:mainfrom
Open
Fix inaccurate comment in SaveEntitiesAsync#988rhodi2436 wants to merge 3 commits intodotnet:mainfrom
rhodi2436 wants to merge 3 commits intodotnet:mainfrom
Conversation
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.
Author
|
@dotnet-policy-service agree |
There was a problem hiding this comment.
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
ReadCommittedisolation and what handlers can observe in option B.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
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
SaveChangesAsynccall "will be committed", but whenSaveEntitiesAsyncruns underTransactionBehaviorthe changes are only flushed to the database and are not committed untilCommitTransactionAsynccommits 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. |
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.
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.