Conphig v2.0 published
Conphig (Nuget package), latest version: 2.1.1
Targeting .net 6.0
The latest version of Conphig targets .net 6.0, and is compiled using version 10 of C#. The DefaultAttribute
attribute is marked as obsolete, and will be removed in a future version.
How to use it
After installing the Conphig
package, create a simple POCO class, and place your configuration items as properties inside. For each property, decorate it with JsonPropertyName
, CommandLine
, and EnvironmentVariable
attributes to control how that property is loaded from JSON files, command line arguments and environment variables. Like this:
using System.Text.Json.Serialization;
using ATornblad.Conphig;
namespace MyApp
{
public class Setting
{
[JsonPropertyName("title")]
[EnvironmentVariable("TITLE")]
[CommandLine("-t", "--title")]
public string Title { get; set; } = "Default title";
[JsonPropertyName("verbose")]
[EnvironmentVariable("VERBOSE")]
[CommandLine("-v", "--verbose")]
public bool Verbose { get; set; } = false;
[JsonPropertyName("minAge")]
[EnvironmentVariable("MIN_AGE")]
[CommandLine("--min-age")]
public int? MinAge { get; set; }
[EnvironmentVariable("API_TOKEN")]
public string ApiToken { get; set; }
[JsonPropertyName("categories")]
[CommandLine("-c", "--category")]
public string[] Categories { get; set; }
}
}
Then, to load the configuration settings, just add a call to Config.Load<T>
to your application's startup code:
using ATornblad.Conphig;
namespace MyApp
{
public class Program
{
public void Main(string[] args)
{
var config = Config.Load<Setting>();
Console.WriteLine($"Title: {config.Title}");
Console.WriteLine($"Verbose Output: {config.Verbose}");
Console.WriteLine($"Minimum Age: {config.MinAge}");
Console.WriteLine($"Api Token: {config.ApiToken }");
}
}
}
I hope you will find this new version of Conphig useful.
The latest version of the code is always available in the GitHub repository.