在很多.net开发的项目中,我们几乎都会使用到一些自定义的参数,比如说第三方的配置参数之类的.
  他们的特点是:1.系统全局 2,可以做成键值对(Dictionary).
  我们可以将这些参数放到Web.config,xml或者数据库表中,当然部分不常变的可以直接写在程序中.
  为了方便我通常喜欢将他们统放在一个配置管理器中,然后希望别人使用时, 可以像使用AppSetings中的参数一样

  初看起来还是比较容易实现,在ConfiguratonManager中定义一个公开属性AppSettings好了.
  实现如下:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfigManager
{
public class ConfigurationContainer
{
private static ConfigurationContainer m_Instance = null;
public static ConfigurationContainer Instance
{
get
{
if (m_Instance == null)
m_Instance = new ConfigurationContainer();
return m_Instance;
}
}
private ConfigurationContainer()
{
}
private ReadOnlyDictionary<string, string> _configuration;
private Dictionary<string, string> _mutableConfiguration;
public ReadOnlyDictionary<string, string> Configuration
{
get
{
//TODO:check is newest or not in database??
if (_mutableConfiguration == null)
Init();
_configuration =
new ReadOnlyDictionary<string, string>(_mutableConfiguration);
return _configuration;
}
}
public bool Add(string key, string value)
{
bool bRet = false;
if (!_mutableConfiguration.ContainsKey(key))
{
_mutableConfiguration.Add(key, value);
bRet = true;
}
return bRet;
}
public bool Update(string key, string value)
{
bool bRet = false;
if (_mutableConfiguration.ContainsKey(key))
{
_mutableConfiguration[key] = value;
bRet = true;
}
return bRet;
}
public bool Remove(string key)
{
bool bRet = false;
if (_mutableConfiguration.ContainsKey(key))
{
_mutableConfiguration.Remove(key);
bRet = true;
}
return bRet;
}
//private bool ConfigurationAllowed(string key, string value)
//{
//    // Put in your checks and balances
//    // here and return the appropriate result
//    return true;
//}
private void Init()
{
_mutableConfiguration = new Dictionary<string, string>();
_mutableConfiguration.Add("key", "value");
_mutableConfiguration.Add("key1", "value1");
_mutableConfiguration["key2"] = "value2";
}
}
}
ConfigurationContainer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfigManager
{
public class ConfigManager
{
private static IReadOnlyDictionary<string, string> _AppSettings = null;
public static IReadOnlyDictionary<string, string> Appettings
{
get
{
//initial ensurer the newest
_AppSettings = ConfigurationContainer.Instance.Configuration;
return _AppSettings;
}
}
//Exception:Violence to  Add
public static void BeNaughtyWithConfiguration()
{
IDictionary<string, string> convertToReadWrite
= (IDictionary<string, string>)_AppSettings;
//ConfigElement element = convertToReadWrite["key"];
//element.Value = "Haa Haa";
//Console.WriteLine(element.Value);
//Console.WriteLine(convertToReadWrite["key"]);
//Console.ReadLine();
convertToReadWrite.Add("Key12345", "xsds");
}
public static bool Add(string key, string value)
{
return ConfigurationContainer.Instance.Add(key, value);
}
public static bool Update(string key, string value)
{
return ConfigurationContainer.Instance.Update(key, value);
}
public static bool Remove(string key)
{
return ConfigurationContainer.Instance.Remove(key);
}
}
}