Introduction
In our day to day development, it is a rare case when we don’t need to read a configuration file.
There are many techniques for its treatment such as a static class, a singleton class, etc. It is also normal to make this config class be accessible throughout the project.
We will write a utility to automate the task of reading an app.config file. With this utility, you will forget about using the ConfigurationManager
class and the System.Configuration
assembly.
We will make use of dynamics.
App.Config Autoreader
is an Open Source project and it is available inside of MoralesLarios.Development
project in GitHub in this link.
Index
- Autoreader description
- Install and use
- Pros and Cons
- Autoreader Transforms Types
- Strings values
- Numerics values
- Date and DateTime values
- Bools values
- Arrays values
- Force values to string
- Save special character
Autoreader Description
The autoreader action consists of app.config file read in the first step, convert values action for second step and final step, creation result class.
Simple example of a string
value.
The process transforms the string
key value to a strong type target variable.
The Config
class, is responsible for exposing the transformed app.config values.
The Config
class expose the app.Config
values with strong types, but in a dynamic property.
Install and Use
- For use, we download a nuget package
MoralesLarios.Development
.
Install-Package MoralesLarios.Development
.
- Add one setting in an app.config file.
="1.0"="utf-8"
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<appSettings>
<add key="FilterDate" value="01/01/2018"/>
</appSettings>
</configuration>
- Add
using
in the consumer
class.
using MoralesLarios.Development.Configuration;
- Create a new variable of our
app.config
settings key and call Config.ConfigNodes.[app.config_keyName]
.
static void Main(string[] args)
{
DateTime filterDate = Config.ConfigNodes.FilterDate;
}
All code.
using MoralesLarios.Development.Configuration;
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
DateTime filterDate = Config.ConfigNodes.FilterDate;
}
}
}
The execution read a DateTime
value.
Prons and Cons
These are the pros and cons of Autoreader
App.Config utility:
Pros:
- Faster reader
- Faster transform
- Simple use and compression
- Add a new key in app.config and it is available in this time.
Cons:
- The app values are exposed in dynamics values, so that we lost the intellisense.
Autoreader Transforms Types
Autoreader utility can transform values of same types:
- Strings
- Numerics
- Dates and DateTimes
- Bools
- Array of
- Strings Arrays
- Numerics Arrays
- DateTimes Arrays
- Bools Arrays
We will explain each one of them in depth.
Strings Values
Is the very single process and transform the string app.config
key value to string variable destination.
Numerics Values
The process for numerics values is very similar to string values. In this action, the string app.config key value is transformed to decimal value.
We decided to choose a decimal type for numerics values for including all numerics types (short
, int
, double
, etc.).
Date and DateTime Values
In this action, the string app.config
key value is transformed to datetime
value.
Date
DateTime
Bools Values
In this action, the string
app.config
key value is transformed to bool
value.
Arrays Values
The array process is the same of the another’s types, but with the deference, the app.config
key value should contain an internal ‘;
’ value, for delimiting some array nodes.
This rule is valid for all arrays types.
Its result:
Force Values to String
In cases we may need to read app.config
key values of types (numerics, datetimes, bools, etc.) as a strings values, we can use (‘’) for forcing a string
read value.
Variable value.
Save Special Character
If we want read an app.config
key with special character as ( ; or ‘’), we can preceded the ‘\’ backslash the special character.
Example:
History
- 14th January, 2018: Initial version