using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; namespace Telegrator.Hosting.Configuration { /// /// Abstract base class for configuring options from configuration sources. /// Provides a proxy pattern for binding configuration to strongly-typed options classes. /// /// The type of options to configure. public abstract class ConfigureOptionsProxy where TOptions : class { /// /// Configures the options using the default configuration section. /// /// The service collection to configure. /// The configuration source. public void Configure(IServiceCollection services, IConfiguration configuration) => Configure(services, Options.DefaultName, configuration, null); /// /// Configures the options using a named configuration section. /// /// The service collection to configure. /// The name of the configuration section. /// The configuration source. public void Configure(IServiceCollection services, string? name, IConfiguration configuration) => Configure(services, name, configuration, null); /// /// Configures the options using a named configuration section with custom binder options. /// /// The service collection to configure. /// The name of the configuration section. /// The configuration source. /// Optional action to configure the binder options. public void Configure(IServiceCollection services, string? name, IConfiguration configuration, Action? configureBinder) { var namedConfigure = new NamedConfigureFromConfigurationOptions>(name, configuration, configureBinder); namedConfigure.Configure(name, this); services.AddOptions(); services.AddSingleton(Options.Create(Realize())); } /// /// Creates the actual options instance from the configuration. /// /// The configured options instance. protected abstract TOptions Realize(); } }