< Summary

Information
Class: ProjectTemplate.Infrastructure.Data.ApplicationSaveChangesInterceptor
Assembly: ProjectTemplate.Infrastructure
File(s): /home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Infrastructure/Data/ApplicationSaveChangesInterceptor.cs
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 113
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
SavingChanges(...)100%22100%
SavingChangesAsync()100%22100%
SavedChanges(...)100%44100%
SavedChangesAsync()100%44100%

File(s)

/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Infrastructure/Data/ApplicationSaveChangesInterceptor.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore.Diagnostics;
 2
 3namespace ProjectTemplate.Infrastructure.Data;
 4
 5/// <summary>
 6/// Invokes the application save pipeline from the EF Core SaveChanges interception lifecycle.
 7/// </summary>
 8public 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>
 11716    public ApplicationSaveChangesInterceptor(IApplicationSaveChangesPipeline saveChangesPipeline)
 17    {
 11718        ArgumentNullException.ThrowIfNull(saveChangesPipeline);
 19
 11620        _saveChangesPipeline = saveChangesPipeline;
 11621    }
 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    {
 1234        if (eventData.Context is ApplicationDbContext dbContext)
 35        {
 1136            _ = _saveChangesPipeline.ApplyBeforeSaveChanges(dbContext);
 37        }
 38
 1239        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    {
 9456        if (eventData.Context is ApplicationDbContext dbContext)
 57        {
 9358            _ = await _saveChangesPipeline
 9359                .ApplyBeforeSaveChangesAsync(dbContext, cancellationToken)
 9360                .ConfigureAwait(false);
 61        }
 62
 9463        return result;
 9464    }
 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    {
 1178        if (eventData.Context is ApplicationDbContext dbContext &&
 1179            _saveChangesPipeline.ApplyAfterSaveChanges(dbContext))
 80        {
 181            _ = dbContext.SaveChanges();
 82        }
 83
 1184        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    {
 92101        if (eventData.Context is ApplicationDbContext dbContext &&
 92102            await _saveChangesPipeline
 92103                .ApplyAfterSaveChangesAsync(dbContext, cancellationToken)
 92104                .ConfigureAwait(false))
 105        {
 2106            _ = await dbContext
 2107                .SaveChangesAsync(cancellationToken)
 2108                .ConfigureAwait(false);
 109        }
 110
 91111        return result;
 91112    }
 113}