Customizing the Config object

 

App.Config/Web.Config files

The App.Config file (or Web.Config file on ASP.NET Projects) is an XML based file which contains customized application configuration information. The first time ORM.NET  generates a data layer it also creates an App.Config file within the TestApp project with a single name/value pair setting. This is the Data Source Name - dsn - string which is used to connect to the database the Data Layer is based on.

 

ORM.NET also generates a config.cs class file included in the Data Layer project which wraps the .NET ConfigurationSettings object used to read the App.Config file. Once compiled you can use the Config object to read custom application information contained with the appSettings section using statement completion within your own application.

 

For example, database connection information is required to create a new DataManager object. Instead of passing in a string literal with this connection string the Dsn property of the Config object - read from the App.Config file - is used.

 

// Config object reads appSettings section in App.Config

DataManager dm = new DataManager(Config.Dsn);   

 

 

Example: The generated Dsn Property

You can customize the contents of this file to store your own custom application configuration information as well. The contents of the App.Config file used to generate the OleroTraining database is below:

 

 

<?xml version="1.0" encoding="Windows-1252"?>

<configuration>

<appSettings>

 

<add key="dsn" value="Data Source=dbServerName;Initial Catalog=OleroTraining;user id=sa;password=dbpassword"/>

 

</appSettings>

 

</configuration>

 

 

The generated Config.cs class creates a get method on a Property as shown below to read the information using the .NET ConfigurationSettings object. The AppSettings method maps to the appSettings section within the App.Config file where the value(s) will be read from.

 

public class Config

{

      public static string Dsn

       {

              get

              {

                     return ConfigurationSettings.AppSettings["dsn"];

               }

       }

 

}

 

Example 2: Creating a new application setting

 

If you want to add your own customized application settings to take advantage of the Config object you can do by completing the following two steps:

 

  1. Add the new key and value information in the appSettings section of the App.config or Web.Config file.

  2. Create a new property within the Config.cs class which returns the value using the internal ConfigurationSettings object.

 

The following example assumes you have a method which traps any exceptions and writes them to local file. The file path is determined by the ErrorLogPath setting defined in the App.Config file. Once you complete the steps below and re-compile the solution you will be able to reference the ErrorLogPath setting as Config.ErrorLogPath within your application.

 

Step 1. Add the ErrorLogPath key and value information to the App.Config or Web.Config file.

 

<?xml version="1.0" encoding="Windows-1252"?>

<configuration>

 

  <appSettings>

    <add key="dsn" value="Data Source=dbServerName;Initial Catalog=OleroTraining;user id=sa;password=dbpassword"/>

 

        <add key="ErrorLogPath" value="C:\Projects\MyApp\Errors"/>

 

    </appSettings>

 

</configuration>

 

 

Step 2:  Define a new Property within the Config.cs class file.

 

public class Config

{

      public static string Dsn

       {

              get

              {

                     return ConfigurationSettings.AppSettings["dsn"];

               }

       }

      public static string ErrorLogPath

       {

              get

              {

                     return ConfigurationSettings.AppSettings["ErrorLogPath"];

               }

       }

 

}

 

 

Step 3: Compile and then use the new Config property within your application.

 

Console.WriteLine("The Error Log Path is: " + Config.ErrorLogPath);