| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.Extensions.Configuration; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 5 | | using ProjectTemplate.Infrastructure.Data.ExternalLogins; |
| | | 6 | | using ProjectTemplate.Infrastructure.Data.Options; |
| | | 7 | | |
| | | 8 | | namespace ProjectTemplate.Infrastructure.Data.Extensions; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Provides infrastructure-owned service registration methods for application data access. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class InfrastructureDataAccessServiceExtensions |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Registers EF Core data access services for infrastructure and non-web consumers. |
| | | 17 | | /// </summary> |
| | | 18 | | public static IServiceCollection AddApplicationInfrastructureDataAccess( |
| | | 19 | | this IServiceCollection services, |
| | | 20 | | IConfiguration configuration) |
| | | 21 | | { |
| | 192 | 22 | | ArgumentNullException.ThrowIfNull(services); |
| | 192 | 23 | | ArgumentNullException.ThrowIfNull(configuration); |
| | | 24 | | |
| | 192 | 25 | | services |
| | 192 | 26 | | .AddOptions<DataAccessOptions>() |
| | 192 | 27 | | .Bind(configuration.GetSection(DataAccessOptions.SectionName)) |
| | 276 | 28 | | .Validate(options => !string.IsNullOrWhiteSpace(options.Provider), |
| | 192 | 29 | | "ProjectTemplate:DataAccess:Provider must not be empty.") |
| | 276 | 30 | | .Validate(options => DataAccessOptions.IsDisabledProvider(options.Provider) |
| | 276 | 31 | | || !string.IsNullOrWhiteSpace(options.ConnectionStringName), |
| | 192 | 32 | | "ProjectTemplate:DataAccess:ConnectionStringName must not be empty when data access is enabled.") |
| | 192 | 33 | | .ValidateOnStart(); |
| | | 34 | | |
| | 192 | 35 | | DataAccessRegistration registration = ResolveDataAccessRegistration(configuration); |
| | | 36 | | |
| | 182 | 37 | | if (registration.IsDisabled) |
| | | 38 | | { |
| | 12 | 39 | | return services; |
| | | 40 | | } |
| | | 41 | | |
| | 170 | 42 | | services.TryAddScoped<ICurrentActorAccessor, SystemCurrentActorAccessor>(); |
| | | 43 | | |
| | 186 | 44 | | services.AddDbContext<ApplicationDbContext>(options => ConfigureProvider( |
| | 186 | 45 | | options, |
| | 186 | 46 | | registration.Provider, |
| | 186 | 47 | | registration.ConnectionString)); |
| | | 48 | | |
| | 170 | 49 | | services.AddDbContextFactory<ApplicationDbContext>( |
| | 14 | 50 | | options => ConfigureProvider( |
| | 14 | 51 | | options, |
| | 14 | 52 | | registration.Provider, |
| | 14 | 53 | | registration.ConnectionString), |
| | 170 | 54 | | ServiceLifetime.Scoped); |
| | | 55 | | |
| | 170 | 56 | | services.TryAddScoped<IExternalLoginAccountResolver, EfCoreExternalLoginAccountResolver>(); |
| | | 57 | | |
| | 170 | 58 | | return services; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | private static DataAccessRegistration ResolveDataAccessRegistration( |
| | | 62 | | IConfiguration configuration) |
| | | 63 | | { |
| | 192 | 64 | | DataAccessOptions dataAccessOptions = configuration |
| | 192 | 65 | | .GetSection(DataAccessOptions.SectionName) |
| | 192 | 66 | | .Get<DataAccessOptions>() ?? new DataAccessOptions(); |
| | | 67 | | |
| | 192 | 68 | | string provider = dataAccessOptions.Provider?.Trim() ?? string.Empty; |
| | 192 | 69 | | string connectionStringName = dataAccessOptions.ConnectionStringName?.Trim() ?? string.Empty; |
| | | 70 | | |
| | 192 | 71 | | if (string.IsNullOrWhiteSpace(provider)) |
| | | 72 | | { |
| | 4 | 73 | | throw new InvalidOperationException( |
| | 4 | 74 | | "Application data access provider was not configured."); |
| | | 75 | | } |
| | | 76 | | |
| | 188 | 77 | | if (DataAccessOptions.IsDisabledProvider(provider)) |
| | | 78 | | { |
| | 12 | 79 | | return DataAccessRegistration.Disabled(provider); |
| | | 80 | | } |
| | | 81 | | |
| | 176 | 82 | | if (string.IsNullOrWhiteSpace(connectionStringName)) |
| | | 83 | | { |
| | 4 | 84 | | throw new InvalidOperationException( |
| | 4 | 85 | | "Application data access connection string name was not configured."); |
| | | 86 | | } |
| | | 87 | | |
| | 172 | 88 | | string connectionString = configuration.GetConnectionString(connectionStringName) |
| | 172 | 89 | | ?? throw new InvalidOperationException( |
| | 172 | 90 | | $"Connection string '{connectionStringName}' was not configured."); |
| | | 91 | | |
| | 170 | 92 | | return DataAccessRegistration.Enabled( |
| | 170 | 93 | | provider, |
| | 170 | 94 | | connectionString); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | private static void ConfigureProvider( |
| | | 98 | | DbContextOptionsBuilder options, |
| | | 99 | | string provider, |
| | | 100 | | string connectionString) |
| | | 101 | | { |
| | 30 | 102 | | if (provider.Equals(DataAccessOptions.SqliteProvider, StringComparison.OrdinalIgnoreCase)) |
| | | 103 | | { |
| | 16 | 104 | | options.UseSqlite(connectionString); |
| | 16 | 105 | | return; |
| | | 106 | | } |
| | | 107 | | |
| | 14 | 108 | | if (provider.Equals(DataAccessOptions.SqlServerProvider, StringComparison.OrdinalIgnoreCase)) |
| | | 109 | | { |
| | 12 | 110 | | options.UseSqlServer(connectionString); |
| | 12 | 111 | | return; |
| | | 112 | | } |
| | | 113 | | |
| | 2 | 114 | | throw new InvalidOperationException( |
| | 2 | 115 | | $"Unsupported data access provider '{provider}'. Supported providers: {DataAccessOptions.SqliteProvider}, {D |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | private readonly record struct DataAccessRegistration( |
| | 30 | 119 | | string Provider, |
| | 30 | 120 | | string ConnectionString, |
| | 182 | 121 | | bool IsDisabled) |
| | | 122 | | { |
| | | 123 | | public static DataAccessRegistration Enabled( |
| | | 124 | | string provider, |
| | | 125 | | string connectionString) |
| | | 126 | | { |
| | 170 | 127 | | return new DataAccessRegistration(provider, connectionString, false); |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | public static DataAccessRegistration Disabled( |
| | | 131 | | string provider) |
| | | 132 | | { |
| | 12 | 133 | | return new DataAccessRegistration(provider, string.Empty, true); |
| | | 134 | | } |
| | | 135 | | } |
| | | 136 | | } |