TomRR.SourceGenerator.SettingsBinder 0.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package TomRR.SourceGenerator.SettingsBinder --version 0.0.3
                    
NuGet\Install-Package TomRR.SourceGenerator.SettingsBinder -Version 0.0.3
                    
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="TomRR.SourceGenerator.SettingsBinder" Version="0.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TomRR.SourceGenerator.SettingsBinder" Version="0.0.3" />
                    
Directory.Packages.props
<PackageReference Include="TomRR.SourceGenerator.SettingsBinder" />
                    
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 TomRR.SourceGenerator.SettingsBinder --version 0.0.3
                    
#r "nuget: TomRR.SourceGenerator.SettingsBinder, 0.0.3"
                    
#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 TomRR.SourceGenerator.SettingsBinder@0.0.3
                    
#: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=TomRR.SourceGenerator.SettingsBinder&version=0.0.3
                    
Install as a Cake Addin
#tool nuget:?package=TomRR.SourceGenerator.SettingsBinder&version=0.0.3
                    
Install as a Cake Tool

⚙️ SettingsBinder

A lightweight, zero-boilerplate Roslyn Source Generator that binds [Settings]-annotated classes directly to the .NET Options pattern.


✨ Features

  • ✅ Automatic binding of settings from appsettings.json
  • ✅ Strongly-typed configuration via [Settings] attribute
  • ✅ Supports IOptions<T>, validation, and DI
  • ✅ Source-generated SectionName and registration logic
  • ✅ Zero reflection, minimal runtime overhead

🚀 Quick Start

1. Install NuGet Package

dotnet add package TomRR.SourceGenerator.SettingsBinder

2. Define a Settings Class

using TomRR.SourceGenerator.SettingsBinder;

[Settings]
public sealed partial class LoggingSettings : ISettings
{
    public static string SectionName => "LoggingSettings";

    public string? Level { get; init; }
    public string? Format { get; init; }
}

3. Add to appsettings.json

{
  "LoggingSettings": {
    "Level": "Information",
    "Format": "Json"
  }
}

4. Register with DI in Program.cs

var builder = WebApplication.CreateBuilder(args);

// Add base configuration files
builder.Configuration.AddConfigurationSources();

// Automatically registers all [Settings] classes
builder.AddSettingsOptions();

var app = builder.Build();

✅ What Gets Generated?

✅ Interface

public interface ISettings
{
    static abstract string SectionName { get; }
}

✅ Partial Class Implementation

public sealed partial class LoggingSettings : ISettings
{
    public static string SectionName => "LoggingSettings";
}

✅ Binding Extension

builder.Services
    .AddOptions<LoggingSettings>()
    .Bind(builder.Configuration.GetSection(LoggingSettings.SectionName))
    .ValidateDataAnnotations()
    .ValidateOnStart();

🧩 Customize Section Names

You can explicitly override the section name using the attribute:

[Settings("MyCustomSection")]
public sealed partial class AdvancedSettings : ISettings
{
    public string? Mode { get; init; }
    public bool Enabled { get; init; }
}

This generates:

public sealed partial class AdvancedSettings : ISettings
{
    public static string SectionName => "MyCustomSection";
}
{
  "MyCustomSection": {
    "Mode": "Debug",
    "Enabled": true
  }
}

🧠 Use this when the config section does not match the class name.

➕ Adding More Settings

Just define more [Settings]-annotated classes:

[Settings]
public sealed partial class ApiKeySettings : ISettings
{
    public string Key { get; init; } = default!;
}

All [Settings] classes are automatically picked up and registered.

📦 Package Info

Name Description
Package ID TomRR.SourceGenerator.SettingsBinder
License Apache 2.0
Author Tom-Robert Resing
Repo GitHub

📄 License

Licensed under the Apache License 2.0.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.

Version Downloads Last Updated
0.0.4 161 9/4/2025
0.0.3 148 7/10/2025

0.0.3: bug fix
           - Fixed missing 'this' keyword on extension method AddConfigurationSources

           0.0.1: Initial Testing Version
           - Generates strongly-typed settings binders for [Settings]-marked classes
           - Supports custom section names via attribute constructor
           - Includes extension methods for configuration and DI registration
           
           - Current version only for web api projects