App.Config AutoReader

In our day to day development, it is very rare that we don’t need to read a configuration file. There are many techniques for the treatment of such static classes, singleton classes, etc. and it is normally that 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.NET

Simple example of a string value.

ASP.NET

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. For using it, we need to download a NuGet Package.

    ASP.NET

    Install-Package MoralesLarios.Development

    ASP.NET
  1. Add the following setting in the app.config file.
    1. <?xml version="1.0" encoding="utf-8" ?>  
    2. <configuration>  
    3.     <startup>  
    4.         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup>  
    5.     <appSettings>  
    6.         <add key="FilterDate" value="01/01/2018" /> </appSettings>  
    7. </configuration>  
  1. Add using in the consumer class.

    using MoralesLarios.Development.Configuration;
  1. Create a new variable of your app.config settings key and call Config.ConfigNodes.[app.config_keyName].

    1. static void Main(string[] args) {  
    2.     DateTime filterDate = Config.ConfigNodes.FilterDate;  
    3. }  
    ASP.NET

    All code.

    1. using MoralesLarios.Development.Configuration;  
    2. using System;  
    3. namespace ConsoleApp1 {  
    4.     class Program {  
    5.         static void Main(string[] args) {  
    6.             DateTime filterDate = Config.ConfigNodes.FilterDate;  
    7.         }  
    8.     }  
    9. }  

The execution reads a DateTime value.

ASP.NET

Pros and Cons

These are the pros and cons of 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 same types:

  • Stirngs
  • 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.

ASP.NET

Numerics values

The process for numeric values is very similar to string values. In this action, the string app.config key value is transformed to a decimal value.

We decided to choose a decimal type for numeric values for including all numeric types (short, int, double, etc).

ASP.NET

Date and DateTime values

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

  • DATE

    ASP.NET

  • DATETIME

    ASP.NET

 

Bools values

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

ASP.NET

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 arrays types.

ASP.NET

It is the result.

ASP.NET

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.

ASP.NET

Variable value.

ASP.NET

ASP.NET

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.

ASP.NET

Example

ASP.NET

ASP.NET

Up Next
    Ebook Download
    View all
    Learn
    View all