In our day-to-day development, rarely, we don’t need to read a configuration file. There are many techniques for the treatment of such static classes, singleton classes, etc. and, normally, the Config class is accessible in all projects.
We write a utility to automate the reading of app.config files. With this utility, you will forget the ConfigurationManager class and the System.Configuration assembly.
We will support each of them for this dynamically.
App.Config Autoreader is an open-source project and is available in the MoralesLarios.Development project on GitHub. Here is the 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 is used for reading app.config file in the first step, converting values action in the second step, and creating a result class in the final step,
A simple example of a string value.
The process transforms the string key value to a strongly typed target variable. The Config class is responsible for exposing the transformed app.config values. The Config class exposes the app.Config values with strong types, but in a dynamic property.
Installation and use
- To use it, we need to download a NuGet Package.
- Install-Package MoralesLarios.Development
- Add the following setting in the app.config file.
<?xml version="1.0" encoding="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 your 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 reads a DateTime value.
Pros and Cons
These are the pros and cons of the Autoreader App.Config utility.
PROS.
- Faster reading
- Faster transformation
- Simple to use and easy compression
- Adds a new key in app.config and it is available this time.
CONS.
- The app values are exposed to dynamic values so we lose the IntelliSense.
Autoreader transforms types
Autoreader utility can transform values of the 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 a single process and transforms the string app.config key value to string variable destination.
Numerics values
The process for numeric values is very similar to string values. In this action, the string app.config key value is transformed into a decimal value.
We decided to choose a decimal type for numeric values for including all numeric types (short, int, double, etc).
Date and DateTime values
In this action, the string app.config key value is transformed into a datetime value.
- DATE
- DATETIME
Bools values
In this action, the string app.config key value is transformed into a boolean value.
Arrays values
The array process is the same as other types, but with the difference that the app.config key value should contain an internal ‘;’ value for delimiting some array nodes.
This rule is valid for all array types.
It is the result.
Force values to string
In some cases, we may need to read app.config key values of types (numeric, DateTime, bool, etc.) as a string value. In these cases, we can use (‘’) for forcing a string read value.
Variable value
Save special character
If we want to read an app.config key with special characters such as (; or ‘’), we can precede the ‘\’ backslash as the special character.
Example