Click here to Skip to main content
14,691,731 members
Articles » Platforms, Frameworks & Libraries » .NET Framework » General
Article
Posted 14 Jan 2018

Tagged as

Stats

8.6K views
9 bookmarked

App.Config Autoreader

Rate me:
Please Sign up or sign in to vote.
4.52/5 (8 votes)
14 Jan 2018CPOL
Automatic read and convert data of App.Config settings keys.

Introduction

In our day to day, in our developments, 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 to 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 in inside of MoralesLarios.Development project in GitHub in this 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 consists of app.config file read in the first step, convert values action for second step and final step, creation result class.

Image 1

Simple example of a string value.

 

Image 2

The process transforms the string key value to a strong type target variable.

The Config class, is responsible for expose the transformed app.config values.  

The Config class expose the app.Config values with strong types, but in a dynamic property.

 

Install and use

1.- For use, we download a nuget package MoralesLarios.Development.

Image 3

Image 4

Install-Package MoralesLarios.Development

 

2.- Add one setting in an 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>

 

3.- Add using in the consumer class.

using MoralesLarios.Development.Configuration;

 

4.- 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;
}

 

Image 5

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.

 

Image 6

 

 

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:

  • Stirngs
  • Numerics
  • Dates and DateTimes
  • Bools
  • Array of
    • Strings Arrays
    • Numerics Arrays
    • DateTimes Arrays
    • Bools Arrays

We will explain in deep each one of them.

 

 

Strings values

Is the very single process and transform the string app.config key value to string variable destination.

Image 7

 

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 choose a decimal type for numerics values for include all numerics types (short, int, double, etc).

Image 8

 

Date and DateTime values

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

DATE

Image 9

DATETIME

Image 10

 

Bools values

In this action the string app.config key value is transformed to bool value.

Image 11

 

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.

Image 12

Its result:

Image 13

 

 

Force values to string

In cases we can may need read app.config key values of types (numerics, datetimes, bools, etc) as a strings values. In these cases, we can use (‘’) for force a string read value.

Image 14

Variable value.

Image 15

Image 16

 

 

Save special character

If we want read a app.config key with special character as ( ; or ‘’), we can preceded the ‘\’ backslash the special character.

Image 17

Example:

Image 18

Image 19

 

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Juan Francisco Morales Larios
Software Developer (Senior) Cecabank
Spain Spain
MVP C# Corner 2017

MAP Microsoft Active Professional 2014

MCPD - Designing and Developing Windows Applications .NET Framework 4
MCTS - Windows Applications Development .NET Framework 4
MCTS - Accessing Data Development .NET Framework 4
MCTS - WCF Development .NET Framework 4

Comments and Discussions

 
QuestionMy vote of #2 Pin
BillWoodruff16-Jan-18 14:14
mveBillWoodruff16-Jan-18 14:14 
The VS built-in app settings facility offers native support for all the numeric types, string, DateTime, TimeSpan, bool, Font, Color, Point, etc. imho, using your code would require about the same level of syntactic complexity.

Where there is a need to load complex objects to instantiate, and use, you can, of course, serialize/deserialize in several different ways: WCF DataContract has made this very easy; it's no longer difficult to persist generic Dictionaries.

This article/code appears to be an elaborate substitute for creating a strongly typed app settings' facility, which, because it uses Dynamic, is going to have Type conversion overhead ... more so because you are using string conversion.

Your code seems to implement what is often called a PropertyBag; .NET already has one [^].

Other examples on the web are based on using HashSets for efficiency: of course, with those, you pay a price in conversion to/from Type Object.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12

AnswerRe: My vote of #2 Pin
Juan Francisco Morales Larios16-Jan-18 15:39
MemberJuan Francisco Morales Larios16-Jan-18 15:39 
GeneralRe: My vote of #2 Pin
BillWoodruff18-Jan-18 17:38
mveBillWoodruff18-Jan-18 17:38 
GeneralRe: My vote of #2 Pin
Juan Francisco Morales Larios19-Jan-18 3:23
MemberJuan Francisco Morales Larios19-Jan-18 3:23 
Questionwhat is the license of your GitHub project? Pin
Southmountain15-Jan-18 12:03
MemberSouthmountain15-Jan-18 12:03 
AnswerRe: what is the license of your GitHub project? Pin
Juan Francisco Morales Larios15-Jan-18 22:08
MemberJuan Francisco Morales Larios15-Jan-18 22:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.