GithubHelp home page GithubHelp logo

polarbeardk / miracle.settings Goto Github PK

View Code? Open in Web Editor NEW
11.0 2.0 2.0 3.54 MB

Load your application settings into strong typed objects with two lines of code.

License: MIT License

C# 99.23% PowerShell 0.43% TSQL 0.34%
settings appsettings nested-objects strong-typed dictionaries array list

miracle.settings's Introduction

Miracle.Settings

Load application settings into strong typed objects with two lines of code.

  • No need to write code that load, convert and validate each configuration value.
  • No "magic strings" and no defaults scattered around in source files.
  • Loaded models are guaranteed to be valid.
  • Get detailed error message on load error, describing exactly which setting has failed.
  • Load settings at startup and application fails immediately upon bad configuration (Fail fast).
  • Load complex structores like nested objects, arrays, lists and dictionaries.
  • Use DI to inject strong typed setting models into application at startup.
  • Works with Full .NET and .NET Core.

Table of content

Install

Available as a NuGet package: Miracle.Settings

To install Miracle.Settings, run the following command in the Package Manager Console

PM> Install-Package Miracle.Settings

Usage

A basic example on how to load settings into a POCO object.

<configuration>
  <appSettings>
    <add key="Foo" value="Foo string" />
    <add key="Bar" value="42" />
  </appSettings>
</configuration>
// Setting model that appSettings are loaded into
public class FooBar
{
    public string Foo { get; private set; }
    public int Bar { get; private set; }
}

The POCO (Plain Old CLR Object) that settings are serialized/loaded into.

ISettingsLoader settingsLoader = new SettingsLoader();
// Get settings at "root" level (without a prefix)
var settings = settingsLoader.Create<FooBar>();

This code loads settings of type of type FooBar into settings variable.

Top Tip! Load settings ONCE at startup, and expose settings to application using Dependency Injection.

Nested objects

Nested objects are supported using property separator (configurable using: SettingLoader.PropertySeparator)

<configuration>
  <appSettings>
    <add key="MyPrefix:Foo" value="Foo string" />
    <add key="MyPrefix:Nested:Foo" value="Foo" />
    <add key="MyPrefix:Nested:Bar" value="42" />
  </appSettings>
</configuration>
public class FooBar
{
    public string Foo { get; private set; }
    public Nested Nested { get; private set; }
}

public class Nested
{
    public string Foo { get; private set; }
    public int Bar { get; private set; }
}

// Get settings prefixed by "MyPrefix"
var settings = settingsLoader.Create<FooBar>("MyPrefix");

Arrays, Lists & Dictionaries

Collections (Arrays, Lists & Dictionaries) can be loaded implicitly into setting model properties, or explicitly by calling CreateArray/CreateList/CreateDictionary .

Simple arrays and lists can be loaded from a single string value containing separated values. See Controlling settings with annotations

More advanced scenarios requires a separate value for each collection item. Keys must be unique, so collection keys must be suffixed by something to make them unique.

<configuration>
  <appSettings>
    <add key="MyPrefix.1" value="Foo string" />
    <add key="MyPrefix.2" value="Foo" />
    <add key="MyPrefix.x" value="42" />
  </appSettings>
</configuration>
// Get the same settings as array, list & dictionary.

// With array and list, the "key" path is lost.
string[] settings1 = settingsLoader.CreateArray<string>("MyPrefix");
List<string> settings2 = settingsLoader.CreateList<string>("MyPrefix");

// With dictionary, the part of the key after prefix is used as dictionary key.
// In this case this would produce keys: "1","2","x"
Dictionary<string,string> settings3 = settingsLoader.CreateDictionary<string>("MyPrefix");

Elements in Arrays and Lists are returned in the same order as they are returned by the value provider. The AppSettings value proveider returns values in the same order as they are specified.

Advanced topics

miracle.settings's People

Contributors

polarbeardk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

miracle.settings's Issues

Nullable<MyEnum> fails to load the value

Related to #2 . See the initial investigation done there.

All target frameworks are impacted.

Here is the line I stepped into:

image

.IsEnum is evaluated to false for nullable enums.

Can I load settings from a dictonary?

Hi I would like to load setting from a database, are there a way to load data from a dictionary or list of objects.
I would like to take advantages of the type safe loading you have build

Simple nullables (int? MyEnum?) throws in .NET Core when settingKey: null

Repro changeset in: #1

.NET Core only, because of this piece: ConfigurationValueProvider.cs

public bool TryGetValue(string key, out string value)
		{
			value = _configurationRoot[key];
			return value != null;
		}

For settingKey: null in JSON config _configurationRoot[key]; returns ""

It works in .NET Framework:

image

If I change the setting entry to: settingKey: "" it throws the same error.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.