{"id":51727545,"url":"https://github.com/Atulin/ConfigBinder","last_synced_at":"2026-08-06T04:00:52.628Z","repository":{"id":368530364,"uuid":"1284494415","full_name":"Atulin/ConfigBinder","owner":"Atulin","description":"Zero-reflection configuration binding library for .NET,  powered by Roslyn Source Generators. It allows you to bind strongly typed configuration classes directly from `IConfiguration`  at compile time, eliminating the startup overhead and reflection cost","archived":false,"fork":false,"pushed_at":"2026-07-03T10:32:13.000Z","size":72,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-07-26T09:33:28.675Z","etag":null,"topics":["config","csharp","dotnet","ioptions","source-generators"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Atulin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-06-29T23:28:53.000Z","updated_at":"2026-07-23T23:51:47.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/Atulin/ConfigBinder","commit_stats":null,"previous_names":["atulin/configbinder"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Atulin/ConfigBinder","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Atulin%2FConfigBinder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Atulin%2FConfigBinder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Atulin%2FConfigBinder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Atulin%2FConfigBinder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Atulin","download_url":"https://codeload.github.com/Atulin/ConfigBinder/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Atulin%2FConfigBinder/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":36327349,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-07-20T02:08:10.276Z","status":"online","status_checked_at":"2026-08-06T02:00:07.852Z","response_time":54,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["config","csharp","dotnet","ioptions","source-generators"],"created_at":"2026-07-17T22:00:25.998Z","updated_at":"2026-08-06T04:00:52.621Z","avatar_url":"https://github.com/Atulin.png","language":"C#","funding_links":[],"categories":["Source Generators"],"sub_categories":["Statically typed resources / configurations"],"readme":"# ConfigBinder\n\n[![NuGet](https://img.shields.io/nuget/v/Atulin.ConfigBinder.svg)](https://www.nuget.org/packages/Atulin.ConfigBinder)\n[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)\n\n**ConfigBinder** is a zero-reflection, highly performant configuration binding library for .NET, \npowered by Roslyn Source Generators. It allows you to bind strongly typed configuration classes directly from `IConfiguration` \nat compile time, eliminating the startup overhead and reflection cost associated with the built-in `Microsoft.Extensions.Configuration` binder.\n\n## Features\n\n- **Zero Reflection**: Uses Roslyn Source Generators to generate binding code at compile time.\n- **AOT / Trimming Friendly**: Completely avoids reflection, making it perfect for Native AOT and heavily trimmed applications.\n- **Direct Access or IOptions**: Choose between injecting your raw configuration objects directly (`AddSingleton\u003cMyConfig\u003e`) or using the standard `IOptions\u003cMyConfig\u003e` pattern.\n- **Immediate.Validations Integration**: Out-of-the-box support for validating configurations on startup using `Immediate.Validations`.\n- **Custom Converters**: Define property-level or global custom type converters for complex types.\n- **Built-in Parsing**: Automatically parses all standard primitives, enums, and any type implementing `IParsable\u003cT\u003e`.\n\n## Installation\n\nAdd the `ConfigBinder` package to your project. Since this is a source generator, you may want to reference it accordingly \n(though it provides attributes as well).\n\n```xml\n\u003cPackageReference Include=\"ConfigBinder\" Version=\"x.y.z\" /\u003e\n```\n\n## Quick Start\n\n### 1. Define your Configuration Model\n\nDecorate your configuration class, struct, or record with `[ConfigSection(\"SectionName\")]`. By default, \nproperties must have an accessible setter (or `init`).\n\n```csharp\nusing ConfigBinder.Attributes;\n\n[ConfigSection(\"MyConfig\")]\npublic sealed class MyConfig\n{\n    public required string Name { get; init; }\n    public required int MaxRetries { get; init; }\n}\n```\n\n### 2. Register Generated Configurations\n\nIn your `Program.cs` or startup code, call the generated extension method `RegisterGeneratedConfigs` on your `IServiceCollection`.\n\n```csharp\nvar builder = WebApplication.CreateBuilder(args);\n\n// This single call registers all types decorated with [ConfigSection]\nbuilder.Services.RegisterGeneratedConfigs(builder.Configuration);\n\nvar app = builder.Build();\n\n// You can now resolve your config!\nvar config = app.Services.GetRequiredService\u003cIOptions\u003cMyConfig\u003e\u003e();\n```\n\n## Registration Modes\n\nConfigBinder supports two modes for registering your configuration objects:\n\n- `RegistrationMode.DirectAccess`: Registers the object directly as a singleton (`services.AddSingleton\u003cT\u003e`).\n- `RegistrationMode.Options` (Default): Registers the object using the standard Options pattern (`services.AddOptions\u003cT\u003e()`).\n\nYou can override the mode on a per-class basis:\n\n```csharp\n[ConfigSection(\"MyConfig\", Mode = ConfigRegistrationMode.DirectAccess)]\npublic class MyOptionsConfig { /* ... */ }\n```\n\nOr set an assembly-wide default:\n\n```csharp\n[assembly: ConfigSectionDefaults(Mode = ConfigRegistrationMode.DirectAccess)]\n```\n\n## Custom Converters\n\nIf you need to parse complex types that don't implement `IParsable\u003cT\u003e`, you can write custom converters. \nA converter is simply a type with a static method (default name `Convert`) taking a `string` and a `string` (property name) \nand returning the parsed type.\n\n### Property-Level Converter\n\n```csharp\n[ConfigSection(\"Feature\")]\npublic class FeatureConfig\n{\n    [ConfigConverter(typeof(MyCustomParser), \"ParseMyType\")]\n    public MyType SomeProperty { get; set; }\n}\n```\n\n### Global Converter\n\nRegister a converter for a specific type across your entire assembly:\n\n```csharp\n[assembly: ConfigTypeConverter(typeof(MyType), typeof(MyCustomParser))]\n```\n\n## Validation\n\nIf your project references [Immediate.Validations](https://github.com/ImmediatePlatform/Immediate.Validations) \nand your configuration type implements `IValidationTarget\u003cT\u003e`, `ConfigBinder` will automatically wire up `IValidateOptions\u003cT\u003e` \nwhen using `RegistrationMode.Options`. This ensures your configuration is strictly validated on application startup.\n\n\u003e [!WARNING]\n\u003e Validation works only in `RegistrationMode.Options` mode.\n\n```csharp\nusing Immediate.Validations.Shared;\n\n[Validate]\n[ConfigSection(\"ValidatedConfig\")]\npublic sealed partial class ValidatedConfig : IValidationTarget\u003cValidatedConfig\u003e\n{\n    public required string Host { get; init; }\n    public required int Port { get; init; }\n}\n```\n\n## Example\n\nA config model like\n\n```csharp\n[Validate]\n[ConfigSection(\"Validated\")]\ninternal sealed partial class ValidatedConfig : IValidationTarget\u003cValidatedConfig\u003e\n{\n\t[MinLength(10)]\n\tpublic required string Name { get; init; }\n\tpublic required float Weight { get; init; }\n\tpublic required DateTime BuildDate { get; init; }\n}\n```\nwill generate the following binding code:\n\n```csharp\ninternal static class ValidatedConfigConfigBinder\n{\n\tpublic static ValidatedConfig Bind(IConfiguration configuration)\n\t{\n\t\tvar section = configuration.GetSection(\"Validated\");\n\n\t\tvar instance = new ValidatedConfig {\n\t\t\tName = ValidateString(section[\"Name\"], \"Name\"),\n\t\t\tWeight = ParseFloat(section[\"Weight\"], \"Weight\"),\n\t\t\tBuildDate = ParseIParsable\u003cDateTime\u003e(section[\"BuildDate\"], \"BuildDate\"),\n\t\t};\n\n\t\treturn instance;\n\t}\n\n\tprivate static string ValidateString(string? value, string propertyName)\n\t{\n\t\tif (string.IsNullOrEmpty(value))\n\t    {\n\t\t\tthrow Required(propertyName);\n\t    }\n\t\treturn value;\n\t}\n    \n\tprivate static T ParseIParsable\u003cT\u003e(string? value, string propertyName) where T : IParsable\u003cT\u003e\n\t{\n\t\tif (string.IsNullOrEmpty(value))\n\t    {\n\t        throw Required(propertyName);\n\t    }\n\t    if (T.TryParse(value, CultureInfo.InvariantCulture, out var t))\n\t    {\n\t     \treturn t;   \n\t    }\n\t    throw BadValue(propertyName, value, typeof(T).Name);\n\t}\n    \n\tprivate static float ParseFloat(string? value, string propertyName)\n\t{\n\t    if (string.IsNullOrEmpty(value))\n\t    {\n\t        throw Required(propertyName);\n\t    }\n\t    if (float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var n))\n\t    {\n\t     \treturn n;   \n\t    }\n\t    throw BadValue(propertyName, value, \"float\");\n\t}\n    \n\tprivate static InvalidOperationException Required(string key) =\u003e\n\t\tnew($\"Required configuration key '{key}' is missing or empty\");\n\t      \n\tprivate static InvalidOperationException BadValue(string key, string? value, string type) =\u003e\n\t\tnew($\"Configuration key '{key}' value '{value}' cannot be parsed as '{type}'\");\n}\n```\nand the following extension method:\n\n```csharp\npublic static class GeneratedConfigRegistration\n{\n\tpublic static IServiceCollection RegisterGeneratedConfigs(\n\t\tthis IServiceCollection services,\n\t\tIConfiguration configuration)\n\t{\n\t\tservices.AddSingleton\u003cIOptionsFactory\u003cValidatedConfig\u003e\u003e(sp =\u003e \n\t\t\tnew ConfigBinderOptionsFactory\u003cValidatedConfig\u003e(\n\t\t\t\tsp.GetRequiredService\u003cIEnumerable\u003cIConfigureOptions\u003cValidatedConfig\u003e\u003e\u003e(),\n\t\t\t\tsp.GetRequiredService\u003cIEnumerable\u003cIPostConfigureOptions\u003cValidatedConfig\u003e\u003e\u003e(),\n\t\t\t\tsp.GetRequiredService\u003cIEnumerable\u003cIValidateOptions\u003cValidatedConfig\u003e\u003e\u003e(),\n\t\t\t\t_ =\u003e ValidatedConfigConfigBinder.Bind(configuration)));\n\t\tservices.AddSingleton\u003cIValidateOptions\u003cValidatedConfig\u003e, ImmediateValidationOptionsValidator\u003cValidatedConfig\u003e\u003e();\n\t\tservices.AddOptions\u003cValidatedConfig\u003e().ValidateOnStart();\n\n\t\treturn services;\n\t}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAtulin%2FConfigBinder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAtulin%2FConfigBinder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAtulin%2FConfigBinder/lists"}