Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using Config.Net;
using Config.Net.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
using System.Text.RegularExpressions;
namespace Settings
{
class Factory
{
static ConfigurationBuilder<TInterface> GetConfigurationBuilder<TInterface>(string rootPath)
{
var patterns = new List<string>()
{
//in order
"*.config.local.json",
"*.config.global.json"
};
var allFiles = new List<string>();
foreach(var pattern in patterns)
{
allFiles.AddRange(Directory.EnumerateFiles(rootPath, pattern, SearchOption.AllDirectories));
}
var configurationBuilder = new ConfigurationBuilder<TInterface>();
foreach (var configPath in allFiles.OrderBy(
//Pads integers, for sorting only, at the beginning of the file so that 10 comes after 2 lexicographically
filename => Regex.Replace(filename, "^[0-9]+", match => match.Value.PadLeft(10, '0'))
))
{
configurationBuilder.UseJsonFile(configPath);
}
return configurationBuilder;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.