一个简单的配置管理器(SettingManager)
作者:网络转载 发布时间:[ 2015/2/3 15:55:11 ] 推荐标签:配置管理 软件测试管理
在很多.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);
}
}
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11