SourceGenerator.Configuration 0.5.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package SourceGenerator.Configuration --version 0.5.1
                    
NuGet\Install-Package SourceGenerator.Configuration -Version 0.5.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="SourceGenerator.Configuration" Version="0.5.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SourceGenerator.Configuration" Version="0.5.1" />
                    
Directory.Packages.props
<PackageReference Include="SourceGenerator.Configuration">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
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 SourceGenerator.Configuration --version 0.5.1
                    
#r "nuget: SourceGenerator.Configuration, 0.5.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=SourceGenerator.Configuration&version=0.5.1
                    
Install SourceGenerator.Configuration as a Cake Addin
#tool nuget:?package=SourceGenerator.Configuration&version=0.5.1
                    
Install SourceGenerator.Configuration as a Cake Tool

ConfigurationGenerator

A helper Source Generator, to generate a strongly typed configuration from a json file.

It is meant to be used with the Microsoft.Extensions.Configuration library and its IConfiguration interface.

Getting started

Define your class that will hold the configuration:

[SourceGenerator.Configuration.GenerateConfigurationPropertiesAttribute("config-definition.json", nameof(configuration))]
public partial class MyConfiguration(IConfiguration configuration)
{
}

The GenerateConfigurationAccessors attribute will generate the properties for the configuration class based on the json file config-definition.json. Ensure that the class is partial and that it can access the IConfiguration instance. The Sample uses the dependency injection to inject the IConfiguration instance. But you could also MySingelton.Configuration instead of nameof(configuration) to access the static property of MySingleton class.

The config-definition.json file could look like this:

{
  "some-int-value": {
    "type": "int"
  },
  "some-required-string": {
    "type": "string",
    "required": true
  },
  "some-nested-properties": {
    "type": "namespace",
    "description": "Some nested Property",
    "members": {
      "nested-int": {
        "type": "int",
        "default": 42
      }
    }
  }
}

The JSON must have the structure defined in the json schema and is hopfully self-explanatory with this example.

The configuration must be marked in the project file as AdditionalFiles:

<ItemGroup>
  <AdditionalFiles Include="config-definition.json" />
</ItemGroup>

The path in the Project is not important, only the name will be matched. This means also that the file name must be unique in the project.

The generated class will look like this (uglier formated…):

#nullable restore
using System;

namespace MyNamespcae
{
    internal partial class MyConfiguration : MyConfiguration.ISomeNestedProperties
    {
        /// <summary>Some nested Property
        /// </summary>
        public ISomeNestedProperties SomeNestedProperties => (this as ISomeNestedProperties)!;
        /// <summary>Some nested Property
        /// </summary>
        public interface ISomeNestedProperties
        {
            int NestedInt { get; }
        }


        public int? SomeIntValue => config.GetValue<int?>("some-int-value");
        public string SomeRequiredString => config.GetValue<string?>("some-required-string") ?? throw new global::SourceGenerator.Configuration.MissingConfigurationException(":some-required-string");
        int MyConfiguration.ISomeNestedProperties.NestedInt => config.GetValue<int?>("some-nested-properties:nested-int") ?? 42;
    }
}

Todo

  • Add support to check for missing required properties at load time
  • Generation of JSON schema
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

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.0 217 3/17/2025
0.5.1 131 3/17/2025
0.5.0 131 3/17/2025
0.4.11 79 3/14/2025
0.4.10 72 3/14/2025
0.4.9 75 3/14/2025
0.4.8 74 3/14/2025
0.4.7 77 3/14/2025
0.4.6 77 3/14/2025
0.4.5 76 3/14/2025
0.4.4 80 3/14/2025
0.4.3 74 3/14/2025
0.4.2 74 3/14/2025
0.4.1 75 3/14/2025
0.4.0 81 3/14/2025
0.3.0 212 3/4/2025
0.2.0 215 3/4/2025
0.1.0 195 3/3/2025