-
Notifications
You must be signed in to change notification settings - Fork 0
Examples Basic
#Description
In this section we are going to show the most simple way to read configurations. In the advanced section we will cover more complex scenarios such as specifying extraction strategies or mapping properties to keys where there is a name difference.
#AppSettings
Reading from the AppSettings is already quite easy in .Net however you still need to parse your keys to ensure they exist and to convert them to the appropriate primitive types. ConfigurationAssist helps speed up this process by allowing you to read only parts of your AppSettings into a configuration class and performing the necessary primitive type conversion for you. Below is the most basic scenario for reading AppSettings. Note if you only specify some of the keys, only those keys will be read. This allows you to create settings "groups".
<AppSettings>
<add key="TestValue" value="Value1" />
<add key="DecimalValue" value="1.5" />
<add key="IntegerValue" value="9" />
</AppSettings>Create a simple c# class that has properties names exactly the same as the propertes in AppSettings you want to read (Case Sensitive).
public class MySettings
{
public string TestValue { get; set; }
public decimal DecimalValue { get; set; }
public int IntegerValue { get; set; }
}Finally to read it, simply create a new instance of ConfigurationAssist, then extract your object.
var config = new ConfigurationAssist();
var settings = config.ExtractSettings<MySettings>();A simple section is what I call a section that doesn't belong to a section group. Below I've shown the most basic section configuration possible. There are variances to this. Please see the advanced section in the wiki to see how to do more complex linking of properties, setting defaults and using more complex section types.
Below is an example of a Configuration Section in an app.config.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="TestProperties" type="System.Configuration.SingleTagSectionHandler" />
</configSections>
<TestProperties
TestPropertyOne="one"
TestPropertyTwo="two"
TestPropertyThree="three"
TestPropertyFour="4"
TestPropertyFiveAndHalf="5.5"/>
</configuration>As you can see here we have a configuration section called TestProperties. The type is the fully namespaced reference to the configuration class to be used along with the assembly that class belongs too.
To set up this class we merely add the ConfigurationAssist reference to that project, then create the class as shown below:
using System.Configuration;
using ConfigurationAssist.CustomAttributes;
namespace CodeSandboxConfigAssistTests
{
public class TestProperties
{
public string TestPropertyOne { get; set; }
public string TestPropertyTwo { get; set; }
public string TestPropertyThree { get; set; }
public int TestPropertyFour { get; set; }
public decimal TestPropertyFiveAndHalf { get; set; }
}
}That's it, the class is configured, now all we need to do is query it out. There is an Interface for this for if you want to use injection. In the example below though, I will just strongly reference a new instance of it.
To retrieve your configuration settings:
var configAssistant = new ConfigurationAssist.ConfigurationAssist();
var testProperties = configAssistant.ExtractSettings<TestProperties>();