The .NET 2.0 and up configuration system is quite powerful and extensible - as long as you don\'t want to change the fact it all comes from XML files in the filesystem.
You can use the same xml parsing .NET uses when reading the web.config and configuration sections with some reflection.
Here is the sample code to do it.
Here is a class that you want represented in xml.
public class TestConfiguration : ConfigurationSection
{
[ConfigurationProperty("optionalProperty", DefaultValue = "defaultValue")]
public string OptionalProperty
{
get { return (string)base["optionalProperty"]; }
set { base["optionalProperty"] = value; }
}
[ConfigurationProperty("requiredProperty", IsRequired = true)]
public string RequiredProperty
{
get { return (string)base["requiredProperty"]; }
set { base["requiredProperty"] = value; }
}
}
Here is how you instantiate this ConfigurationSection using XML from a string (or a database). This was taken from the tests on GitHub that was linked in the blog post above.
[TestMethod]
public void Can_build_configuration_with_default_value_set()
{
var result = _configurationSectionBuilder
.BuildSection<TestConfiguration>("<config requiredProperty=\"required\" optionalProperty=\"setValue\"></config>");
Assert.AreEqual("setValue", result.OptionalProperty);
}
You get all the .NET lift with this approach using the System.Configuration namespace.
There's an article here that talks about doing what you are talking about:
http://www.wrox.com/WileyCDA/Section/Redirecting-Configuration-with-a-Custom-Provider.id-291932.html
In summary what they do is create a derived version of ProtectedConfigurationProvider, which is typically used to encrypt .config files. In the Decrypt method, instead of decrypting the configuration information, it's retrieved from a database.
You can try Cinchoo framework for your needs.
It supports reading and writing configuration entries to File, Registry, INI, Database etc.
Here is the simple way to define and use the configuration object using Cinchoo framework
namespace HelloWorld
{
#region NameSpaces
using System;
using Cinchoo.Core.Configuration;
#endregion NameSpaces
[ChoConfigurationSection("sample")]
public class SampleConfigSection : ChoConfigurableObject
{
[ChoPropertyInfo("name", DefaultValue="Mark")]
public string Name;
[ChoPropertyInfo("message", DefaultValue="Hello World!")]
public string Message;
}
static void Main(string[] args)
{
SampleConfigSection sampleConfigSection = new SampleConfigSection();
Console.WriteLine(sampleConfigSection.ToString());
}
}
Very first time, when you run the application, Cinchoo framework automatically generates the configuration section as below. Then onwards, you can control them either through configuration source or by code.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="sample" type="Cinchoo.Core.Configuration.ChoNameValueSectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
</configSections>
<sample>
<add key="name" value="Mark" />
<add key="message" value="Hello World!" />
</sample>
</configuration>
Try it!
Try with the "Configuration Service for .NET Applications and WCF Services", is part of the StockTrader 2.0 sample app from MSFT:
StockTrader 2.0 Configuration Service overview
StockTrader 2.0 Configuration Service documentation
Download StockTrader 2.0 for .NET 4.0