ToolBox.ConfigGeneration 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ToolBox.ConfigGeneration --version 1.1.0
                    
NuGet\Install-Package ToolBox.ConfigGeneration -Version 1.1.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="ToolBox.ConfigGeneration" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ToolBox.ConfigGeneration" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="ToolBox.ConfigGeneration" />
                    
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 ToolBox.ConfigGeneration --version 1.1.0
                    
#r "nuget: ToolBox.ConfigGeneration, 1.1.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 ToolBox.ConfigGeneration@1.1.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=ToolBox.ConfigGeneration&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=ToolBox.ConfigGeneration&version=1.1.0
                    
Install as a Cake Tool

ToolBox.ConfigGeneration

ConfigGeneration is a library which helps to streamline the interaction with runtime configurations.

Instead of maintaining an enormous appsettings.json file or registering your IOptions<T> to your service collection again and again, this library takes care of automating this for you.

Introduction

When using ToolBox.ConfigGeneration, you declare your configuration in a Domain Driven Design manner.
Practically, this means that instead of maintaining a single appsettings file for all configurations - you manage each configuration.

For example, say you have a redis configuration:

public class RedisConfiguration {
    public string RedisUri { get; set; }
}

and a postgres db configuration:

public class PostgresConfiguration {
    public string DbUri { get; set; }
}

For each of those you will define a json file with an equivalent name.
Below is the example for the RedisConfiguration.json:

RedisConfiguration

{
    "RedisUri": {
        "development": {
            "eastus": "https://...",
            "westeurope": "https://..."
        },
        "production": {
            "eastus": "https://...",
            "westeurope": "https://..."
        }
    }
}

Note the inner hierarchy defined under the RedisUri property.

You have full control over the hierarchy of your configurations.

See below section for more info.

Hierarchy file

In your executable project, you define a single hierarchy file.

Your hierarchy will be defined by the various deployment environments, regions / locations or any other granularity you might need.

The hierarchy file is a .json file and must have two key-value pairs:

  1. hierarchy (array) - a list of nested hierarchies, by order.
  2. combinations (array) - a list of all combinations that must have their own dedicated settings.

For example, the hierarchy file suitable to fit the above RedisConfiguration.json is:

hierarchy.json

{
    "hierarchy": ["environment", "region"],
    "combinations": [
        {
            "environment": "development",
            "region": "eastus"
        },
        {
            "environment": "development",
            "region": "westeurope"
        },
        {
            "environment": "production",
            "region": "eastus"
        },
        {
            "environment": "production",
            "region": "westeurope"
        },
    ]
}

As you can see, there are 4 combinations defined in the above file.
This means there is a total of 4 distinct environments - each deserving of its own set of configuration values.

Read below section to learn how to generate the configurations

Configuration generation

<PropertyGroup>
        <ConfigGenVersion>1.0.9</ConfigGenVersion>
        <JsonConfigsDirectory>$(ProjectDir)configurations</JsonConfigsDirectory>
        <SettingsOutputDirectory>configurations/generated</SettingsOutputDirectory>
        <HierarchyFilePath>$(ProjectDir)configurations/hierarchy.json</HierarchyFilePath>
        <AssemblyToLoadPath>$(ProjectDir)bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).dll</AssemblyToLoadPath>
    </PropertyGroup>
    
    <Target Name="RunConfigTool" AfterTargets="Build">
        <Message Importance="High" Text="Checking if tool manifest exists..." />
        <Exec Command="dotnet new tool-manifest" ContinueOnError="true" />
        <Message Importance="High" Text="Ensuring toolbox-config-gen is installed..." />
        <Exec Command="dotnet tool install --local ConfigGeneration.Tool --version $(ConfigGenVersion)" ContinueOnError="true" />
        <Message Importance="High" Text="Running toolbox-config-gen to generate configuration..." />
        <Exec Command="
            dotnet tool run toolbox-config-gen --jsonConfigsDirectory $(JsonConfigsDirectory) --settingsOutputDirectory $(ProjectDir)$(SettingsOutputDirectory) --hierarchyFilePath $(HierarchyFilePath) --assemblyToLoadPath $(AssemblyToLoadPath)" />
    </Target>
    <Target Name="CopyGeneratedJson" AfterTargets="RunConfigTool">
        <Message Importance="High" Text="Copying the generated settings files..." />
        <ItemGroup>
            <GeneratedJsonFiles Include="$(ProjectDir)$(SettingsOutputDirectory)/**/*.json" />
        </ItemGroup>

        
        <Copy SourceFiles="@(GeneratedJsonFiles)" DestinationFolder="$(OutputPath)$(SettingsOutputDirectory)" />
    </Target>
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.  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 (1)

Showing the top 1 NuGet packages that depend on ToolBox.ConfigGeneration:

Package Downloads
ConfigGenerationSample

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.5.0 156 1/1/2025
1.4.0 122 12/24/2024
1.3.0 129 12/23/2024
1.2.1 115 12/23/2024
1.2.0 120 12/23/2024
1.1.1 124 12/22/2024
1.1.0 134 12/22/2024
1.0.9 124 12/19/2024
1.0.8 117 12/19/2024
1.0.7 130 12/19/2024
1.0.6 122 12/19/2024
1.0.5 123 12/19/2024
1.0.3 122 12/18/2024
1.0.0 125 12/18/2024

v.1.1.0: Added `AutoRegisterConfigurations` to enable automatic binding of configurations.
       v.1.0.0: Initial release.