NotNot.AppSettings 1.1.1

This package has a SemVer 2.0.0 package version: 1.1.1+26.
dotnet add package NotNot.AppSettings --version 1.1.1                
NuGet\Install-Package NotNot.AppSettings -Version 1.1.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="NotNot.AppSettings" Version="1.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NotNot.AppSettings --version 1.1.1                
#r "nuget: NotNot.AppSettings, 1.1.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.
// Install NotNot.AppSettings as a Cake Addin
#addin nuget:?package=NotNot.AppSettings&version=1.1.1

// Install NotNot.AppSettings as a Cake Tool
#tool nuget:?package=NotNot.AppSettings&version=1.1.1                

NotNot.AppSettings

Automatically create strongly typed C# settings objects from AppSettings.json. Uses Source Generators.

Includes a simple deserialization helper for when you are using Dependency Injection, or not.

Getting Started

  1. Add an appsettings.json file to your project (make sure it's copied to the output).
  2. Install this nuget package NotNot.AppSettings.
  3. Build your project
  4. Use the generated AppSettings class in your code. (See the example section below).

How it works

During your project's build process, NotNot.AppSettings will parse the appsettings*.json in your project's root folder. These files are all merged into a single schema. Using source-generators it then creates a set of csharp classes that matches each node in the json hierarchy.

After building your project, an AppSettings class contains the strongly-typed definitions, and an AppSettingsBinder helper/loader util will be found under the {YourProjectRootNamespace}.AppSettingsGen namespace.

Example

appsettings.json

{
  "Hello": {
	"World": "Hello back at you!"
  }
}

Program.cs

using ExampleApp.AppSettingsGen;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace ExampleApp;
public class Program
{ 
   public static async Task Main(string[] args)
   {
      {
         Console.WriteLine("NON-DI EXAMPLE");
                  
         var appSettings = ExampleApp.AppSettingsGen.AppSettingsBinder.LoadDirect();
         Console.WriteLine(appSettings.Hello.World);         
      }
      {
         Console.WriteLine("DI EXAMPLE");

         HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
         builder.Services.AddSingleton<IAppSettingsBinder, AppSettingsBinder>();
         var app = builder.Build();
         var appSettings = app.Services.GetRequiredService<IAppSettingsBinder>().AppSettings;
         Console.WriteLine(appSettings.Hello.World);
      }
   }
}

See the ./NotNot.AppSettings.Example folder in the repository for a fully buildable version of this example.

Troubleshooting / Tips

How to extend the generated AppSettings class?

You can extend any/all of the generated code by creating a partial class in the same namespace.

Some settings not being loaded (value is NULL). Or: My appSettings.Development.json file is not loaded

Ensure the proper environment variable is set. For example, The appSettings.Development.json file is only loaded when the ASPNETCORE_ENVIRONMENT or DOTNET_ENVIORNMENT environment variable is set to Development.

Intellisense not working for AppSettings class

A strongly-typed AppSettings (and sub-classes) is recreated every time you build your project. This may confuse your IDE and you might need to restart it to get intellisense working again.

Why are some of my nodes typed as object?

Under some circumstances, the type of a node's value in appsettings.json would be ambiguous, so object is used:

  • If the value is null or undefined
  • If the value is a POJO/Array/primitive in one appsettings file, and a different one of those three in another.

Tip: Backup generated code in your git repository

Add this to your .csproj to have the code output to ./Generated and have it be ignored by your project. This way you can check it into source control and have a backup of the generated code in case you need to stop using this package.


<Target Name="DeleteFolder" BeforeTargets="PreBuildEvent">
	<RemoveDir Directories="$(CompilerGeneratedFilesOutputPath)" />
</Target>	
<PropertyGroup>
	<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
	<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
	
	<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**" />
</ItemGroup>

Contribute

  • If you find value from this project, consider sponsoring.

Acknowledgments

License: MPL-2.0

A summary from TldrLegal:

MPL is a copyleft license that is easy to comply with. You must make the source code for any of your changes available under MPL, but you can combine the MPL software with proprietary code, as long as you keep the MPL code in separate files. Version 2.0 is, by default, compatible with LGPL and GPL version 2 or greater. You can distribute binaries under a proprietary license, as long as you make the source available under MPL.

In brief: You can basically use this project however you want, but all changes to it must be open sourced.

Changes

  • 1.0.0 : polish and readme tweaks. Put a fork in it, it's done!
  • 0.12.0 : change appsettings read logic to use "AdditionalFiles" workflow instead of File.IO
  • 0.10.0 : Initial Release.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.1.1 120 2/10/2024
1.0.0 148 12/16/2023
0.12.0 60 12/15/2023
0.10.4 63 12/14/2023
0.10.0 133 12/14/2023
0.9.0-main.1 76 12/14/2023