14. Dezember 2014

ConfigSharp - Scripting Configuration in C#

ConfigSharp (github, nuget)

Config files are C# source files, managed by Visual Studio like any other code file: intellisensed, refactorable, resharpered, syntax checked, compiled, type safe.

Write real code with control structures and classes in config files. Include other local or remote (HTTP) config files.

No more key-value lists of string based app settings in XML. These settings are typed properties of CLR objects, even aggregated types. No more workarounds for complex settings, which do not fit properly in strings.

Devops friendly, because admins can program their config files. Developers can document settings with examples in their own code, which also is the admins's config file.

BTW: this is what everyone (PHP, Python, RoR) does. Configuration in your main language.

Example: 

Program.cs
static void Main(string[] args)
{
    var config = new MyConfig();
    config.Include("ConfigFile.cs");
    ...
    var prop1 = config.SomeProperty;
    // or
    var prop2 = Config.Global.SomeProperty;
    var prop3 = App.Settings.SomeProperty;
    var prop4 = AppSettings.Get("SomeProperty", "default");
}

public class MyConfig : ConfigSharp.Container
{
    public string SomeProperty { get; set; }
    public int OrAsMemberVariable = 41;
    public DateTime PlainOldCLRTypes;
    public NLog.Config ComplexAggregatedTypes;
}
ConfigFile.cs
namespace MyProgram.Configuration // any namespace
{
    class ConfigFile : MyProgram.MyConfig // Derived from your config class
    {
        public void Load()
        {
            SomeProperty = "42";
            Include("OtherConfigFile.cs");
        }
    }
}

More on github and nuget.

_happy_configuring();

Tags: Config, Roslyn, C#, Script, Scripting, CLR, Configuration, File, HTTP, Remote, C# Script, Devops

Rant: XML config files. Static, unreadable XML hell. What where they thinking? Anyone is configuring their app in the native project language. Only poor high level language coders use XML or ini files. Even worse: generating XML config files for different environments with XSLT transformations from master XML config files. Are they out of their mind? Web.config Transformation Syntax for Web Application Project Deployment. <add... xdt:Transform="Replace" xdt:Locator="Condition(@name='oldname' or @providerName='oldprovider')"> --- Condition(), or, @, What? There is already a proven syntax: it is called C#. There is already a good transformation: it is called compiler. This must end now, once and for all.

Keine Kommentare: