| | | 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.Auditing; |
| | | 6 | | using ProjectTemplate.Infrastructure.Data.ExternalLogins; |
| | | 7 | | using ProjectTemplate.Infrastructure.Data.Options; |
| | | 8 | | |
| | | 9 | | namespace ProjectTemplate.Infrastructure.Data.Extensions; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Provides infrastructure-owned service registration methods for application data access. |
| | | 13 | | /// </summary> |
| | | 14 | | public static class InfrastructureDataAccessServiceExtensions |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Registers EF Core data access services for infrastructure and non-web consumers. |
| | | 18 | | /// </summary> |
| | | 19 | | public static IServiceCollection AddApplicationInfrastructureDataAccess( |
| | | 20 | | this IServiceCollection services, |
| | | 21 | | IConfiguration configuration) |
| | | 22 | | { |
| | 212 | 23 | | ArgumentNullException.ThrowIfNull(services); |
| | 212 | 24 | | ArgumentNullException.ThrowIfNull(configuration); |
| | | 25 | | |
| | 212 | 26 | | services |
| | 212 | 27 | | .AddOptions<DataAccessOptions>() |
| | 212 | 28 | | .Bind(configuration.GetSection(DataAccessOptions.SectionName)) |
| | 286 | 29 | | .Validate(options => !string.IsNullOrWhiteSpace(options.Provider), |
| | 212 | 30 | | "ProjectTemplate:DataAccess:Provider must not be empty.") |
| | 286 | 31 | | .Validate(options => DataAccessOptions.IsDisabledProvider(options.Provider) |
| | 286 | 32 | | || !string.IsNullOrWhiteSpace(options.ConnectionStringName), |
| | 212 | 33 | | "ProjectTemplate:DataAccess:ConnectionStringName must not be empty when data access is enabled.") |
| | 286 | 34 | | .Validate(options => AuditStorageModes.IsSupported(options.Auditing.StorageMode), |
| | 212 | 35 | | "ProjectTemplate:DataAccess:Auditing:StorageMode must be Local, Outbox, or ExternalSink.") |
| | 212 | 36 | | .ValidateOnStart(); |
| | | 37 | | |
| | 212 | 38 | | DataAccessRegistration registration = ResolveDataAccessRegistration(configuration); |
| | 200 | 39 | | if (registration.IsDisabled) |
| | | 40 | | { |
| | 12 | 41 | | return services; |
| | | 42 | | } |
| | | 43 | | |
| | 188 | 44 | | services.TryAddScoped<ICurrentActorAccessor, SystemCurrentActorAccessor>(); |
| | 188 | 45 | | services.TryAddScoped<IApplicationAuditContextAccessor, SystemApplicationAuditContextAccessor>(); |
| | 188 | 46 | | services.TryAddScoped<IApplicationAuditValuePolicy, DefaultApplicationAuditValuePolicy>(); |
| | 188 | 47 | | services.TryAddSingleton<IApplicationMutationManifestBuilder, CanonicalApplicationMutationManifestBuilder>(); |
| | 188 | 48 | | services.TryAddSingleton<IApplicationMutationManifestHasher, Sha256ApplicationMutationManifestHasher>(); |
| | 188 | 49 | | services.TryAddSingleton<IApplicationMutationManifestVerifier, ApplicationMutationManifestVerifier>(); |
| | | 50 | | |
| | 188 | 51 | | if (AuditStorageModes.IsLocal(registration.AuditStorageMode)) |
| | | 52 | | { |
| | 186 | 53 | | services.TryAddScoped<IApplicationAuditStore, LocalApplicationAuditStore>(); |
| | | 54 | | } |
| | | 55 | | |
| | 188 | 56 | | services.TryAddScoped<ContextIsolatedApplicationSaveChangesPipeline>(); |
| | 188 | 57 | | services.TryAddScoped<IApplicationSaveChangesPipeline>(serviceProvider => |
| | 204 | 58 | | serviceProvider.GetRequiredService<ContextIsolatedApplicationSaveChangesPipeline>()); |
| | 188 | 59 | | services.TryAddScoped<IApplicationMutationAuditReceiptRegistry>(serviceProvider => |
| | 192 | 60 | | serviceProvider.GetRequiredService<ContextIsolatedApplicationSaveChangesPipeline>()); |
| | 188 | 61 | | services.TryAddScoped<IApplicationMutationAuditReceiptAccessor, ApplicationDbContextMutationAuditReceiptAccessor |
| | 188 | 62 | | services.TryAddScoped<ApplicationSaveChangesInterceptor>(); |
| | | 63 | | |
| | 206 | 64 | | services.AddDbContext<ApplicationDbContext>(options => ConfigureProvider( |
| | 206 | 65 | | options, |
| | 206 | 66 | | registration.Provider, |
| | 206 | 67 | | registration.ConnectionString)); |
| | | 68 | | |
| | 188 | 69 | | services.AddDbContextFactory<ApplicationDbContext>( |
| | 16 | 70 | | options => ConfigureProvider( |
| | 16 | 71 | | options, |
| | 16 | 72 | | registration.Provider, |
| | 16 | 73 | | registration.ConnectionString), |
| | 188 | 74 | | ServiceLifetime.Scoped); |
| | | 75 | | |
| | 188 | 76 | | services.TryAddScoped<IExternalLoginAccountResolver, EfCoreExternalLoginAccountResolver>(); |
| | 188 | 77 | | return services; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | private static DataAccessRegistration ResolveDataAccessRegistration(IConfiguration configuration) |
| | | 81 | | { |
| | 212 | 82 | | DataAccessOptions dataAccessOptions = configuration |
| | 212 | 83 | | .GetSection(DataAccessOptions.SectionName) |
| | 212 | 84 | | .Get<DataAccessOptions>() ?? new DataAccessOptions(); |
| | | 85 | | |
| | 212 | 86 | | string provider = dataAccessOptions.Provider?.Trim() ?? string.Empty; |
| | 212 | 87 | | string connectionStringName = dataAccessOptions.ConnectionStringName?.Trim() ?? string.Empty; |
| | 212 | 88 | | string auditStorageMode = AuditStorageModes.Normalize(dataAccessOptions.Auditing.StorageMode); |
| | | 89 | | |
| | 212 | 90 | | if (string.IsNullOrWhiteSpace(provider)) |
| | | 91 | | { |
| | 4 | 92 | | throw new InvalidOperationException("Application data access provider was not configured."); |
| | | 93 | | } |
| | | 94 | | |
| | 208 | 95 | | if (!AuditStorageModes.IsSupported(auditStorageMode)) |
| | | 96 | | { |
| | 2 | 97 | | throw new InvalidOperationException("Application audit storage mode was not configured with a supported valu |
| | | 98 | | } |
| | | 99 | | |
| | 206 | 100 | | if (DataAccessOptions.IsDisabledProvider(provider)) |
| | | 101 | | { |
| | 12 | 102 | | return DataAccessRegistration.Disabled(provider, auditStorageMode); |
| | | 103 | | } |
| | | 104 | | |
| | 194 | 105 | | if (string.IsNullOrWhiteSpace(connectionStringName)) |
| | | 106 | | { |
| | 4 | 107 | | throw new InvalidOperationException("Application data access connection string name was not configured."); |
| | | 108 | | } |
| | | 109 | | |
| | 190 | 110 | | string connectionString = configuration.GetConnectionString(connectionStringName) |
| | 190 | 111 | | ?? throw new InvalidOperationException($"Connection string '{connectionStringName}' was not configured."); |
| | | 112 | | |
| | 188 | 113 | | return DataAccessRegistration.Enabled(provider, connectionString, auditStorageMode); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | private static void ConfigureProvider( |
| | | 117 | | DbContextOptionsBuilder options, |
| | | 118 | | string provider, |
| | | 119 | | string connectionString) |
| | | 120 | | { |
| | 34 | 121 | | if (provider.Equals(DataAccessOptions.SqliteProvider, StringComparison.OrdinalIgnoreCase)) |
| | | 122 | | { |
| | 20 | 123 | | options.UseSqlite(connectionString); |
| | 20 | 124 | | return; |
| | | 125 | | } |
| | | 126 | | |
| | 14 | 127 | | if (provider.Equals(DataAccessOptions.SqlServerProvider, StringComparison.OrdinalIgnoreCase)) |
| | | 128 | | { |
| | 12 | 129 | | options.UseSqlServer(connectionString); |
| | 12 | 130 | | return; |
| | | 131 | | } |
| | | 132 | | |
| | 2 | 133 | | throw new InvalidOperationException( |
| | 2 | 134 | | $"Unsupported data access provider '{provider}'. Supported providers: {DataAccessOptions.SqliteProvider}, {D |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | private readonly record struct DataAccessRegistration( |
| | 34 | 138 | | string Provider, |
| | 34 | 139 | | string ConnectionString, |
| | 188 | 140 | | string AuditStorageMode, |
| | 200 | 141 | | bool IsDisabled) |
| | | 142 | | { |
| | | 143 | | public static DataAccessRegistration Enabled( |
| | | 144 | | string provider, |
| | | 145 | | string connectionString, |
| | | 146 | | string auditStorageMode) |
| | | 147 | | { |
| | 188 | 148 | | return new(provider, connectionString, auditStorageMode, false); |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | public static DataAccessRegistration Disabled( |
| | | 152 | | string provider, |
| | | 153 | | string auditStorageMode) |
| | | 154 | | { |
| | 12 | 155 | | return new(provider, string.Empty, auditStorageMode, true); |
| | | 156 | | } |
| | | 157 | | } |
| | | 158 | | } |