Skip to content

ndcorder/AppSettingsGen

Repository files navigation

AppSettingsGen

Generate strongly-typed settings classes from appsettings.json at compile time.

No more hand-writing settings classes, registering IOptions<T> bindings, or keeping C# code in sync with JSON config. AppSettingsGen reads your appsettings.json during compilation and generates matching classes and DI registration code automatically.

Features

  • Type inference — Infers string, int, long, double, bool, arrays, and nested objects from JSON values
  • Class generation — Generates a Settings class per JSON section with PascalCase properties
  • DI registration — Generates AddAppSettings(IConfiguration) extension method for IOptions<T> pattern
  • AOT compatible — All generated code is static with no reflection; works with NativeAOT trimming
  • Incremental — Only regenerates when appsettings.json changes (Roslyn incremental source generator)
  • Naming conventions — Converts camelCase, snake_case, and kebab-case JSON keys to PascalCase C# properties

Installation

dotnet add package AppSettingsGen

Quick Start

1. Mark your config file as an additional file

In your .csproj:

<ItemGroup>
  <AdditionalFiles Include="appsettings.json" />
</ItemGroup>

2. Write your config as usual

{
  "Database": {
    "ConnectionString": "Server=localhost;Database=MyDb",
    "MaxRetries": 3
  },
  "Logging": {
    "Level": "Information"
  }
}

3. Use the generated code

The generator produces classes in the AppSettingsGen.Generated namespace:

using AppSettingsGen.Generated;
using Microsoft.Extensions.Options;

// Register all settings in one call
builder.Services.AddAppSettings(builder.Configuration);

// Inject via IOptions<T>
public class MyService
{
    public MyService(IOptions<DatabaseSettings> dbOptions)
    {
        var connectionString = dbOptions.Value.ConnectionString;
        var maxRetries = dbOptions.Value.MaxRetries;
    }
}

Generated Classes

// Auto-generated from appsettings.json
namespace AppSettingsGen.Generated
{
    public partial class DatabaseSettings
    {
        public string? ConnectionString { get; init; }
        public int MaxRetries { get; init; }
    }

    public partial class LoggingSettings
    {
        public string? Level { get; init; }
    }
}

Type Mapping

JSON Value C# Type
"text" string?
42 int
9999999999 long
3.14 double
true / false bool
["a", "b"] List<string>?
[1, 2] List<int>?
{ ... } Nested class
[{ ... }] List<ItemClass>?

Nested Objects

Nested JSON objects become nested C# classes:

{
  "App": {
    "Cache": {
      "Enabled": true,
      "TtlSeconds": 300
    }
  }
}

Generates:

public partial class AppSettings
{
    public Cache? Cache { get; init; }

    public partial class Cache
    {
        public bool Enabled { get; init; }
        public int TtlSeconds { get; init; }
    }
}

Object Arrays

Arrays of objects generate a typed item class:

{
  "Config": {
    "Endpoints": [
      { "Url": "http://api.example.com", "Timeout": 30 }
    ]
  }
}

Generates:

public partial class ConfigSettings
{
    public List<EndpointsItem>? Endpoints { get; init; }

    public partial class EndpointsItem
    {
        public string? Url { get; init; }
        public int Timeout { get; init; }
    }
}

Diagnostics

Code Severity Description
ASG001 Error Invalid JSON in appsettings file
ASG002 Warning No object sections found to generate

Requirements

  • .NET 8.0+ (for the consuming project)
  • C# 12+
  • Roslyn 4.12+

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Write tests for your changes
  4. Run dotnet test and dotnet format
  5. Submit a pull request

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages