|
2 | 2 |
|
3 | 3 | namespace DKNet.SlimBus.Extensions.Interceptors; |
4 | 4 |
|
| 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> |
5 | 9 | public static class EfSaveChangesExtension |
6 | 10 | { |
7 | 11 | #region Methods |
8 | 12 |
|
| 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> |
9 | 39 | public static async Task<int> SaveChangesWithConcurrencyHandlingAsync( |
10 | 40 | this DbContext dbContext, |
11 | 41 | IEfCoreExceptionHandler? handler = null, |
|
0 commit comments