| | | 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> |
| | 117 | 16 | | public ApplicationSaveChangesInterceptor(IApplicationSaveChangesPipeline saveChangesPipeline) |
| | | 17 | | { |
| | 117 | 18 | | ArgumentNullException.ThrowIfNull(saveChangesPipeline); |
| | | 19 | | |
| | 116 | 20 | | _saveChangesPipeline = saveChangesPipeline; |
| | 116 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Runs <see cref="IApplicationSaveChangesPipeline.ApplyBeforeSaveChanges" /> before EF Core persists the |
| | | 25 | | /// tracked changes of an <see cref="ApplicationDbContext" />. Other contexts pass through unchanged. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="eventData">Contextual information about the save operation.</param> |
| | | 28 | | /// <param name="result">The current interception result, returned unchanged.</param> |
| | | 29 | | /// <returns>The <paramref name="result" /> supplied by EF Core.</returns> |
| | | 30 | | public override InterceptionResult<int> SavingChanges( |
| | | 31 | | DbContextEventData eventData, |
| | | 32 | | InterceptionResult<int> result) |
| | | 33 | | { |
| | 12 | 34 | | if (eventData.Context is ApplicationDbContext dbContext) |
| | | 35 | | { |
| | 11 | 36 | | _ = _saveChangesPipeline.ApplyBeforeSaveChanges(dbContext); |
| | | 37 | | } |
| | | 38 | | |
| | 12 | 39 | | return result; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Runs <see cref="IApplicationSaveChangesPipeline.ApplyBeforeSaveChangesAsync" /> before EF Core |
| | | 44 | | /// asynchronously persists the tracked changes of an <see cref="ApplicationDbContext" />. Other contexts pass |
| | | 45 | | /// through unchanged. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="eventData">Contextual information about the save operation.</param> |
| | | 48 | | /// <param name="result">The current interception result, returned unchanged.</param> |
| | | 49 | | /// <param name="cancellationToken">A token that can cancel the operation.</param> |
| | | 50 | | /// <returns>The <paramref name="result" /> supplied by EF Core.</returns> |
| | | 51 | | public override async ValueTask<InterceptionResult<int>> SavingChangesAsync( |
| | | 52 | | DbContextEventData eventData, |
| | | 53 | | InterceptionResult<int> result, |
| | | 54 | | CancellationToken cancellationToken = default) |
| | | 55 | | { |
| | 94 | 56 | | if (eventData.Context is ApplicationDbContext dbContext) |
| | | 57 | | { |
| | 93 | 58 | | _ = await _saveChangesPipeline |
| | 93 | 59 | | .ApplyBeforeSaveChangesAsync(dbContext, cancellationToken) |
| | 93 | 60 | | .ConfigureAwait(false); |
| | | 61 | | } |
| | | 62 | | |
| | 94 | 63 | | return result; |
| | 94 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Runs <see cref="IApplicationSaveChangesPipeline.ApplyAfterSaveChanges" /> after EF Core persists the tracked |
| | | 68 | | /// changes of an <see cref="ApplicationDbContext" />, and saves again when the pipeline appended audit records |
| | | 69 | | /// that depend on database-generated values. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="eventData">Contextual information about the completed save operation.</param> |
| | | 72 | | /// <param name="result">The number of state entries written by the original save.</param> |
| | | 73 | | /// <returns>The <paramref name="result" /> supplied by EF Core.</returns> |
| | | 74 | | public override int SavedChanges( |
| | | 75 | | SaveChangesCompletedEventData eventData, |
| | | 76 | | int result) |
| | | 77 | | { |
| | 11 | 78 | | if (eventData.Context is ApplicationDbContext dbContext && |
| | 11 | 79 | | _saveChangesPipeline.ApplyAfterSaveChanges(dbContext)) |
| | | 80 | | { |
| | 1 | 81 | | _ = dbContext.SaveChanges(); |
| | | 82 | | } |
| | | 83 | | |
| | 11 | 84 | | return result; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Runs <see cref="IApplicationSaveChangesPipeline.ApplyAfterSaveChangesAsync" /> after EF Core asynchronously |
| | | 89 | | /// persists the tracked changes of an <see cref="ApplicationDbContext" />, and saves again when the pipeline |
| | | 90 | | /// appended audit records that depend on database-generated values. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="eventData">Contextual information about the completed save operation.</param> |
| | | 93 | | /// <param name="result">The number of state entries written by the original save.</param> |
| | | 94 | | /// <param name="cancellationToken">A token that can cancel the operation.</param> |
| | | 95 | | /// <returns>The <paramref name="result" /> supplied by EF Core.</returns> |
| | | 96 | | public override async ValueTask<int> SavedChangesAsync( |
| | | 97 | | SaveChangesCompletedEventData eventData, |
| | | 98 | | int result, |
| | | 99 | | CancellationToken cancellationToken = default) |
| | | 100 | | { |
| | 92 | 101 | | if (eventData.Context is ApplicationDbContext dbContext && |
| | 92 | 102 | | await _saveChangesPipeline |
| | 92 | 103 | | .ApplyAfterSaveChangesAsync(dbContext, cancellationToken) |
| | 92 | 104 | | .ConfigureAwait(false)) |
| | | 105 | | { |
| | 2 | 106 | | _ = await dbContext |
| | 2 | 107 | | .SaveChangesAsync(cancellationToken) |
| | 2 | 108 | | .ConfigureAwait(false); |
| | | 109 | | } |
| | | 110 | | |
| | 91 | 111 | | return result; |
| | 91 | 112 | | } |
| | | 113 | | } |