App.Config AutoReader

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

  1. Autoreader description
  2. Install and use
  3. Pros and Cons
  4. Autoreader Transforms Types
    • Strings values
    • Numerics values
    • Date and DateTime values
    • Bools values
    • Arrays values
  5. Force values to string
  6. 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,

ASP dot NET

A simple example of a string value.

Transform

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

  1. To use it, we need to download a NuGet Package.
    Morales lories
  2. Install-Package MoralesLarios.Development
    Install package
  3. 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>  
  4. Add using in the consumer class.
    using MoralesLarios.Development.Configuration;
  5. 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;  
    }  
    
  6. 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.

Date

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.

String variable

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).

Numerics values

Date and DateTime values

In this action, the string app.config key value is transformed into a datetime value.

  • DATE
    Date
  • DATETIME
    Date time

Bools values

In this action, the string app.config key value is transformed into a boolean value.

Bools values

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.

Arrays values

It is the result.

Decimals

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.

Values to string

Variable value

Variable

Text visualizer

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.

Save special character

Example

Keys

Filter date


Similar Articles