< 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: 84
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>
 18016    public ApplicationSaveChangesInterceptor(IApplicationSaveChangesPipeline saveChangesPipeline)
 17    {
 18018        ArgumentNullException.ThrowIfNull(saveChangesPipeline);
 19
 17820        _saveChangesPipeline = saveChangesPipeline;
 17821    }
 22
 23    /// <inheritdoc />
 24    public override InterceptionResult<int> SavingChanges(
 25        DbContextEventData eventData,
 26        InterceptionResult<int> result)
 27    {
 2228        if (eventData.Context is ApplicationDbContext dbContext)
 29        {
 2030            _ = _saveChangesPipeline.ApplyBeforeSaveChanges(dbContext);
 31        }
 32
 2233        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    {
 14242        if (eventData.Context is ApplicationDbContext dbContext)
 43        {
 14044            _ = await _saveChangesPipeline
 14045                .ApplyBeforeSaveChangesAsync(dbContext, cancellationToken)
 14046                .ConfigureAwait(false);
 47        }
 48
 14249        return result;
 14250    }
 51
 52    /// <inheritdoc />
 53    public override int SavedChanges(
 54        SaveChangesCompletedEventData eventData,
 55        int result)
 56    {
 2057        if (eventData.Context is ApplicationDbContext dbContext &&
 2058            _saveChangesPipeline.ApplyAfterSaveChanges(dbContext))
 59        {
 260            _ = dbContext.SaveChanges();
 61        }
 62
 2063        return result;
 64    }
 65
 66    /// <inheritdoc />
 67    public override async ValueTask<int> SavedChangesAsync(
 68        SaveChangesCompletedEventData eventData,
 69        int result,
 70        CancellationToken cancellationToken = default)
 71    {
 13872        if (eventData.Context is ApplicationDbContext dbContext &&
 13873            await _saveChangesPipeline
 13874                .ApplyAfterSaveChangesAsync(dbContext, cancellationToken)
 13875                .ConfigureAwait(false))
 76        {
 477            _ = await dbContext
 478                .SaveChangesAsync(cancellationToken)
 479                .ConfigureAwait(false);
 80        }
 81
 13682        return result;
 13683    }
 84}