日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一个简单的配置管理器(SettingManager)

發布時間:2025/5/22 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个简单的配置管理器(SettingManager) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ? 在很多.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";}} } View Code

?

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 Addpublic 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);}} } View Code

最底層是一個單例模式,并封裝了字典的CRUD操作。

客戶端測試一下:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace ConfigManager {class Program{static void Main(string[] args){var settings = ConfigManager.Appettings;foreach(var item in settings){Console.WriteLine("key:{0},value:{1}",item.Key,item.Value);}var t1 = ConfigManager.Appettings["key1"];//test addConfigManager.Add("t","test");//var t2 = ConfigManager.Appettings;//updateConfigManager.Update("t","test123");//removeConfigManager.Remove("t");Console.ReadKey();}} } View Code

?

好像也能運行。

那么,問題來了!測試代碼改一改,

//test not item in Dictionaryvar t2 = ConfigManager.Appettings["luckyhu"];

代碼崩潰了,老兄!

其實上面的代碼能夠滿足一般的需求,但是對使用著來說,仍然不太方便.所以我和同事進一步優化了上述代碼.

現在變得更加簡潔了.

public class SettingManager : Dictionary<string, string>{private static SettingManager _Settings = null;public static SettingManager Settings{get{if (_Settings == null)_Settings = new SettingManager();return _Settings;}}private SettingManager(){//Init Data//DataSoure:truely data here...for (int i = 0; i < 10; i++){var key = String.Format("key{0}", i);var value = String.Format("value{0}", i);if (!this.Keys.Contains(key))this.Add(key, value);}}public string this[string key]{get{if (!this.ContainsKey(key))return String.Empty;return base[key];}set{base[key] = value;}}public static bool GetBoolValue(string key){bool value = false;bool.TryParse(Settings[key], out value);return value;}public static int GetIntValue(string key){int value = 0;int.TryParse(Settings[key], out value);return value;}} View Code

大家看到代碼簡潔了不少,有了以下改進:

1.代碼變少了

2.可以控制索引的返回結果了

3.更多的利用了Dictionary自身的特性,如CRUD

4.增加了自定義類型轉換方法

?總之,這些努力都是為了方便別人使用.

?

好吧,看看客戶端測試吧

測試結果是OK的

好了,這樣一個通用的配置管理器完成了, 當然有更多的需求,還可以對其進行擴展。歡迎大家不吝賜教 .

祝大家新年快樂,萬事如意! 2015,一起任性!

?

轉載于:https://www.cnblogs.com/lucky_hu/p/4198227.html

總結

以上是生活随笔為你收集整理的一个简单的配置管理器(SettingManager)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产在线一 | 牛牛视频在线 | 日日插夜夜爽 | 国产伦精品一区二区三区视频黑人 | 四虎在线网址 | 在线看片中文字幕 | 色诱视频在线观看 | 成人免费福利 | 亚洲一区日本 | 999免费视频 | www.777奇米| 黑人玩弄人妻一区二 | 亚洲色图国产视频 | 亚洲日本在线观看 | 97狠狠| 屁屁影院一区二区三区 | 久久久久久久久久国产 | 国产黄视频网站 | 久久久久久网 | 成人免费黄色 | 久久久久性色av无码一区二区 | 日日操日日干 | 天天插插插 | 日本少妇做爰全过程毛片 | yw在线观看| 日本黄色三级视频 | 久久精品午夜 | 成人深夜电影 | 中文字幕一本 | 91影院在线观看 | 性欢交69精品久久久 | www.成年人| 国产免费av一区二区 | 国产3p露脸普通话对白 | 这里只有久久精品 | 精品国产午夜 | 秘密基地动漫在线观看免费 | 亚洲色图欧美日韩 | 性生交大片免费看女人按摩 | 岛国av一区 | 亚洲成人91| 欧美丝袜一区二区三区 | 欧美另类z0z变态 | 秋霞7777鲁丝伊人久久影院 | 久久久97| 亚洲成人资源 | 亚洲国产成人自拍 | 亚洲欧美日韩第一页 | 美国美女群体交乱 | 亚洲av综合色区无码一二三区 | 中国videosex高潮hd | 一本之道av| 国产成人精品视频在线观看 | 毛片久久久久 | 欧美日韩在线播放三区四区 | 99精品在线播放 | 亚洲精品日韩在线 | 欧美蜜桃视频 | 色悠悠av | 久久成人免费电影 | 欧美精产国品一二三区 | 亚洲第一自拍 | 精品玖玖玖 | 色综合久久88 | 欧美视频精品 | 亚洲毛茸茸 | 日本在线播放视频 | 国产成人av无码精品 | 亚洲美女视频网站 | 黄色无遮挡网站 | 久久久国产成人 | 强行挺进白丝老师翘臀网站 | 五月激情综合网 | 伊人福利在线 | 亚洲黄片一区二区 | 一区二区三区在线免费播放 | 国产成人+综合亚洲+天堂 | 欧美涩涩视频 | 亚洲高清不卡 | 亚洲精品天堂在线 | 直接看av的网站 | 亚洲日本久久 | 大尺度做爰呻吟62集 | 免费又黄又爽又色的视频 | 亚洲成av人片 | 久草影视网| 色婷婷综合久久久久中文 | 欧美中文字幕在线 | 四虎影视免费永久大全 | 国产午夜在线 | 国产在线网址 | 亚洲一区二区电影网 | 男女猛烈无遮挡 | 国产视频一二三 | 欧美日韩一区二区视频观看 | 国产精品第108页 | 亚洲午夜一区二区三区 | 午夜爱爱免费视频 | 久久免费播放视频 |