| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 3 | | using Microsoft.Extensions.Options; |
| | | 4 | | using ProjectTemplate.Infrastructure.Data.Auditing; |
| | | 5 | | |
| | | 6 | | namespace ProjectTemplate.Infrastructure.Data.Extensions; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides opt-in registration for durable audit-completion outbox services. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class ApplicationAuditCompletionOutboxServiceExtensions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Registers durable staging, dispatch, and minimized operational query services. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <remarks> |
| | | 17 | | /// Call <see cref="InfrastructureDataAccessServiceExtensions" /> before this method. |
| | | 18 | | /// This infrastructure registration does not start a background dispatcher loop. |
| | | 19 | | /// </remarks> |
| | | 20 | | public static IServiceCollection AddApplicationAuditCompletionOutboxCore( |
| | | 21 | | this IServiceCollection services, |
| | | 22 | | Action<ApplicationAuditCompletionOutboxOptions>? configure = null) |
| | | 23 | | { |
| | 0 | 24 | | ArgumentNullException.ThrowIfNull(services); |
| | | 25 | | |
| | 0 | 26 | | OptionsBuilder<ApplicationAuditCompletionOutboxOptions> optionsBuilder = services |
| | 0 | 27 | | .AddOptions<ApplicationAuditCompletionOutboxOptions>() |
| | 0 | 28 | | .Validate(options => !string.IsNullOrWhiteSpace(options.DefaultDestination), |
| | 0 | 29 | | "The default audit-completion destination must not be empty.") |
| | 0 | 30 | | .Validate(options => options.DefaultDestination.Length <= 128, |
| | 0 | 31 | | "The default audit-completion destination cannot exceed 128 characters.") |
| | 0 | 32 | | .Validate(options => options.BatchSize > 0, |
| | 0 | 33 | | "The audit-completion outbox batch size must be greater than zero.") |
| | 0 | 34 | | .Validate(options => options.PollInterval > TimeSpan.Zero, |
| | 0 | 35 | | "The audit-completion polling interval must be greater than zero.") |
| | 0 | 36 | | .Validate(options => options.BaseRetryDelay > TimeSpan.Zero, |
| | 0 | 37 | | "The base audit-completion retry delay must be greater than zero.") |
| | 0 | 38 | | .Validate(options => options.MaxRetryDelay >= options.BaseRetryDelay, |
| | 0 | 39 | | "The maximum audit-completion retry delay must not be less than the base delay.") |
| | 0 | 40 | | .Validate(options => options.DeferredRetryDelay > TimeSpan.Zero, |
| | 0 | 41 | | "The deferred audit-completion retry delay must be greater than zero.") |
| | 0 | 42 | | .Validate(options => options.MaxRetryAttempts > 0, |
| | 0 | 43 | | "The maximum audit-completion retry attempts must be greater than zero.") |
| | 0 | 44 | | .Validate(options => options.MaxErrorDetailLength is > 0 and <= 512, |
| | 0 | 45 | | "The audit-completion error detail length must be between 1 and 512 characters.") |
| | 0 | 46 | | .ValidateOnStart(); |
| | | 47 | | |
| | 0 | 48 | | if (configure is not null) |
| | | 49 | | { |
| | 0 | 50 | | optionsBuilder.Configure(configure); |
| | | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | services.TryAddSingleton(TimeProvider.System); |
| | 0 | 54 | | services.TryAddScoped<ApplicationAuditCompletionOutbox>(); |
| | 0 | 55 | | services.TryAddScoped<IApplicationAuditCompletionOutbox>(serviceProvider => |
| | 0 | 56 | | serviceProvider.GetRequiredService<ApplicationAuditCompletionOutbox>()); |
| | 0 | 57 | | services.TryAddScoped<IApplicationAuditCompletionOutboxDispatcher>(serviceProvider => |
| | 0 | 58 | | serviceProvider.GetRequiredService<ApplicationAuditCompletionOutbox>()); |
| | 0 | 59 | | services.TryAddScoped<IApplicationAuditCompletionOutboxQuery>(serviceProvider => |
| | 0 | 60 | | serviceProvider.GetRequiredService<ApplicationAuditCompletionOutbox>()); |
| | | 61 | | |
| | 0 | 62 | | return services; |
| | | 63 | | } |
| | | 64 | | } |