Tired of Writing POCOs for appsettings.json? Automate It with a Source Generator
Amirhossein Beheshti

Amirhossein Beheshti @amirhossein_beheshti

About: .NET Backend Dev | Clean code & microservices fan | Love C#, gRPC, Docker, Azure | Open to remote & relocation | Building globally from Iran 🌍

Joined:
Jul 29, 2025

Tired of Writing POCOs for appsettings.json? Automate It with a Source Generator

Publish Date: Jul 29
1 0

If you’ve been building .NET applications for a while, you know the pain of managing configuration files. You tweak appsettings.json, then rush to update your POCOs (Plain Old CLR Objects) to reflect those changes. Forget one property or mistype a key, and boom—runtime errors that sneak past the compiler.

What if I told you there's a smarter way?

Meet SetSharp: a source generator that writes your configuration classes automatically, directly from your appsettings.json blueprint. It’s fast, clean, and saves you from repetitive boilerplate. And yes, it still plays perfectly with .NET’s built-in IOptions<T> pattern.

Why Should You Care?

Because you’ve got better things to do than syncing C# classes with JSON files.

Here’s what SetSharp gives you:

  • 🔍 Zero "magic strings" — the compiler validates everything.
  • 🧼 No more manual POCO updates — classes are auto-generated.
  • 🧩 Built-in DI integration — extension methods are generated to bind everything effortlessly.
  • ⚙️ Customizable — want to skip DI support? You can.

SetSharp is like having a helpful robot that watches your appsettings.json and says, “Hey, I made the matching classes for you. You’re welcome.”

Installing SetSharp

Add the NuGet package to your project:

dotnet add package SetSharp
Enter fullscreen mode Exit fullscreen mode

Or use the NuGet Package Manager in Visual Studio.

Tell the Compiler Where Your Config Lives

You’ll need to mark appsettings.json as an additional file so the generator can do its thing. Add this to your .csproj:

<ItemGroup>
  <AdditionalFiles Include="appsettings.json" />
</ItemGroup>
Enter fullscreen mode Exit fullscreen mode

⚠️ Important: This is just a blueprint. At runtime, values can still come from environment variables, user secrets, or whatever providers you’ve registered.

Build It — And Watch the Magic

Just run:

dotnet build
Enter fullscreen mode Exit fullscreen mode

SetSharp kicks in and generates the C# classes behind the scenes.

Here’s an Example

Say your appsettings.json looks like this:

{
  "MyService": {
    "ApiKey": "supersecret",
    "RetryCount": 3
  }
}
Enter fullscreen mode Exit fullscreen mode

SetSharp generates:

public class MyServiceOptions
{
    public const string SectionName = "MyService";
    public string ApiKey { get; set; }
    public int RetryCount { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

And the DI extension:

builder.Services.AddMyServiceOptions(builder.Configuration);
Enter fullscreen mode Exit fullscreen mode

Now you can inject it into your services:

public class MyService
{
    private readonly MyServiceOptions _options;

    public MyService(IOptions<MyServiceOptions> options)
    {
        _options = options.Value;
    }

    public void Run()
    {
        Console.WriteLine(_options.ApiKey);
    }
}
Enter fullscreen mode Exit fullscreen mode

Optional: Skip IOptions Generation

Don’t need auto-DI methods? Just add this to your config:

"SetSharp": {
  "OptionPatternGenerationEnabled": false
}
Enter fullscreen mode Exit fullscreen mode

TL;DR: Stop Writing POCOs by Hand

SetSharp eliminates a boring, error-prone step in your .NET development workflow. It gives you:

  • ✅ Type-safe config bindings
  • ✅ Auto-generated DI helpers
  • ✅ Cleaner code with fewer runtime surprises

It’s open-source, lightweight, and fits naturally into your existing stack.

👉 Check it out on GitHub

📦 Grab it on NuGet

Have feedback or ideas? I’d love to hear from you!


💬 Drop a comment if you try SetSharp — or if you're curious about source generators in .NET. Let's keep the boilerplate out of our lives, one file at a time.

Comments 0 total

    Add comment