Automatically generates the registration code for IOptions and can validate them on startup using DataAnnotations or fluent validation.
- Install the NuGet package into your project.
Install-Package IeuanWalker.AppSettings
- Inherit the
IAppSettingsinterface onto the class that models your IOptions
public class ConfirmationEmailSettings : IAppSettings
{
public required string Subject { get; set; }
}-
Register the app settings
Once
IAppSettingshas been added to a class in your project, several extension methods will automatically be created. The extension method name convention is AddAppSettingsFrom + your assembly name, and the namespace is your assembly name.3.1 An extension method for
IServiceCollectionis created for all project types by defaultbuilder.Services.AddAppSettingsFromApiProjectNestedClassLibrary(builder.Configuration);
3.2 If your project is a backend/blazor project, then it will also have an extension method for
IHostApplicationBuilder, allowing you to easily chain the registration in your progam.csbuilder.AddAppSettingsFromApiProject();
3.3 If it's a MAUI project, it will also have an extension method for
MauiAppBuilder, allowing you to easily chain the registration in your MauiProgam.csbuilder.AddAppSettingsFromMauiProject();
By default, it maps the IOptions model to the section name based on the name of the model. For example, the following model -
public class ConfirmationEmailSettings : IAppSettings
{
public required string Subject { get; set; }
}Would automatically map to the following app setting section -
{
"ConfirmationEmailSettings": {
"Subject": "Test subject"
}
}If your model name and configuration section don't match or you want to bind a nested configuration, you can override this within your model by using the SectionName attribute
[SectionName("ConfirmationEmail")]
public class ConfirmationEmailSettings : IAppSettings
{
public required string Subject { get; set; }
}[SectionName("NestedConfiguration:ConfirmationEmail")]
public class ConfirmationEmailSettings : IAppSettings
{
public required string Subject { get; set; }
}You can perform validation on startup using DataAnnotations or FluentValidation.
All you need to do is add a DataAnnotation attribute onto any property
public class ConfirmationEmailSettings : IAppSettings
{
[MinLength(5)]
public required string Subject { get; set; }
}To use FluentValidation you need to create an AbstractValidator for your app settings model and add that validator to the IAppSettings inheritance.
public class ConfirmationEmailSettings : IAppSettings<ConfirmationEmailSettingsValidator>
{
public required string Subject { get; set; }
}
sealed class ConfirmationEmailSettingsValidator : AbstractValidator<ConfirmationEmailSettings>
{
public ConfirmationEmailSettingsValidator()
{
RuleFor(x => x.Subject)
.NotEmpty()
.MinimumLength(5);
}
}You can use FluentValidation without the source generator by not inheriting from IAppSettings and using the extension method
public class ConfirmationEmailSettings
{
public required string Subject { get; set; }
}
sealed class ConfirmationEmailSettingsValidator : AbstractValidator<ConfirmationEmailSettings>
{
public ConfirmationEmailSettingsValidator()
{
RuleFor(x => x.Subject)
.NotEmpty()
.MinimumLength(5);
}
}In your startup -
services.AddScoped<IValidator<ConfirmationEmailSettings>, ConfirmationEmailSettingsValidator>();
services.AddOptions<ConfirmationEmailSettings>()
.Configure(options => configuration.GetSection("FluentValidationWithValidationButNoSectionNameSettings").Bind(options))
.ValidateFluentValidation()
.ValidateOnStart();If something fails validation as the application starts up, you will get an exception explaining the exact issue -

The generated code is just standard C#/ .NET APIs -
I do not recommend adding validation to a MAUI project as it can/ will slow startup. To prevent validation, add the DontValidate attribute above your class.
[DontValidate]
public class MobileAppSettings : IAppSettings
{
public required string Subject { get; set; }
}