| | 1 | | using Microsoft.EntityFrameworkCore; |
| | 2 | | using Microsoft.Extensions.Configuration; |
| | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | 4 | |
|
| | 5 | | namespace NLightning.Infrastructure.Persistence; |
| | 6 | |
|
| | 7 | | using Contexts; |
| | 8 | | using Enums; |
| | 9 | | using Providers; |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// Extension methods for setting up Persistence infrastructure services in an IServiceCollection. |
| | 13 | | /// </summary> |
| | 14 | | public 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 | | { |
| 0 | 25 | | ArgumentNullException.ThrowIfNull(services); |
| 0 | 26 | | ArgumentNullException.ThrowIfNull(configuration); |
| | 27 | |
|
| 0 | 28 | | var dbConfigSection = configuration.GetSection("Database"); |
| 0 | 29 | | var providerName = dbConfigSection["Provider"]?.ToLowerInvariant(); |
| 0 | 30 | | var connectionString = dbConfigSection["ConnectionString"]; |
| | 31 | |
|
| 0 | 32 | | if (string.IsNullOrWhiteSpace(providerName)) |
| | 33 | | { |
| 0 | 34 | | throw new InvalidOperationException("Database provider ('Database:Provider') is not configured."); |
| | 35 | | } |
| | 36 | |
|
| 0 | 37 | | if (string.IsNullOrWhiteSpace(connectionString)) |
| | 38 | | { |
| 0 | 39 | | throw new InvalidOperationException( |
| 0 | 40 | | "Database connection string ('Database:ConnectionString') is not configured."); |
| | 41 | | } |
| | 42 | |
|
| 0 | 43 | | var resolvedDatabaseType = providerName.ToLowerInvariant() switch |
| 0 | 44 | | { |
| 0 | 45 | | "postgresql" or "postgres" => DatabaseType.PostgreSql, |
| 0 | 46 | | "sqlite" => DatabaseType.Sqlite, |
| 0 | 47 | | "sqlserver" or "microsoftsql" => DatabaseType.MicrosoftSql, |
| 0 | 48 | | _ => throw new InvalidOperationException($"Unsupported database provider configured: {providerName}") |
| 0 | 49 | | }; |
| | 50 | |
|
| 0 | 51 | | services.AddSingleton(new DatabaseTypeProvider(resolvedDatabaseType)); |
| | 52 | |
|
| 0 | 53 | | services.AddDbContext<NLightningDbContext>((_, optionsBuilder) => |
| 0 | 54 | | { |
| 0 | 55 | | switch (resolvedDatabaseType) |
| 0 | 56 | | { |
| 0 | 57 | | case DatabaseType.PostgreSql: |
| 0 | 58 | | const string pgMigrationsAssembly = "NLightning.Infrastructure.Persistence.Postgres"; |
| 0 | 59 | | optionsBuilder.UseNpgsql(connectionString, sqlOptions => |
| 0 | 60 | | { |
| 0 | 61 | | sqlOptions.MigrationsAssembly(pgMigrationsAssembly); |
| 0 | 62 | | }) |
| 0 | 63 | | .EnableSensitiveDataLogging() |
| 0 | 64 | | .UseSnakeCaseNamingConvention(); |
| 0 | 65 | | break; |
| 0 | 66 | |
|
| 0 | 67 | | case DatabaseType.Sqlite: |
| 0 | 68 | | const string sqliteMigrationsAssembly = "NLightning.Infrastructure.Persistence.Sqlite"; |
| 0 | 69 | | optionsBuilder.UseSqlite(connectionString, sqlOptions => |
| 0 | 70 | | { |
| 0 | 71 | | sqlOptions.MigrationsAssembly(sqliteMigrationsAssembly); |
| 0 | 72 | | }); |
| 0 | 73 | | break; |
| 0 | 74 | |
|
| 0 | 75 | | case DatabaseType.MicrosoftSql: |
| 0 | 76 | | const string sqlServerMigrationsAssembly = "NLightning.Infrastructure.Persistence.SqlServer"; |
| 0 | 77 | | optionsBuilder.UseSqlServer(connectionString, sqlOptions => |
| 0 | 78 | | { |
| 0 | 79 | | sqlOptions.MigrationsAssembly(sqlServerMigrationsAssembly); |
| 0 | 80 | | }); |
| 0 | 81 | | break; |
| 0 | 82 | | default: |
| 0 | 83 | | throw new InvalidOperationException($"Unsupported database provider configured: {providerName}"); |
| 0 | 84 | | } |
| 0 | 85 | | }); |
| | 86 | |
|
| 0 | 87 | | return services; |
| | 88 | | } |
| | 89 | | } |