Skip to content

Commit 9b945ca

Browse files
authored
Update EfSaveChangesExtension.cs
1 parent 775b06d commit 9b945ca

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/SlimBus/DKNet.SlimBus.Extensions/Interceptors/EfSaveChangesExtension.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,40 @@
22

33
namespace DKNet.SlimBus.Extensions.Interceptors;
44

5+
/// <summary>
6+
/// Provides extension methods for <see cref="DbContext"/> to handle EF Core concurrency exceptions
7+
/// during save operations with configurable retry and resolution behavior.
8+
/// </summary>
59
public static class EfSaveChangesExtension
610
{
711
#region Methods
812

13+
/// <summary>
14+
/// Attempts to save changes on the provided <see cref="DbContext"/>, handling <see cref="DbUpdateConcurrencyException"/>
15+
/// according to the supplied <see cref="IEfCoreExceptionHandler"/>. The method will retry the save operation
16+
/// if the handler resolves the concurrency conflict with <see cref="EfConcurrencyResolution.RetrySaveChanges"/>.
17+
/// </summary>
18+
/// <param name="dbContext">The <see cref="DbContext"/> to save.</param>
19+
/// <param name="handler">
20+
/// An optional concurrency exception handler that decides how to resolve <see cref="DbUpdateConcurrencyException"/>.
21+
/// If <c>null</c>, a default <see cref="EfCoreExceptionHandler"/> instance will be used.
22+
/// </param>
23+
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
24+
/// <returns>
25+
/// A <see cref="Task{Int32}"/> representing the asynchronous operation. The task result contains the number
26+
/// of state entries written to the underlying database. If the handler resolves the conflict with
27+
/// <see cref="EfConcurrencyResolution.IgnoreChanges"/>, <c>0</c> is returned.
28+
/// </returns>
29+
/// <remarks>
30+
/// The method will loop and retry calls to <see cref="DbContext.SaveChangesAsync(CancellationToken)"/>
31+
/// when the handler returns <see cref="EfConcurrencyResolution.RetrySaveChanges"/>. The number of retries
32+
/// is limited by <see cref="IEfCoreExceptionHandler.MaxRetryCount"/> on the provided handler. If the retry
33+
/// count exceeds <see cref="IEfCoreExceptionHandler.MaxRetryCount"/>, the method returns <c>0</c>.
34+
/// </remarks>
35+
/// <exception cref="DbUpdateConcurrencyException">
36+
/// Thrown when a concurrency exception occurs and the handler returns
37+
/// <see cref="EfConcurrencyResolution.RethrowException"/>.
38+
/// </exception>
939
public static async Task<int> SaveChangesWithConcurrencyHandlingAsync(
1040
this DbContext dbContext,
1141
IEfCoreExceptionHandler? handler = null,

0 commit comments

Comments
 (0)