TomRR.SourceGenerator.SettingsBinder
0.0.4
dotnet add package TomRR.SourceGenerator.SettingsBinder --version 0.0.4
NuGet\Install-Package TomRR.SourceGenerator.SettingsBinder -Version 0.0.4
<PackageReference Include="TomRR.SourceGenerator.SettingsBinder" Version="0.0.4" />
<PackageVersion Include="TomRR.SourceGenerator.SettingsBinder" Version="0.0.4" />
<PackageReference Include="TomRR.SourceGenerator.SettingsBinder" />
paket add TomRR.SourceGenerator.SettingsBinder --version 0.0.4
#r "nuget: TomRR.SourceGenerator.SettingsBinder, 0.0.4"
#:package TomRR.SourceGenerator.SettingsBinder@0.0.4
#addin nuget:?package=TomRR.SourceGenerator.SettingsBinder&version=0.0.4
#tool nuget:?package=TomRR.SourceGenerator.SettingsBinder&version=0.0.4
⚙️ 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 settings via
[Settings]
attribute - ✅ Supports
IOptions<T>
, validation, and DI - ✅ Source-generated
SectionName
and registration logic - ✅ Modular and customizable configuration pipelines
- ✅ Zero reflection, minimal runtime overhead
- ✅ XML Documentation
🚀 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
{
public string? Level { get; init; }
public string? Format { get; init; }
}
3. Configure to appsettings.json
{
"LoggingSettings": {
"Level": "Information",
"Format": "Json"
}
}
4. Wire Up in Program.cs
var builder = WebApplication.CreateBuilder(args);
// ✅ Default configuration sources
builder.Configuration.AddDefaultConfigurationSources<Program>();
// ✅ Registers all [Settings] classes automatically
builder.AddSettingsOptions();
var app = builder.Build();
✅ You're done!
🧩 Advanced Configuration Pipeline
If you prefer fine-grained control over configuration sources:
builder.Configuration
.WithBasePath()
.WithJsonFile()
.WithEnvironmentJsonFile()
.WithSecrets<Program>()
.WithEnvironmentVariables();
You can mix & match based on your project needs.
✅ What Gets Generated?
✅ Interface
public interface ISettings
{
static abstract string SectionName { get; }
}
✅ Partial Class Implementation with SectionName
public sealed partial class LoggingSettings : ISettings
{
public static string SectionName => "LoggingSettings";
}
✅ Default Configuration Sourcing
public static IConfigurationBuilder AddDefaultConfigurationSources<T>(this IConfigurationBuilder builder) where T : class
=> builder
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true)
.AddUserSecrets<T>()
.AddEnvironmentVariables();
✅ Automatic Binding with Validation
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
{
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.
🏆 Performance
SettingsBinder:
- ✅ Zero reflection at runtime (everything is compile-time generated).
- ✅ No ActivatorUtilities, no extra runtime DI overhead.
- ✅ Source generation ensures optimal performance with minimal allocations.
📦 Package Info
Name | Description |
---|---|
Package ID | TomRR.SourceGenerator.SettingsBinder |
License | Apache 2.0 |
Author | Tom-Robert Resing |
Repo | GitHub |
Nuget | Nuget |
📄 License
Licensed under the Apache License 2.0.
Product | Versions 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. |
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.3.0)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
0.0.4:
- Added `AddUserSecrets<T>()` to the default configuration method
- Introduced advanced modular configuration methods for fine-grained setup
- Updated file naming and namespace structure for extensions (breaking change)
- Improved XML documentation comments for all generated extension methods
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