Hexalith.Commons.Configurations 1.65.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Hexalith.Commons.Configurations --version 1.65.0
                    
NuGet\Install-Package Hexalith.Commons.Configurations -Version 1.65.0
                    
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="Hexalith.Commons.Configurations" Version="1.65.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hexalith.Commons.Configurations" Version="1.65.0" />
                    
Directory.Packages.props
<PackageReference Include="Hexalith.Commons.Configurations" />
                    
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 Hexalith.Commons.Configurations --version 1.65.0
                    
#r "nuget: Hexalith.Commons.Configurations, 1.65.0"
                    
#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.
#:package Hexalith.Commons.Configurations@1.65.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Hexalith.Commons.Configurations&version=1.65.0
                    
Install as a Cake Addin
#tool nuget:?package=Hexalith.Commons.Configurations&version=1.65.0
                    
Install as a Cake Tool

Hexalith.Commons.Configurations

Overview

Hexalith.Commons.Configurations is a .NET library that provides utilities for handling configuration settings in .NET applications. It simplifies the process of retrieving, validating, and managing configuration settings using Microsoft's configuration and options patterns.

Features

  • Interface-based configuration settings with ISettings
  • Helper methods for retrieving settings from configuration
  • FluentValidation integration for validating configuration options
  • Exception handling for configuration errors
  • Support for Microsoft's Options pattern

Installation

Package Manager Console

Install-Package Hexalith.Commons.Configurations

.NET CLI

dotnet add package Hexalith.Commons.Configurations

Usage

Defining Settings

using Hexalith.Commons.Configurations;

public class MyAppSettings : ISettings
{
    public string ConnectionString { get; set; } = string.Empty;
    public int Timeout { get; set; } = 30;
    
    public static string ConfigurationName() => "MyApp";
}

Retrieving Settings

using Hexalith.Commons.Configurations;
using Microsoft.Extensions.Configuration;

// Get configuration from appsettings.json
IConfiguration configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .Build();

// Get settings using the helper method
MyAppSettings settings = configuration.GetSettings<MyAppSettings>();

// Use the settings
Console.WriteLine($"Connection String: {settings.ConnectionString}");
Console.WriteLine($"Timeout: {settings.Timeout}");

Validating Settings with FluentValidation

using FluentValidation;
using Hexalith.Commons.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

// Create a validator for your settings
public class MyAppSettingsValidator : AbstractValidator<MyAppSettings>
{
    public MyAppSettingsValidator()
    {
        RuleFor(x => x.ConnectionString).NotEmpty();
        RuleFor(x => x.Timeout).GreaterThan(0);
    }
}

// Register services
var services = new ServiceCollection();

// Register configuration
services.AddSingleton<IConfiguration>(configuration);

// Register validator
services.AddSingleton<IValidator<MyAppSettings>, MyAppSettingsValidator>();

// Register settings with validation
services.AddOptions<MyAppSettings>()
    .Bind(configuration.GetSection(MyAppSettings.ConfigurationName()))
    .ValidateFluentValidation()
    .ValidateOnStart();

// Build service provider
var serviceProvider = services.BuildServiceProvider();

// Get validated options
var options = serviceProvider.GetRequiredService<IOptions<MyAppSettings>>().Value;

API Reference

ISettings Interface

public interface ISettings
{
    static abstract string ConfigurationName();
}

The ISettings interface defines a contract for configuration settings classes. It requires implementing a static ConfigurationName() method that returns the name of the configuration section.

SettingsHelper Class

public static class SettingsHelper
{
    public static TSettings GetSettings<TSettings>(this IConfiguration configuration)
        where TSettings : class, ISettings, new();
}

The SettingsHelper class provides extension methods for retrieving settings from configuration.

FluentValidateOptions Class

public class FluentValidateOptions<TOptions> : IValidateOptions<TOptions>
    where TOptions : class
{
    public FluentValidateOptions(string? name, IServiceProvider provider);
    public ValidateOptionsResult Validate(string? name, TOptions options);
}

The FluentValidateOptions class implements IValidateOptions<TOptions> to provide FluentValidation integration for the Options pattern.

Dependencies

  • FluentValidation.DependencyInjectionExtensions
  • Microsoft.Extensions.Configuration.Binder
  • Microsoft.Extensions.Logging.Abstractions
  • Microsoft.Extensions.Options
  • Microsoft.Extensions.Options.ConfigurationExtensions
  • Microsoft.Extensions.Options.DataAnnotations

License

This project is licensed under the MIT License - see the LICENSE file in the project root for details.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on Hexalith.Commons.Configurations:

Package Downloads
Hexalith.Security.Application

Hexalith is a set of libraries to build an application with micro-service architecture.

Hexalith.IdentityStores.Abstractions

Hexalith is a set of libraries to build an application with micro-service architecture.

Hexalith.DaprIdentityStore.Abstractions

Hexalith is a set of libraries to build a micro-service architecture.

Hexalith.KeyValueStorages.Abstractions

Hexalith KeyValueStorages utilities and helpers

Hexalith.KeyValueStorages.Files

Hexalith KeyValueStorages utilities and helpers

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.6 112 8/10/2025
2.1.5 222 8/5/2025
2.1.4 591 7/24/2025
2.1.3 371 7/9/2025
2.1.2 478 7/6/2025
2.1.1 330 7/6/2025
2.1.0 142 7/6/2025
2.0.6 140 7/6/2025
2.0.5 387 7/4/2025
2.0.4 142 7/4/2025
2.0.3 318 6/29/2025
2.0.2 1,064 6/24/2025
2.0.1 967 6/13/2025
2.0.0 263 6/13/2025
1.66.5 867 4/11/2025
1.66.4 196 4/10/2025
1.66.3 310 4/9/2025
1.66.2 179 4/8/2025
1.66.1 149 4/8/2025
1.66.0 146 4/7/2025
1.65.1 147 4/6/2025
1.65.0 153 4/6/2025
1.64.0 377 4/2/2025
1.0.0 161 4/2/2025