??????????霉?????(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