< Summary

Information
Class: ProjectTemplate.Web.Extensions.DataAccessServiceExtensions
Assembly: ProjectTemplate.Web
File(s): /home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/Extensions/DataAccessServiceExtensions.cs
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 61
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddApplicationDataAccess(...)100%11100%
AddApplicationDatabaseReadinessCheck(...)100%22100%

File(s)

/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/Extensions/DataAccessServiceExtensions.cs

#LineLine coverage
 1using ProjectTemplate.Infrastructure.Data;
 2using ProjectTemplate.Infrastructure.Data.Auditing;
 3using ProjectTemplate.Infrastructure.Data.Extensions;
 4using ProjectTemplate.Infrastructure.Data.Services;
 5using ProjectTemplate.Web.Accessors;
 6using ProjectTemplate.Web.HealthChecks;
 7using ProjectTemplate.Web.Options;
 8
 9namespace ProjectTemplate.Web.Extensions;
 10
 11/// <summary>
 12/// Provides service registration methods for application data access.
 13/// </summary>
 14public static class DataAccessServiceExtensions
 15{
 16    /// <summary>
 17    /// Registers EF Core data access services for the web application.
 18    /// </summary>
 19    public static IServiceCollection AddApplicationDataAccess(
 20        this IServiceCollection services,
 21        IConfiguration configuration)
 22    {
 13923        ArgumentNullException.ThrowIfNull(services);
 13924        ArgumentNullException.ThrowIfNull(configuration);
 25
 13926        services.AddHttpContextAccessor();
 13927        services.AddScoped<ICurrentActorAccessor, HttpContextCurrentActorAccessor>();
 13928        services.AddScoped<IApplicationAuditContextAccessor, HttpContextApplicationAuditContextAccessor>();
 29
 13930        services.AddApplicationInfrastructureDataAccess(configuration);
 13331        AddApplicationDatabaseReadinessCheck(services, configuration);
 32
 13333        services.AddHostedService<DataAccessStartupLogger>();
 34
 13335        return services;
 36    }
 37
 38    private static void AddApplicationDatabaseReadinessCheck(
 39        IServiceCollection services,
 40        IConfiguration configuration)
 41    {
 42        // Bound lazily and read by the check on every run, so the setting follows the final configuration, including
 43        // sources added after service registration such as test or orchestrator overrides.
 13344        services
 13345            .AddOptions<ApplicationHealthCheckOptions>()
 13346            .Bind(configuration.GetSection(ApplicationHealthCheckOptions.SectionName));
 47
 48        // Infrastructure registers ApplicationDbContext only when a data access provider is enabled.
 13349        bool dataAccessEnabled = services.Any(descriptor => descriptor.ServiceType == typeof(ApplicationDbContext));
 50
 13351        if (!dataAccessEnabled)
 52        {
 653            return;
 54        }
 55
 12756        services.AddHealthChecks()
 12757            .AddCheck<ApplicationDatabaseHealthCheck>(
 12758                "application-database",
 12759                tags: [ApplicationHealthCheckTags.Ready, ApplicationHealthCheckTags.Database]);
 12760    }
 61}