IeuanWalker.AppSettings 0.0.1-Preview.8

Prefix Reserved
This is a prerelease version of IeuanWalker.AppSettings.
There is a newer version of this package available.
See the version list below for details.
dotnet add package IeuanWalker.AppSettings --version 0.0.1-Preview.8
                    
NuGet\Install-Package IeuanWalker.AppSettings -Version 0.0.1-Preview.8
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="IeuanWalker.AppSettings" Version="0.0.1-Preview.8" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="IeuanWalker.AppSettings" Version="0.0.1-Preview.8" />
                    
Directory.Packages.props
<PackageReference Include="IeuanWalker.AppSettings" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add IeuanWalker.AppSettings --version 0.0.1-Preview.8
                    
#r "nuget: IeuanWalker.AppSettings, 0.0.1-Preview.8"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package IeuanWalker.AppSettings@0.0.1-Preview.8
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=IeuanWalker.AppSettings&version=0.0.1-Preview.8&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=IeuanWalker.AppSettings&version=0.0.1-Preview.8&prerelease
                    
Install as a Cake Tool

AppSettings Nuget Nuget License: MIT

Automatically generates the registration code for IOptions and can optionally validate them on startup using fluent validation.

How to use it?

  1. Install the NuGet package into your project.
Install-Package IeuanWalker.AppSettings
  1. Inherit the IAppSettings interface onto the class that models your IOptions
public class ConfirmationEmailSettings : IAppSettings
{
	public required string Subject { get; set; }
}
  1. Register the app settings

    Once IAppSettings has 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 IServiceCollection is created for all project types by default

    builder.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.cs

    builder.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.cs

    builder.AddAppSettingsFromMauiProject();
    

Section name

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 setting the SectionName property

public class ConfirmationEmailSettings : IAppSettings
{
    public static string? SectionName => "ConfirmationEmail";
    public required string Subject { get; set; }
}
public class ConfirmationEmailSettings : IAppSettings
{
    public static string? SectionName => "NestedConfiguration:ConfirmationEmail";
    public required string Subject { get; set; }
}

Validation

To perform validation on startup using Fluent Validation, you just 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);
	}
}

What does the error look like?

If something fails validation as the application starts up, you will get an exception explaining the exact issue - image

Considerations

I do not recommend adding validation to a MAUI project as it can/ will slow startup.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.