Yaml is a convenient format for config storing . There are bindings in most of the popular programming languages, including .NET. The latter is well served by the library called YamlDotNet.

Yaml has a nice feature calls anchors which allows you to reuse portions of config. It works like this:

foo: &anchor
  K1: "One"
  K2: "Two"

bar:
  <<: *anchor
  K2: "I Changed"
  K3: "Three"

Essentially bar will start with everything foo has and then either add or override these values as needed. YamlDotNet supports this feature. Unfortunately, it's documentation isn't very clear on the subject. In particular, it doesn't mention that the feature will not work with the vanilla deserializer. Luckily, all you have to do is to use the bundled MergingParser like this:

using (var file = File.OpenText(configFile))
                {
                    var deserializer = new DeserializerBuilder()
                        .WithNamingConvention(new CamelCaseNamingConvention())
                        .Build();
                    configs = deserializer.Deserialize<IDictionary<string, Config>>(new MergingParser(new Parser(file)));
                }

That's it!

Tags: computers
Categories: None |

0 comments have been posted.

Your email: we will send you a confirmation link to this address to confirm your identity and to prevent robot posting
Get in touch
»...«
Follow updates

Join our social networks and RSS feed to keep up to date with latest news and publications