Skip to content

A lightweight, source-generated configuration binder for .NET — just tag your class with [ConfigSection] and go.

License

Notifications You must be signed in to change notification settings

HBartosch/Routya.ConfigKit

Repository files navigation

⚙️ Routya.ConfigKit.Generator

🔧 A lightweight, source-generated configuration binder for .NET — just tag your class with [ConfigSection] and go.

CI CI NuGet NuGet


✨ Features

  • ✅ Compile-time generation of config binding and registration code
  • ✅ Supports both IOptions<T> and AddSingleton<T> modes
  • ✅ Full support for System.ComponentModel.Annotations
  • ✅ No reflection at runtime
  • ✅ Drop-in integration with appsettings.json
  • ✅ Currently only supports {get; set;} and {get; init;}.

🚀 Getting Started

🛠 Binding Modes

Mode Behavior
Singleton Registers the instance via AddSingleton<T>
IOptions Uses services.Configure<T>()
Both Adds both for flexibility

1. Install package

dotnet add package Routya.ConfigKit.Generator

2. Create your config class

ConfigSection("MyService", ConfigBindingMode.IOptions)

The 'MyService' in ConfigSection is the section name within your configuration (eg. appsettings.json, Azure App Configuration)

{
  "MyService": {
    "RetryCount": 3,
    "UseCaching": false
  }
}

ConfigBindingMode.IOptions

using System.ComponentModel.DataAnnotations;
using Routya.ConfigKit;

[ConfigSection("MyService", mode: ConfigBindingMode.IOptions)]
public partial class MyServiceOptions
{
    [Required]
    public int RetryCount { get; init; }

    public bool UseCaching { get; init; } = true;
}

Generates

public static IServiceCollection AddMyServiceOptions(this IServiceCollection services, IConfiguration configuration)
{
	services.AddOptions<MyServiceOptions>()
			.Bind(configuration.GetSection("MyService"))
			.ValidateDataAnnotations()
			.ValidateOnStart();

	return services;
}

ConfigBindingMode.Singleton

using System.ComponentModel.DataAnnotations;
using Routya.ConfigKit;

[ConfigSection("MyService", ConfigBindingMode.Singleton)]
public partial class MyServiceOptions
{
    [Required]
    public int RetryCount { get; init; }

    public bool UseCaching { get; init; } = true;
}

Generates

 public static IServiceCollection AddMyServiceOptions(this IServiceCollection services, IConfiguration configuration)
 {
     var options = new MyServiceOptions()
     {
         RetryCount = configuration.GetValue<int>("MyService:RetryCount"),
         UseCaching = configuration.GetValue<bool>("MyService:UseCaching"),
     };

     var validationContext = new ValidationContext(options);
     Validator.ValidateObject(options, validationContext, validateAllProperties: true);

     services.AddSingleton(options);

     return services;
 }

3. Register the generated method in your startup

builder.Services.AddMyServiceOptions(builder.Configuration);

📅 Roadmap

  • Add support for complex/nested config objects
  • {get; private set;}

🔍 More from this author

🧰 Routya

A high-performance, minimal-overhead CQRS + MediatR alternative for .NET applications. Supports request/notification dispatching, behavior pipelines, scoped resolution, and performance-optimized dispatchers.

A companion library for consistent API response modeling. Wraps results with success/failure metadata, integrates with ProblemDetails, and streamlines controller return types.

About

A lightweight, source-generated configuration binder for .NET — just tag your class with [ConfigSection] and go.

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

Contributors 2

  •  
  •  

Languages