< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Persistence.DependencyInjection
Assembly: NLightning.Infrastructure.Persistence
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Persistence/DependencyInjection.cs
Tag: 36_15743069263
Line coverage
0%
Covered lines: 0
Uncovered lines: 52
Coverable lines: 52
Total lines: 89
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 20
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddPersistenceInfrastructureServices(...)0%420200%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Persistence/DependencyInjection.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using Microsoft.Extensions.Configuration;
 3using Microsoft.Extensions.DependencyInjection;
 4
 5namespace NLightning.Infrastructure.Persistence;
 6
 7using Contexts;
 8using Enums;
 9using Providers;
 10
 11/// <summary>
 12/// Extension methods for setting up Persistence infrastructure services in an IServiceCollection.
 13/// </summary>
 14public static class DependencyInjection
 15{
 16    /// <summary>
 17    /// Adds Bitcoin infrastructure services to the specified IServiceCollection.
 18    /// </summary>
 19    /// <param name="services">The IServiceCollection to add services to.</param>
 20    /// <param name="configuration">The IConfiguration instance to read configuration settings from.</param>
 21    /// <returns>The same service collection so that multiple calls can be chained.</returns>
 22    public static IServiceCollection AddPersistenceInfrastructureServices(this IServiceCollection services,
 23                                                                          IConfiguration configuration)
 24    {
 025        ArgumentNullException.ThrowIfNull(services);
 026        ArgumentNullException.ThrowIfNull(configuration);
 27
 028        var dbConfigSection = configuration.GetSection("Database");
 029        var providerName = dbConfigSection["Provider"]?.ToLowerInvariant();
 030        var connectionString = dbConfigSection["ConnectionString"];
 31
 032        if (string.IsNullOrWhiteSpace(providerName))
 33        {
 034            throw new InvalidOperationException("Database provider ('Database:Provider') is not configured.");
 35        }
 36
 037        if (string.IsNullOrWhiteSpace(connectionString))
 38        {
 039            throw new InvalidOperationException(
 040                "Database connection string ('Database:ConnectionString') is not configured.");
 41        }
 42
 043        var resolvedDatabaseType = providerName.ToLowerInvariant() switch
 044        {
 045            "postgresql" or "postgres" => DatabaseType.PostgreSql,
 046            "sqlite" => DatabaseType.Sqlite,
 047            "sqlserver" or "microsoftsql" => DatabaseType.MicrosoftSql,
 048            _ => throw new InvalidOperationException($"Unsupported database provider configured: {providerName}")
 049        };
 50
 051        services.AddSingleton(new DatabaseTypeProvider(resolvedDatabaseType));
 52
 053        services.AddDbContext<NLightningDbContext>((_, optionsBuilder) =>
 054        {
 055            switch (resolvedDatabaseType)
 056            {
 057                case DatabaseType.PostgreSql:
 058                    const string pgMigrationsAssembly = "NLightning.Infrastructure.Persistence.Postgres";
 059                    optionsBuilder.UseNpgsql(connectionString, sqlOptions =>
 060                                   {
 061                                       sqlOptions.MigrationsAssembly(pgMigrationsAssembly);
 062                                   })
 063                                  .EnableSensitiveDataLogging()
 064                                  .UseSnakeCaseNamingConvention();
 065                    break;
 066
 067                case DatabaseType.Sqlite:
 068                    const string sqliteMigrationsAssembly = "NLightning.Infrastructure.Persistence.Sqlite";
 069                    optionsBuilder.UseSqlite(connectionString, sqlOptions =>
 070                    {
 071                        sqlOptions.MigrationsAssembly(sqliteMigrationsAssembly);
 072                    });
 073                    break;
 074
 075                case DatabaseType.MicrosoftSql:
 076                    const string sqlServerMigrationsAssembly = "NLightning.Infrastructure.Persistence.SqlServer";
 077                    optionsBuilder.UseSqlServer(connectionString, sqlOptions =>
 078                    {
 079                        sqlOptions.MigrationsAssembly(sqlServerMigrationsAssembly);
 080                    });
 081                    break;
 082                default:
 083                    throw new InvalidOperationException($"Unsupported database provider configured: {providerName}");
 084            }
 085        });
 86
 087        return services;
 88    }
 89}