<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Amirhossein Beheshti</title>
    <description>The latest articles on DEV Community by Amirhossein Beheshti (@amirhossein_beheshti).</description>
    <link>https://dev.to/amirhossein_beheshti</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3396188%2Fb4366e03-81a6-4694-91c7-4f2aca433777.jpg</url>
      <title>DEV Community: Amirhossein Beheshti</title>
      <link>https://dev.to/amirhossein_beheshti</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amirhossein_beheshti"/>
    <language>en</language>
    <item>
      <title>Tired of Writing POCOs for appsettings.json? Automate It with a Source Generator</title>
      <dc:creator>Amirhossein Beheshti</dc:creator>
      <pubDate>Tue, 29 Jul 2025 07:47:38 +0000</pubDate>
      <link>https://dev.to/amirhossein_beheshti/tired-of-writing-pocos-for-appsettingsjson-automate-it-with-a-source-generator-2fj2</link>
      <guid>https://dev.to/amirhossein_beheshti/tired-of-writing-pocos-for-appsettingsjson-automate-it-with-a-source-generator-2fj2</guid>
      <description>&lt;p&gt;If you’ve been building .NET applications for a while, you know the &lt;strong&gt;pain of managing configuration files&lt;/strong&gt;. You tweak &lt;code&gt;appsettings.json&lt;/code&gt;, 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.&lt;/p&gt;

&lt;p&gt;What if I told you there's a smarter way?&lt;/p&gt;

&lt;p&gt;Meet &lt;strong&gt;SetSharp&lt;/strong&gt;: a source generator that &lt;strong&gt;writes your configuration classes automatically&lt;/strong&gt;, directly from your &lt;code&gt;appsettings.json&lt;/code&gt; blueprint. It’s fast, clean, and saves you from repetitive boilerplate. And yes, it still plays perfectly with .NET’s built-in &lt;code&gt;IOptions&amp;lt;T&amp;gt;&lt;/code&gt; pattern.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Should You Care?
&lt;/h2&gt;

&lt;p&gt;Because you’ve got better things to do than syncing C# classes with JSON files.&lt;/p&gt;

&lt;p&gt;Here’s what SetSharp gives you:&lt;/p&gt;

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

&lt;p&gt;SetSharp is like having a helpful robot that watches your &lt;code&gt;appsettings.json&lt;/code&gt; and says, &lt;em&gt;“Hey, I made the matching classes for you. You’re welcome.”&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing SetSharp
&lt;/h2&gt;

&lt;p&gt;Add the NuGet package to your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet add package SetSharp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or use the NuGet Package Manager in Visual Studio.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tell the Compiler Where Your Config Lives
&lt;/h2&gt;

&lt;p&gt;You’ll need to mark &lt;code&gt;appsettings.json&lt;/code&gt; as an additional file so the generator can do its thing. Add this to your &lt;code&gt;.csproj&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ItemGroup&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;AdditionalFiles&lt;/span&gt; &lt;span class="na"&gt;Include=&lt;/span&gt;&lt;span class="s"&gt;"appsettings.json"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ItemGroup&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h2&gt;
  
  
  Build It — And Watch the Magic
&lt;/h2&gt;

&lt;p&gt;Just run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;SetSharp kicks in and generates the C# classes behind the scenes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Here’s an Example
&lt;/h2&gt;

&lt;p&gt;Say your &lt;code&gt;appsettings.json&lt;/code&gt; looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"MyService"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"ApiKey"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"supersecret"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"RetryCount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;SetSharp generates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyServiceOptions&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;SectionName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"MyService"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;ApiKey&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;RetryCount&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the DI extension:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AddMyServiceOptions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Configuration&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can inject it into your services:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyService&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;MyServiceOptions&lt;/span&gt; &lt;span class="n"&gt;_options&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MyService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IOptions&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;MyServiceOptions&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_options&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ApiKey&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Optional: Skip IOptions Generation
&lt;/h2&gt;

&lt;p&gt;Don’t need auto-DI methods? Just add this to your config:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"SetSharp"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"OptionPatternGenerationEnabled"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  TL;DR: Stop Writing POCOs by Hand
&lt;/h2&gt;

&lt;p&gt;SetSharp eliminates a boring, error-prone step in your .NET development workflow. It gives you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Type-safe config bindings&lt;/li&gt;
&lt;li&gt;✅ Auto-generated DI helpers&lt;/li&gt;
&lt;li&gt;✅ Cleaner code with fewer runtime surprises&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It’s open-source, lightweight, and fits naturally into your existing stack.&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://github.com/beheshty/SetSharp" rel="noopener noreferrer"&gt;Check it out on GitHub&lt;/a&gt;&lt;br&gt;&lt;br&gt;
📦 &lt;a href="https://www.nuget.org/packages/SetSharp" rel="noopener noreferrer"&gt;Grab it on NuGet&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have feedback or ideas? I’d love to hear from you!&lt;/p&gt;




&lt;p&gt;💬 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.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>config</category>
      <category>devtools</category>
    </item>
  </channel>
</rss>
