CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Generic Configuration Files....Beyond web.config

496 Views
Copy Code Show/Hide Line Numbers
   1:  using System;
   2:  using System.Collections.Specialized;
   3:  using System.Web;
   4:  using System.IO;
   5:   
   6:  namespace ConfigFileExample.Services
   7:  {
   8:      public interface IConfigurationManager
   9:      {
  10:          NameValueCollection AppSettings { get; }
  11:          string ConnectionStrings(string name);
  12:      }
  13:   
  14:      public class ConfigurationManagerWrapper : IConfigurationManager
  15:      {
  16:          public NameValueCollection AppSettings
  17:          {
  18:              get { return System.Configuration.ConfigurationManager.AppSettings; }
  19:          }
  20:   
  21:          public string ConnectionStrings(string name)
  22:          {
  23:              if (System.Configuration.ConfigurationManager.ConnectionStrings[name] != null)
  24:              {
  25:                  return System.Configuration.ConfigurationManager.ConnectionStrings[name].ConnectionString;
  26:              }
  27:              return null;
  28:          }
  29:      }
  30:   
  31:      public interface IFileConfigurationManager : IConfigurationManager
  32:      {
  33:          string FileName { get; }
  34:          void ParseFile();
  35:      }
  36:   
  37:      public class SettingsFileConfigurationManager : IFileConfigurationManager       
  38:      {
  39:          private readonly FileSystemWatcher _watcher;
  40:          private readonly NameValueCollection _appSettings = new NameValueCollection();
  41:          private readonly NameValueCollection _connectionStrings = new NameValueCollection();
  42:   
  43:          public NameValueCollection AppSettings
  44:          {
  45:              get
  46:              {
  47:                  lock(_appSettings)
  48:                  {
  49:                      return _appSettings;
  50:                  }
  51:              }
  52:          }
  53:   
  54:          public string ConnectionStrings(string name)
  55:          {
  56:              lock(_connectionStrings)
  57:              {
  58:                  if(_connectionStrings[name]!=null)
  59:                  {
  60:                      return _connectionStrings[name];
  61:                  }
  62:              }
  63:              return null;
  64:          }
  65:   
  66:          public string FileName { get; private set; }
  67:   
  68:          public SettingsFileConfigurationManager(string configFile, bool monitorChanges)
  69:          {
  70:              if(string.IsNullOrEmpty(configFile))
  71:              {
  72:                  throw new ArgumentNullException("configFile");
  73:              }
  74:              if(!File.Exists(configFile))
  75:              {
  76:                  throw new FileNotFoundException("configFile");
  77:              }
  78:              FileName = configFile;
  79:              ParseFile();
  80:   
  81:              if(monitorChanges)
  82:              {
  83:                  string directoryName = Path.GetDirectoryName(FileName);
  84:                  string fileName = Path.GetFileName(FileName);
  85:                  _watcher = new FileSystemWatcher(directoryName, fileName);
  86:                  _watcher.NotifyFilter = NotifyFilters.LastWrite;
  87:                  _watcher.Changed += WatcherChanged;
  88:                  _watcher.EnableRaisingEvents = true;
  89:              }
  90:          }
  91:   
  92:          public void ParseFile()
  93:          {
  94:              // Parse your config file here.
  95:              // Throw execption if it fails.
  96:              
  97:              // Demo code
  98:              lock (_appSettings)
  99:              {
 100:                  _appSettings.Add("Name1", "Value1");
 101:                  _appSettings.Add("Name2", "Value2");
 102:              }
 103:              lock (_connectionStrings)
 104:              {
 105:                  _connectionStrings.Add("DB1", "Connection String 1");
 106:              }
 107:          }
 108:   
 109:          private void WatcherChanged(object sender, FileSystemEventArgs e)
 110:          {
 111:              ParseFile();
 112:          }
 113:      }
 114:   
 115:      public interface IConfigurationData
 116:      {
 117:          bool LogAll { get; }
 118:          string CdnUrl { get; }
 119:      }
 120:   
 121:      public class ConfigurationData : IConfigurationData
 122:      {
 123:          private readonly IConfigurationManager _configurationManager;
 124:   
 125:          public bool LogAll { get { return bool.Parse(_configurationManager.AppSettings["LogAll"]); } }
 126:          public string CdnUrl { get { return _configurationManager.AppSettings["CdnUrl"]; } }
 127:   
 128:          public ConfigurationData(IConfigurationManager configurationManager)
 129:          {
 130:              _configurationManager = configurationManager ?? new ConfigurationManagerWrapper();
 131:          }
 132:      }
 133:   
 134:      public static class Configuration
 135:      {
 136:          private static readonly string _applicationPath = HttpRuntime.AppDomainAppPath;
 137:          private static readonly string _configFilePath = Path.Combine(_applicationPath, "settings.config");
 138:          private static readonly ConfigurationData _instance = new ConfigurationData(new SettingsFileConfigurationManager(_configFilePath, true));
 139:   
 140:          public static IConfigurationData GetInstance { get { return _instance; } }
 141:      }
 142:  }
by Bob Cravens
  February 03, 2010 @ 2:46pm
Tags:
Description:
See discussion here:http://codebetter.com/blogs/karlseguin/archive/2010/01/28/beyond-web-config.aspx

by Bob Cravens    February 03, 2010 @ 3:04pm

Okay...I fixed it.

by Bob Cravens    February 03, 2010 @ 3:03pm

Just noticed there are two instances of 'ParseConfigFile' that should be renamed to 'ParseFile'. It does not look like I can edit once I have posted. Sorry.

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate