Do you make a strongly-typed wrapper for app.config?

Last updated by Igor Goldobin 2 months ago.See history

If your application accesses properties in app.config, you should provide a strongly typed wrapper for the app.config file. The following code shows you how to build a simple wrapper for app.config in an AssemblyConfiguration class:

using System;
using System.Configuration;

namespace SSW.Northwind.WindowsUI
{
   public sealed class AssemblyConfiguration
   {
      // Prevent the class from being constructed
      private AssemblyConfiguration() { }

      public static string ConnectionString
      {
         get
         {
            return
               ConfigurationSettings.AppSettings["ConnectionString"].
               ToString();
         }
      }
   }
}

Unfortunately, the Configuration Block does not automatically provide this wrapper.

We open source. Powered by GitHub