SparkyTools: XmlConfig

Brian Schroer
Mar 7, 2018 · 2 min read
photo of jigsaw puzzle by Chad Valley from Birmingham Museum and Art Gallery

ConfigurationSectionDeserializer / ConfigurationSectionListDeserializer

<configuration>
<configSections>
<section name="Foo" type="SparkyTools.XmlConfig.ConfigurationSectionDeserializer, SparkyTools.XmlConfig" /> <section name="FooList" type="SparkyTools.XmlConfig.ConfigurationSectionListDeserializer, SparkyTools.XmlConfig" />
</configSections>
<Foo type="FooNamespace.Foo, FooAssemblyName">
<Bar>bar</Bar>
<Baz>123.45</Baz>
</Foo>
<FooList type="FooNamespace.Bar, FooAssemblyName">
<Foo>
<Bar>bar1</Bar>
<Baz>111.11</Baz>
</Foo>
<Foo>
<Bar>bar2</Bar>
<Baz>222.22</Baz>
</Foo>
</FooList>
Foo foo = 
ConfigurationSectionDeserializer.Load<Foo>("Foo");
IList<Foo> fooList =
ConfigurationSectionListDeserializer.Load<Foo>("FooList");

DependencyProvider methods

using SparkyTools.DependencyProvider;

public class Foo
{
public Foo(
DependencyProvider<Bar> barProvider,
DependencyProvider<IList<Baz>> bazProvider)
{
}
}
using SparkyTools.XmlConfig;
. . .
var foo = new Foo(
ConfigurationSectionDeserializer.DependencyProvider<Bar>("Bar"),
ConfigurationSectionListDeserializer.DependencyProvider<Baz>
("BazList"));
using SparkyTools.DependencyProvider;

public class Qux
{
private readonly Func<string, string> _getAppSetting;

public Qux(
DependencyProvider<Func<string, string> appSettingsProvider)
{
_getAppSetting = appSettingsProvider.GetValue();
}

public void MethodUsingAppSettings()
{
string valueFromAppSettings = _getAppSetting("key);
}
}
using SparkyTools.XmlConfig;
...
var qux = new Qux(AppSettings.DependencyProvider());

Brian Schroer

Written by

I heart writing code.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade