Extension methods to register settings
Install nuget package
dotnet add package Settings.Extensions.Configurationthen add the following code
using Settings.Extensions.Configuration;
...
services.AddSettings<SomeApplicationSettings>(configuration);
This will call AddOptions which will register default options types(IOptions<>, IOptionsSnapshot<>, IOptionsMonitor<>), but also register settings type as well.
I.e., unlike AddOptions when resolving SomeApplicationSettings there is no need to resolve IOptions<SomeApplicationSettings>, but instead you can simply write
SomeService(SomeApplicationSettings settings)
{
...
}By default the settings type name will be used to map settings, but this can be overridden by specifying name parameter of the AddSettings method.
services.AddSettings<SomeSettings>(configuration, name:"DifferentName");This extension method returns tuple of IServiceCollection, IConfiguration, so that you can chain multiple settings without passing configuration every time
services.AddSettings<SomeSettings>(configuration)
.AddSettings<OtherSettings>()
.AddSettings<SomeElseSettings>();It is possible to specify validation by passing validation delegate, for example:
services
.AddSettings<SomeSettings>(configuration, OptionsBuilderDataAnnotationsExtensions.ValidateDataAnnotations) // using data annotation
.AddSettings<OtherSettings>(s => s.AddSingleton<IValidateOptions<OtherSettings>>(new CustomValidation<OtherSettings>>())); // custom validation