diff --git a/EnvelopeGenerator.DependencyInjection/DependencyInjection.cs b/EnvelopeGenerator.DependencyInjection/DependencyInjection.cs index e4122c60..cf7f870d 100644 --- a/EnvelopeGenerator.DependencyInjection/DependencyInjection.cs +++ b/EnvelopeGenerator.DependencyInjection/DependencyInjection.cs @@ -11,10 +11,11 @@ namespace EnvelopeGenerator.DependencyInjection; public static class DependencyInjection { - public static IServiceCollection AddEnvelopeGenerator(this IServiceCollection services, IConfiguration config, Action options, Action distributedCacheOptions) + public static IServiceCollection AddEnvelopeGenerator(this IServiceCollection services, Action options) { var egConfig = new EGConfiguration(); options.Invoke(egConfig); + egConfig.EnsureAllServicesConfigured(); egConfig.RegisterAll(services); // Add envelope generator services @@ -29,41 +30,67 @@ public static class DependencyInjection public record EGConfiguration { - internal readonly Queue> ServiceRegs = new(); + internal readonly Queue> _serviceRegs = new(); internal void RegisterAll(IServiceCollection services) { - while (ServiceRegs.Count > 0) - ServiceRegs.Dequeue().Invoke(services); + while (_serviceRegs.Count > 0) + _serviceRegs.Dequeue().Invoke(services); } - public EGConfiguration AddLocalization() + private readonly Dictionary _addingStatus = new () { - ServiceRegs.Enqueue(s => s.AddLocalization()); + { nameof(AddLocalization), false }, + { nameof(AddDistributedSqlServerCache), false }, + { nameof(AddInfrastructure), false }, + { nameof(AddServices), false }, + }; + + public EGConfiguration AddLocalization(Action? customLocalizationOptions = null) + { + _serviceRegs.Enqueue(customLocalizationOptions ?? (s => s.AddLocalization())); + _addingStatus[nameof(AddLocalization)] = true; return this; } public EGConfiguration AddDistributedSqlServerCache(Action setupAction) { - ServiceRegs.Enqueue(s => s.AddDistributedSqlServerCache(setupAction)); + _serviceRegs.Enqueue(s => s.AddDistributedSqlServerCache(setupAction)); + _addingStatus[nameof(AddDistributedSqlServerCache)] = true; return this; } public EGConfiguration AddInfrastructure(Action options) { #pragma warning disable CS0618 - ServiceRegs.Enqueue(s => s.AddEGInfrastructureServices(options)); + _serviceRegs.Enqueue(s => s.AddEGInfrastructureServices(options)); #pragma warning restore CS0618 + _addingStatus[nameof(AddInfrastructure)] = true; return this; } - public IConfiguration Configuration + public EGConfiguration AddServices(IConfiguration config) { - set - { #pragma warning disable CS0618 - ServiceRegs.Enqueue(s => s.AddEnvelopeGeneratorServices(value)); + _serviceRegs.Enqueue(s => s.AddEnvelopeGeneratorServices(config)); #pragma warning restore CS0618 + _addingStatus[nameof(AddServices)] = true; + return this; + } + + internal void EnsureAllServicesConfigured() + { + var missingServices = _addingStatus + .Where(kv => !kv.Value) + .Select(kv => kv.Key) + .ToList(); + + if (missingServices.Count > 0) + { + var missingList = string.Join(", ", missingServices); + throw new InvalidOperationException( + $"Service configuration incomplete. The following required service methods were not called: {missingList}. " + + "Please ensure all necessary configuration methods are invoked before building the application."); } } }