{"id":26937101,"url":"https://github.com/huhouhua/microsoft.extensions.configuration.annotations","last_synced_at":"2025-06-14T17:37:24.575Z","repository":{"id":284360413,"uuid":"954531279","full_name":"huhouhua/Microsoft.Extensions.Configuration.Annotations","owner":"huhouhua","description":"A configuration provider that enables configuration binding via attributes in Microsoft.Extensions.Configuration.","archived":false,"fork":false,"pushed_at":"2025-04-01T15:51:38.000Z","size":77,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-01T16:53:19.135Z","etag":null,"topics":["aop","attributes","configuration","dotnet-core","library","microsoft-extensions-configuration","options","validate"],"latest_commit_sha":null,"homepage":"https://www.nuget.org/packages/Ares.Extensions.Configuration.Annotations","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/huhouhua.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}},"created_at":"2025-03-25T08:20:41.000Z","updated_at":"2025-04-01T15:51:42.000Z","dependencies_parsed_at":"2025-03-25T14:37:19.529Z","dependency_job_id":null,"html_url":"https://github.com/huhouhua/Microsoft.Extensions.Configuration.Annotations","commit_stats":null,"previous_names":["huhouhua/microsoft.extensions.configuration.annotations"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huhouhua%2FMicrosoft.Extensions.Configuration.Annotations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huhouhua%2FMicrosoft.Extensions.Configuration.Annotations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huhouhua%2FMicrosoft.Extensions.Configuration.Annotations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/huhouhua%2FMicrosoft.Extensions.Configuration.Annotations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/huhouhua","download_url":"https://codeload.github.com/huhouhua/Microsoft.Extensions.Configuration.Annotations/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246819785,"owners_count":20839095,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["aop","attributes","configuration","dotnet-core","library","microsoft-extensions-configuration","options","validate"],"created_at":"2025-04-02T13:14:16.939Z","updated_at":"2025-06-14T17:37:24.562Z","avatar_url":"https://github.com/huhouhua.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Binder Options with Microsoft.Extensions.Configuration.Annotations\n![workflow ci](https://github.com/huhouhua/Microsoft.Extensions.Configuration.Annotations/actions/workflows/dotnet.yml/badge.svg)\n[![NuGet](https://img.shields.io/nuget/v/Ares.Extensions.Configuration.Annotations.svg?style=flat-square)](https://www.nuget.org/Ares.Extensions.Configuration.Annotations)\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/huhouhua/Microsoft.Extensions.Configuration.Annotations/blob/main/LICENSE)\n[![NuGet](https://img.shields.io/nuget/dt/Ares.Extensions.Configuration.Annotations?style=flat\u0026logo=nuget\u0026cacheSeconds=1\u0026label=Downloads)](https://www.nuget.org/packages/Ares.Extensions.Configuration.Annotations)\n\n\u003e English | [简体中文](README_zh.md)\n\n\u003e Microsoft.Extensions.Configuration.Annotations is a library that extends the Microsoft.Extensions.Configuration\n\u003e system by providing attribute-based support, allowing more flexible and structured control over configuration\n\u003e items during the binding process via AOP.\n\n## Features\n- Attribute Support: Automatically bind and verify configuration items through attributes\n- Automatic registration options\n- Compatibility with Existing Configuration System: Fully compatible with Microsoft.Extensions.Configuration \nand can be seamlessly integrated into existing projects\n\n## Requirements\n\nThis library requires .NET 8.0+.\n\n## How to Use\n\nInstall the [Nuget](https://www.nuget.org/packages/Ares.Extensions.Configuration.Annotations) package:\n\n```sh\nInstall-Package Ares.Extensions.Configuration.Annotations\n```\n\nOr via .NET CLI:\n\n```sh\ndotnet add package Ares.Extensions.Configuration.Annotations\n```\n\n### Configuration\nFirst, configure it in the Program.cs file as follows:\n\n```c#\nvar builder = WebApplication.CreateBuilder(args);\n\n// Add Ares.Extensions.Configuration.Annotations\n// Add all `OptionsAttribute` defined in the `Program` assembly to the IServiceCollection\nbuilder.Services.AddAttributeConfigurationOptions(builder.Configuration,true,typeof(Program).Assembly);\n```\n\n### How to Define?\n\nThe [examples](examples/)  directory contains a couple of clear examples\n\nExample: Standard Options Class\n```c#\n[Options(\"app\")]\npublic class MyAppOptions\n{\n    public int Id { get; set; }\n    \n    public string Name {get; set; }\n    ...\n}\n```\n\nExample: Bind Non-Public Properties\n```c#\n[Options(SessionKey = \"app\", BindNonPublic = true)]\npublic class AppOptions\n{\n    private int Id { get; set; }\n\n    public string Name { get;set; }\n    ...\n}\n```\nExample: Throw Exception if Missing Properties\n```c#\n// appsettings.json Configuration\n//\n//  \"App\":{\n//     \"Id\":1,\n//     \"Name\":\"my app\",\n//     \"Version\": \"1.0.0\"\n// }\n[Options(SessionKey = \"app\", BindNonPublic = true,ThrowOnUnknownConfig = true)]\npublic class AppOptions\n{\n    private int Id { get; set; }\n\n    public string Name { get;set; }\n    \n    ...\n}\n```\n### Validators\n\nExample: Options Class with Validator\n```c#\n[Validate]\n[Options(\"app\")]\npublic class MyAppOptions\n{\n    [Range(1,20)]\n    public int Id { get; set; }\n    \n    [Required]\n    public string Name {get; set; }\n    ...\n}\n```\n\nExample: Options Class with Custom Validator\n```c#\n[Validate(typeof(MyAppValidateOptions))]\n[Options(\"app\")]\npublic class MyAppOptions\n{\n    public int Id { get; set; }\n    \n    public string Name {get; set; }\n    ...\n}\n\npublic class MyAppValidateOptions : IValidateOptions\u003cMyAppOptions\u003e\n{\n    public ValidateOptionsResult Validate(string name, MyAppOptions options)\n    {\n        // To do validate for MyAppOptions\n        \n        return ValidateOptionsResult.Success;\n    }\n}\n```\n### Accessing Options\n\nExample: Accessing in Constructor\n```c#\npublic class MyService\n{\n    private readonly MyAppOptions _myAppOptions;\n\n    public MyService(IOptions\u003cMyAppOptions\u003e myAppOptions)\n    {\n        _myAppOptions = myAppOptions.Value;\n    }\n\n    public void PrintSettings()\n    {\n      Console.WriteLine(_myAppOptions.Id);\n      Console.WriteLine(_myAppOptions.Name);\n    }\n}\n```\n\nExample: Accessing via IServiceProvider\n```c#\nIServiceCollection services = new ServiceCollection();\n\nIConfigurationBuilder builder = new ConfigurationBuilder();\n\n// Add option data\nbuilder.AddInMemoryCollection(new Dictionary\u003cstring, string?\u003e()\n    {\n        { \"app:id\", \"1\" },\n        { \"app:name\", \"test app\" },\n        { \"app:version\", \"1.0.0\" },\n        { \"app:description\", \"test options bind\" },\n    });\n\nIConfigurationRoot configurationRoot = builder.Build();\n\nservices.AddAttributeConfigurationOptions(configurationRoot,true,typeof(Program).Assembly);\n\nvar provider = services.BuildServiceProvider();\n\n// Gets Options with Options Attribute\nvar options =  provider.GetService\u003cIOptions\u003cMyAppOptions\u003e\u003e();\n\nConsole.WriteLine(options.Value.Id);\nConsole.WriteLine(options.Value.Name);\n\n```\n\n## Contribute\n\nOne of the easiest ways to contribute is to participate in discussions and discuss issues. You can also contribute by submitting pull requests with code changes.\n\n### License\n\n[MIT](https://github.com/huhouhua/Microsoft.Extensions.Configuration.Annotations/blob/main/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhuhouhua%2Fmicrosoft.extensions.configuration.annotations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhuhouhua%2Fmicrosoft.extensions.configuration.annotations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhuhouhua%2Fmicrosoft.extensions.configuration.annotations/lists"}