Ares.Extensions.Configuration.Annotations 1.0.1

dotnet add package Ares.Extensions.Configuration.Annotations --version 1.0.1
                    
NuGet\Install-Package Ares.Extensions.Configuration.Annotations -Version 1.0.1
                    
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="Ares.Extensions.Configuration.Annotations" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ares.Extensions.Configuration.Annotations" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Ares.Extensions.Configuration.Annotations" />
                    
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 Ares.Extensions.Configuration.Annotations --version 1.0.1
                    
#r "nuget: Ares.Extensions.Configuration.Annotations, 1.0.1"
                    
#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.
#addin nuget:?package=Ares.Extensions.Configuration.Annotations&version=1.0.1
                    
Install Ares.Extensions.Configuration.Annotations as a Cake Addin
#tool nuget:?package=Ares.Extensions.Configuration.Annotations&version=1.0.1
                    
Install Ares.Extensions.Configuration.Annotations as a Cake Tool

Binder Options with Microsoft.Extensions.Configuration.Annotations

workflow ci NuGet GitHub license Releases

English | 中文

Microsoft.Extensions.Configuration.Annotations is a library that extends the Microsoft.Extensions.Configuration system by providing attribute-based support, allowing more flexible and structured control over configuration items during the binding process via AOP.

Features

  • Attribute Support: Bind and validate configuration items using attributes.

  • Compatibility with Existing Configuration System: Fully compatible with Microsoft.Extensions.Configuration and can be seamlessly integrated into existing projects.

How to Use

Install the Nuget package:

Install-Package Ares.Extensions.Configuration.Annotations

Or via .NET CLI:

dotnet add package Ares.Extensions.Configuration.Annotations

Configuration

First, configure it in the Program.cs file as follows:

var builder = WebApplication.CreateBuilder(args);

// Add Ares.Extensions.Configuration.Annotations
// Add all `OptionsAttribute` defined in the `Program` assembly to the IServiceCollection
builder.Services.AddAttributeConfigurationOptions(builder.Configuration,true,typeof(Program).Assembly);

How to Define?

Example: Standard Options Class

[Options("app")]
public class MyAppOptions
{
    public int Id { get; set; }
    
    public string Name {get; set; }
    ...
}

Example: Bind Non-Public Properties

[Options(SessionKey = "app", BindNonPublicProperties = true)]
public class AppOptions
{
    private int Id { get; set; }

    public string Name { get;set; }
    ...
}

Example: Throw Exception if Missing Properties

// appsettings.json Configuration
//
//  "App":{
//     "Id":1,
//     "Name":"my app",
//     "Version": "1.0.0"
// }
[Options(SessionKey = "app", BindNonPublicProperties = true,ErrorOnUnknownConfiguration = true)]
public class AppOptions
{
    private int Id { get; set; }

    public string Name { get;set; }
    
    ...
}

Validators

Example: Options Class with Validator

[Validate]
[Options("app")]
public class MyAppOptions
{
    [Range(1,20)]
    public int Id { get; set; }
    
    [Required]
    public string Name {get; set; }
    ...
}

Example: Options Class with Custom Validator

[Validate(typeof(MyAppValidateOptions))]
[Options("app")]
public class MyAppOptions
{
    public int Id { get; set; }
    
    public string Name {get; set; }
    ...
}

public class MyAppValidateOptions : IValidateOptions<MyAppOptions>
{
    public ValidateOptionsResult Validate(string name, MyAppOptions options)
    {
        // To do validate for MyAppOptions
        
        return ValidateOptionsResult.Success;
    }
}

Accessing Options

Example: Accessing in Constructor

public class MyService
{
    private readonly MyAppOptions _myAppOptions;

    public MyService(IOptions<MyAppOptions> myAppOptions)
    {
        _myAppOptions = myAppOptions.Value;
    }

    public void PrintSettings()
    {
      Console.WriteLine(_myAppOptions.Id);
      Console.WriteLine(_myAppOptions.Name);
    }
}

Example: Accessing via IServiceProvider

IServiceCollection services = new ServiceCollection();

IConfigurationBuilder builder = new ConfigurationBuilder();

// Add option data
builder.AddInMemoryCollection(new Dictionary<string, string?>()
    {
        { "app:id", "1" },
        { "app:name", "test app" },
        { "app:version", "1.0.0" },
        { "app:description", "test options bind" },
    });

IConfigurationRoot configurationRoot = builder.Build();

services.AddAttributeConfigurationOptions(configurationRoot,true,typeof(Program).Assembly);

var provider = services.BuildServiceProvider();

// Gets Options with Options Attribute
var options =  provider.GetService<IOptions<MyAppOptions>>();

Console.WriteLine(options.Value.Id);
Console.WriteLine(options.Value.Name);

Contribute

One 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.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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
1.0.1 43 4 days ago

Use Ares.Extensions.Configuration.Annotations attributes binding get Microsoft.Extensions.Configuration options.

           Release notes:
           
           1.0.0: Upgrade Microsoft.NET.Sdk to net8.0.