| | | 1 | | using Microsoft.EntityFrameworkCore.Diagnostics; |
| | | 2 | | |
| | | 3 | | namespace ProjectTemplate.Infrastructure.Data; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Invokes the application save pipeline from the EF Core SaveChanges interception lifecycle. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class ApplicationSaveChangesInterceptor : SaveChangesInterceptor |
| | | 9 | | { |
| | | 10 | | private readonly IApplicationSaveChangesPipeline _saveChangesPipeline; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Initializes a new instance of the <see cref="ApplicationSaveChangesInterceptor" /> class. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="saveChangesPipeline">The application-owned save pipeline.</param> |
| | 180 | 16 | | public ApplicationSaveChangesInterceptor(IApplicationSaveChangesPipeline saveChangesPipeline) |
| | | 17 | | { |
| | 180 | 18 | | ArgumentNullException.ThrowIfNull(saveChangesPipeline); |
| | | 19 | | |
| | 178 | 20 | | _saveChangesPipeline = saveChangesPipeline; |
| | 178 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public override InterceptionResult<int> SavingChanges( |
| | | 25 | | DbContextEventData eventData, |
| | | 26 | | InterceptionResult<int> result) |
| | | 27 | | { |
| | 22 | 28 | | if (eventData.Context is ApplicationDbContext dbContext) |
| | | 29 | | { |
| | 20 | 30 | | _ = _saveChangesPipeline.ApplyBeforeSaveChanges(dbContext); |
| | | 31 | | } |
| | | 32 | | |
| | 22 | 33 | | return result; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public override async ValueTask<InterceptionResult<int>> SavingChangesAsync( |
| | | 38 | | DbContextEventData eventData, |
| | | 39 | | InterceptionResult<int> result, |
| | | 40 | | CancellationToken cancellationToken = default) |
| | | 41 | | { |
| | 142 | 42 | | if (eventData.Context is ApplicationDbContext dbContext) |
| | | 43 | | { |
| | 140 | 44 | | _ = await _saveChangesPipeline |
| | 140 | 45 | | .ApplyBeforeSaveChangesAsync(dbContext, cancellationToken) |
| | 140 | 46 | | .ConfigureAwait(false); |
| | | 47 | | } |
| | | 48 | | |
| | 142 | 49 | | return result; |
| | 142 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc /> |
| | | 53 | | public override int SavedChanges( |
| | | 54 | | SaveChangesCompletedEventData eventData, |
| | | 55 | | int result) |
| | | 56 | | { |
| | 20 | 57 | | if (eventData.Context is ApplicationDbContext dbContext && |
| | 20 | 58 | | _saveChangesPipeline.ApplyAfterSaveChanges(dbContext)) |
| | | 59 | | { |
| | 2 | 60 | | _ = dbContext.SaveChanges(); |
| | | 61 | | } |
| | | 62 | | |
| | 20 | 63 | | return result; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | | 67 | | public override async ValueTask<int> SavedChangesAsync( |
| | | 68 | | SaveChangesCompletedEventData eventData, |
| | | 69 | | int result, |
| | | 70 | | CancellationToken cancellationToken = default) |
| | | 71 | | { |
| | 138 | 72 | | if (eventData.Context is ApplicationDbContext dbContext && |
| | 138 | 73 | | await _saveChangesPipeline |
| | 138 | 74 | | .ApplyAfterSaveChangesAsync(dbContext, cancellationToken) |
| | 138 | 75 | | .ConfigureAwait(false)) |
| | | 76 | | { |
| | 4 | 77 | | _ = await dbContext |
| | 4 | 78 | | .SaveChangesAsync(cancellationToken) |
| | 4 | 79 | | .ConfigureAwait(false); |
| | | 80 | | } |
| | | 81 | | |
| | 136 | 82 | | return result; |
| | 136 | 83 | | } |
| | | 84 | | } |