Custom Configurations:
Custom configurations is designed to reduce the repeative lines of code needed to get configuration settings into your application. If there are 5 or 50 settings there doesn't need any extra code to get your settings. It allows the application to simplify the use of configuration settings.
A simple but effective use is to have an object with N number of properties, such as:
publicclassMyAppSettings{publicstringName{get;set;}publicstringLocation{get;set;}publicboolShouldDoX{get;set;}publicintminValue{get;set;}}Then use the configuration loader to populate the values:
MyAppSettingsappSettings=newCustomConfigurations.Config().Create<MyAppSettings>();Lets give a simple strongly typed example, lets say we have the following configuration file:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionname="domainModelTemplate"type="CustomConfigurations.ConfigurationSectionLoader, CustomConfigurations"/>
</configSections>
<domainModelTemplate>
<Configs>
<ConfigurationGroupname="model">
<ValueItems>
<ValueItemkey="CanExecute"value="true"/>
<ValueItemkey="Description"value="domain model template desciption field"/>
<ValueItemkey="NumberUnits"value="5"/>
<ValueItemkey="ModelType"value="TheirType"/>
</ValueItems>
</ConfigurationGroup>
</Configs>
</domainModelTemplate>
<startup><supportedRuntimeversion="v2.0.50727"/></startup></configuration>We want to load this model into memory:
publicclassDomainModel{publicstringName{get;set;}publicboolCanExecute{get;set;}publicstringDescription{get;set;}publicintNumberUnits{get;set;}publicDomainModelTypeModelType{get;set;}}publicenumDomainModelType{MyType,TheirType,AnotherType,}We can create a configuration load to isolate the use of custom configuration and simply return a strongly typed object to your business logic:
publicstaticclassConfigurationLoader{publicstaticDomainModelLoadDomainModelTemplate(){returnnewConfig("domainModelTemplate").GetSection("model").Create<DomainModel>();}}Another example is when the app has multiple clients settings to be recorded a section for each can be defined and the values seperated out. The config class allows easy access to a clients configuration and deals with the configuration hooking up in the background for you.
At this point it is not possible to save back to the configuration file. I have tried to get this to work across the board but failed misserably, will try to tackle this again at a later stage, (there are a few commented out sections and tests aimed at this in the committed code).
Here is an example configuration file:
<configuration>
<configSections>
<sectionname="testsection2"type="CustomConfigurations.ConfigurationSectionLoader, CustomConfigurations" />
<sectionGroupname="myCustomGroup">
<sectionname="mysection"type="CustomConfigurations.ConfigurationSectionLoader, CustomConfigurations" />
</sectionGroup>
</configSections>
<myCustomGroup>
<mysection>
<Configs>
<ConfigurationGroupname="client1">
<ValueItems>
<ValueItemkey="key2"value="value2" />
<ValueItemkey="key3"value="value3" />
<ValueItemkey="key4"value="value4" />
<ValueItemkey="key5"value="7" />
<ValueItemkey="key6"value="0.6" /> </ConfigurationGroup>
<ConfigurationGroupname="client2">
<ValueItems>
<ValueItemkey="keya"value="abc" />
<ValueItemkey="key2"value="123" />
</ValueItems>
</ConfigurationGroup>
</Configs>
</mysection>
</myCustomGroup>
<testsection2>
<Configs>
<ConfigurationGroupname="clienta">
<ValueItems>
<ValueItemkey="key2"value="valueabc" /> </ValueItems> </ConfigurationGroup>
</Configs>
</testsection2>
</configuration>CustomConfigurations.ConfigConfigloader=newCustomConfigurations.Config("client2");The console application "ExampleApp" shows the different ways the customConfiguration application can be used. The examples include:
- Simply loading the configuration settings and manually getting a setting from it, (SimpleLoadingOfConfiguration.cs)
- Showing how the application will try and determine the custom section in the config file so you do not have to explicity declare it everytime (LoadingOfConfigurationByAllowingAppToDetermineConfigPath.cs).
- How to load multiple configuration sections at one time (LoadingMultipleConfigSections.cs)
- How to create more complex data structures in config, using inner collections (UsingInnerCollections.cs)
- How to load a different configuration file (LoadingAConfigFileThatIsNotTheDefaultConfig.cs)
- How to generate strongly typed objects automatically (ExampleApp.MoreComplexExamples.AutoGenerateModelsFromConfig)
- How to map configuration settings keys to the strongly typed object that have different property names (ExampleApp.MoreComplexExamples.MappingFieldsAutoGen)
If required the whole configuration section can be encrypted. The enryption can be used by multiple machines, (no machine key files used). The encryption uses Triple-DES with a key file. The first time it is to be used the key file needs to be generated.
Configconfig=newConfig();config.CreateConfigKey();There are two ways to encrypt the configuration.
Configconfig=newConfig();config.EncryptConfigurationSection();This will encrypt all custom config sections in the configuration file.
Configconfig=newConfig();config.EncryptConfigurationSection("sectionName");To be able to do the encyption and decryption there is one addition to the configuration file:
<configProtectedData>
<providers>
<addname="TripleDESProtectedConfigurationProvider"type="CustomConfigurations.TripleDESProtectedConfigurationProvider, CustomConfigurations"keyFilePath="configkey.txt" />
</providers>
</configProtectedData>This will only encrypt the configuration section with the name "sectionName".
Thanks
N.B. all config sections are case sensitive, be careful how you write the xml and using the selectors.