在我們開(kāi)發(fā)Winform界面的時(shí)候,往往需要綁定數(shù)據(jù)字典操作,也就是綁定一些下拉列表或者一些列表顯示等,以便我們方便選擇數(shù)據(jù)操作,常見(jiàn)的字典綁定操作就是對(duì)下拉列表的處理,本篇隨筆是基于DevExpress界面的一些處理操作,原理也適用于常規(guī)Winform界面或者DotNetBar控件界面處理。另外對(duì)于緩存的處理,一般在基于單機(jī)版數(shù)據(jù)或者局域網(wǎng)API接口處理的字典綁定,速度是比較快的,基本上可以不用考慮緩存的處理,但是對(duì)于基于互聯(lián)網(wǎng)API接口的數(shù)據(jù)處理,往往受限于帶寬等原因,請(qǐng)求數(shù)據(jù)的速度沒(méi)有那么快,那么需要做好數(shù)據(jù)緩存處理,才可能更好的提高用戶體驗(yàn)。
1、常規(guī)字典列表的綁定
對(duì)于普通的下拉列表控件,我們綁定操作就是先獲取字典數(shù)據(jù)列表,然后對(duì)它的數(shù)據(jù)項(xiàng)進(jìn)行添加操作即可,為了方便,我們往往做成一個(gè)擴(kuò)展函數(shù)的方式來(lái)進(jìn)行處理,并把這些通用的擴(kuò)展函數(shù)放到界面基類(lèi)庫(kù)里面方便重用,這樣我們可以在設(shè)計(jì)到界面的數(shù)據(jù)綁定的時(shí)候,非常方便的調(diào)用了。
如類(lèi)似下面的擴(kuò)展函數(shù)定義。
/// <summary>
/// 綁定下拉列表控件為指定的數(shù)據(jù)字典列表
/// </summary>
/// <param name="combo">下拉列表控件</param>
/// <param name="itemList">數(shù)據(jù)字典列表</param>
/// <param name="defaultValue">控件默認(rèn)值</param>
/// <param name="emptyFlag">是否加入空值選項(xiàng)</param>
public static void BindDictItems(this ComboBoxEdit combo, List<string> itemList, string defaultValue, bool emptyFlag = true)
{
combo.Properties.BeginUpdate();//可以加快
combo.Properties.Items.Clear();
combo.Properties.Items.AddRange(itemList);
if (emptyFlag)
{
combo.Properties.Items.Insert(0, "");
}
if (itemList.Count > 0)
{
combo.SetDropDownValue(defaultValue);
}
combo.Properties.EndUpdate();//可以加快
}
還有有時(shí)候我們需要一個(gè)顯示文本、值的鍵值對(duì)來(lái)對(duì)字典進(jìn)行處理,如定義的CListItem對(duì)象
/// <summary>
/// 框架用來(lái)記錄字典鍵值的類(lèi),用于Comobox等控件對(duì)象的值傳遞
/// </summary>
[Serializable]
public class CListItem
{
/// <summary>
/// 顯示內(nèi)容
/// </summary>
public string Text { get; set; }
/// <summary>
/// 實(shí)際值內(nèi)容
/// </summary>
public string Value { get; set; }
}
這樣我們綁定列表的擴(kuò)展函數(shù)在定義一個(gè)函數(shù),如下所示。
/// <summary>
/// 綁定下拉列表控件為指定的數(shù)據(jù)字典列表
/// </summary>
/// <param name="combo">下拉列表控件</param>
/// <param name="itemList">數(shù)據(jù)字典列表</param>
/// <param name="defaultValue">控件默認(rèn)值</param>
/// <param name="emptyFlag">是否加入空值選項(xiàng)</param>
public static void BindDictItems(this ComboBoxEdit combo, List<CListItem> itemList, string defaultValue, bool emptyFlag = true)
{
combo.Properties.BeginUpdate();//可以加快
combo.Properties.Items.Clear();
combo.Properties.Items.AddRange(itemList);
if (emptyFlag)
{
combo.Properties.Items.Insert(0, new CListItem(""));
}
if (itemList.Count > 0)
{
if (!string.IsNullOrEmpty(defaultValue))
{
combo.SetComboBoxItem(defaultValue);
}
else
{
combo.SelectedIndex = 0;
}
}
combo.Properties.EndUpdate();//可以加快
}
當(dāng)然,除了上面的這兩個(gè)處理,我們還可以定義很多不同類(lèi)型的重載方法,以便更方便處理相關(guān)的控件的字典數(shù)據(jù)綁定。
對(duì)于固定數(shù)據(jù)源List<string>、或者List<CListItem>來(lái)說(shuō),我們綁定的操作就非常簡(jiǎn)單。
List<CListItem> itemList= new List<CListItem>() { new CListItem("有"), new CListItem("無(wú)") };
txtItem.BindDictItems(itemList);
然后獲取對(duì)應(yīng)字典值的方式,我們可以定義一個(gè)擴(kuò)展函數(shù)來(lái)處理,如下代碼所示。
/// <summary>
/// 獲取下拉列表的值
/// </summary>
/// <param name="combo">下拉列表</param>
/// <returns></returns>
public static string GetComboBoxValue(this ComboBoxEdit combo)
{
CListItem item = combo.SelectedItem as CListItem;
if (item != null)
{
return item.Value;
}
else
{
return "";
}
}
對(duì)于以上的操作,我們這里還沒(méi)有涉及到字典模塊里面的數(shù)據(jù)源,只是提供一些常規(guī)的固定列表,我們知道,大多數(shù)的數(shù)據(jù)字典我們是通過(guò)字典模塊來(lái)進(jìn)行維護(hù)的。

因此我們也需要?jiǎng)討B(tài)的從字典庫(kù)上獲取對(duì)應(yīng)的字典集合來(lái)進(jìn)行綁定。字典的數(shù)據(jù),我們可以通過(guò)字典類(lèi)型或者字典代碼來(lái)獲取,如下是通過(guò)字典類(lèi)型獲取對(duì)應(yīng)的字典列表代碼。
BLLFactory<DictData>.Instance.GetDictByDictType(dictTypeName);
有了這些數(shù)據(jù)的獲取方法,我們就可以通過(guò)擴(kuò)展函數(shù)來(lái)進(jìn)一步擴(kuò)展我們綁定字典類(lèi)別的方式了,如下擴(kuò)展函數(shù)所示。
/// <summary>
/// 綁定下拉列表控件為指定的數(shù)據(jù)字典列表
/// </summary>
/// <param name="combo">下拉列表控件</param>
/// <param name="dictTypeName">數(shù)據(jù)字典類(lèi)型名稱(chēng)</param>
/// <param name="defaultValue">控件默認(rèn)值</param>
public static void BindDictItems(this ComboBoxEdit combo, string dictTypeName, string defaultValue)
{
Dictionary<string, string> dict = BLLFactory<DictData>.Instance.GetDictByDictType(dictTypeName);
List<CListItem> itemList = new List<CListItem>();
foreach (string key in dict.Keys)
{
itemList.Add(new CListItem(key, dict[key]));
}
BindDictItems(combo, itemList, defaultValue);
}
使用的時(shí)候,就非常簡(jiǎn)單了,如下代碼是實(shí)際項(xiàng)目中對(duì)字典列表綁定的操作,字典數(shù)據(jù)在字典模塊里面統(tǒng)一定義的。
/// <summary>
/// 初始化數(shù)據(jù)字典
/// </summary>
private void InitDictItem()
{
txtInDiagnosis.BindDictItems("入院診斷");
txtLeaveDiagnosis.BindDictItems("最后診斷");
//初始化代碼
this.txtFollowType.BindDictItems("隨訪方式");
this.txtFollowStatus.BindDictItems("隨訪狀態(tài)");
}
這樣就非常簡(jiǎn)化了我們對(duì)字典數(shù)據(jù)源的綁定操作了,非常方便易讀,下面是其中一個(gè)功能界面的下拉列表展示。

2、使用緩存提高界面響應(yīng)速度
前面介紹了通過(guò)擴(kuò)展函數(shù)來(lái)進(jìn)一步擴(kuò)展我們綁定字典類(lèi)別的方式了,如下擴(kuò)展函數(shù)所示。
/// <summary>
/// 綁定下拉列表控件為指定的數(shù)據(jù)字典列表
/// </summary>
/// <param name="combo">下拉列表控件</param>
/// <param name="dictTypeName">數(shù)據(jù)字典類(lèi)型名稱(chēng)</param>
/// <param name="defaultValue">控件默認(rèn)值</param>
public static void BindDictItems(this ComboBoxEdit combo, string dictTypeName, string defaultValue)
{
Dictionary<string, string> dict = BLLFactory<DictData>.Instance.GetDictByDictType(dictTypeName);
List<CListItem> itemList = new List<CListItem>();
foreach (string key in dict.Keys)
{
itemList.Add(new CListItem(key, dict[key]));
}
BindDictItems(combo, itemList, defaultValue);
}
如果是基于服務(wù)接口的方式(通過(guò)Web API或者WCF方式)獲取字典列表,那么BLLFactory<T>的方式就修改為CallerFactory<T>的方式獲取數(shù)據(jù)了,如下擴(kuò)展函數(shù)所示。
/// <summary>
/// 綁定下拉列表控件為指定的數(shù)據(jù)字典列表
/// </summary>
/// <param name="control">下拉列表控件</param>
/// <param name="dictTypeName">數(shù)據(jù)字典類(lèi)型名稱(chēng)</param>
/// <param name="defaultValue">控件默認(rèn)值</param>
/// <param name="emptyFlag">是否添加空行</param>
public static void BindDictItems(this ComboBoxEdit control, string dictTypeName, string defaultValue, bool emptyFlag = true)
{
Dictionary<string, string> dict = CallerFactory<IDictDataService>.Instance.GetDictByDictType(dictTypeName);
List<CListItem> itemList = new List<CListItem>();
foreach (string key in dict.Keys)
{
itemList.Add(new CListItem(key, dict[key]));
}
control.BindDictItems(itemList, defaultValue, emptyFlag);
}
也就是通過(guò)服務(wù)接口工廠方法調(diào)用
CallerFactory<IDictDataService>.Instance.GetDictByDictType(dictTypeName);
而獲取數(shù)據(jù)字典列表的內(nèi)容,這個(gè)可以配置為Web API訪問(wèn)方式、WCF訪問(wèn)方式,底層就是調(diào)用客戶端封裝的代理方法獲取就是了。例如對(duì)于Web API調(diào)用來(lái)說(shuō)就是通過(guò)客戶端直接訪問(wèn)Web API服務(wù)接口獲取數(shù)據(jù)的,實(shí)現(xiàn)代碼如下所示。
/// <summary>
/// 根據(jù)字典類(lèi)型名稱(chēng)獲取所有該類(lèi)型的字典列表集合(Key為名稱(chēng),Value為值)
/// </summary>
/// <param name="dictTypeName">字典類(lèi)型名稱(chēng)</param>
/// <returns></returns>
public Dictionary<string, string> GetDictByDictType(string dictTypeName)
{
var action = System.Reflection.MethodBase.GetCurrentMethod().Name;
string url = GetTokenUrl(action) + string.Format("&dictTypeName={0}", dictTypeName.UrlEncode());
Dictionary<string, string> result = JsonHelper<Dictionary<string, string>>.ConvertJson(url);
return result;
}
由于字典數(shù)據(jù)是相對(duì)比較固定的,一般時(shí)效不是那么及時(shí)都沒(méi)問(wèn)題,由于這部分?jǐn)?shù)據(jù)是通過(guò)網(wǎng)絡(luò)的方式獲取的,反復(fù)的調(diào)用獲取是會(huì)耗費(fèi)一定的時(shí)間。
為了提高用戶響應(yīng)速度,我們可以把它放到客戶端的緩存里面(非服務(wù)器緩存),設(shè)置一定的失效時(shí)間,在失效時(shí)間內(nèi),我們數(shù)據(jù)不再反復(fù)的從網(wǎng)絡(luò)接口獲取,而是直接通過(guò)緩存里面提取,速度非???,同時(shí)也提高了界面響應(yīng)速度。
但是為了不影響已有代碼,我們可以繼續(xù)在擴(kuò)展函數(shù)的實(shí)現(xiàn)上做一些擴(kuò)展即可,首先我們定義一個(gè)公共的獲取字典數(shù)據(jù)的方法,如下所示。
/// <summary>
/// 獲取字典類(lèi)型的通用處理
/// </summary>
/// <param name="dictTypeName">字典類(lèi)型</param>
/// <param name="isCache">是否緩存,默認(rèn)為true</param>
/// <returns></returns>
private static Dictionary<string, string> GetDictByDictType(string dictTypeName, bool isCache = true)
{
Dictionary<string, string> dict = null;
if (isCache)
{
System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod();
string key = string.Format("{0}-{1}-{2}", method.DeclaringType.FullName, method.Name, dictTypeName);
dict = MemoryCacheHelper.GetCacheItem<Dictionary<string, string>>(key,
delegate () {
return CallerFactory<IDictDataService>.Instance.GetDictByDictType(dictTypeName);
},
new TimeSpan(0, 30, 0));//30分鐘過(guò)期
}
else
{
dict = CallerFactory<IDictDataService>.Instance.GetDictByDictType(dictTypeName);
}
return dict;
}
通過(guò)使用 MemoryCacheHelper.GetCacheItem<Dictionary<string, string>> 的方式,我們可以把它設(shè)置為緩存處理方式,如果在失效時(shí)間內(nèi),則從緩存里面提取。
這樣原來(lái)的綁定下拉列表的擴(kuò)展方法獲取字典數(shù)據(jù),從這個(gè)公共的接口里面獲取即可,而我們也僅僅是增加一個(gè)具有默認(rèn)值的緩存與否的參數(shù),用來(lái)決定是否使用緩存模式,默認(rèn)為使用緩存處理。
/// <summary>
/// 綁定下拉列表控件為指定的數(shù)據(jù)字典列表
/// </summary>
/// <param name="control">下拉列表控件</param>
/// <param name="dictTypeName">數(shù)據(jù)字典類(lèi)型名稱(chēng)</param>
/// <param name="defaultValue">控件默認(rèn)值</param>
/// <param name="emptyFlag">是否添加空行</param>
public static void BindDictItems(this ComboBoxEdit control, string dictTypeName, string defaultValue, bool isCache = true, bool emptyFlag = true)
{
var dict = GetDictByDictType(dictTypeName, isCache);
List<CListItem> itemList = new List<CListItem>();
foreach (string key in dict.Keys)
{
itemList.Add(new CListItem(key, dict[key]));
}
control.BindDictItems(itemList, defaultValue, emptyFlag);
}
這樣原來(lái)的數(shù)據(jù)下拉列表綁定的方式?jīng)]有變化,依舊是我們?cè)瓉?lái)的代碼,但是默認(rèn)采用緩存方式來(lái)綁定基于網(wǎng)絡(luò)接口(混合框架模式)獲取的字典數(shù)據(jù)。
/// <summary>
/// 初始化數(shù)據(jù)字典
/// </summary>
private void InitDictItem()
{
//初始化代碼
this.txtSurgeryType.BindDictItems("手術(shù)方式");
this.txtIsFirstTime.BindDictItems("首發(fā)");
this.txtWHOGrade.BindDictItems("病理WHO分級(jí)");
this.txtLesionPart.BindDictItems("病灶部位");
this.txtOccupation.BindDictItems("病人職業(yè)");
this.txtRelapse.BindDictItems("復(fù)發(fā)");
this.txtPathologyGrade.BindDictItems("病理分級(jí)");
this.txtSymptom.BindDictItems("初發(fā)癥狀");
this.txtAnesthesiaMethod.BindDictItems("麻醉方法");
this.txtSpecimenDetail.BindDictItems("具體標(biāo)本情況");
}
得到的編輯界面如下所示,使用緩存接口,對(duì)于大量字典數(shù)據(jù)顯示的界面,界面顯示速度有了不錯(cuò)的提升。

而對(duì)于一些特殊列表的字典顯示,如需要通過(guò)拼音首字母進(jìn)行檢索功能的下拉列表,我們依舊可以使用這種綁定的方式實(shí)現(xiàn)緩存處理的。
如字典綁定的擴(kuò)展函數(shù)如下所示,這樣就統(tǒng)一了整個(gè)字典列表的綁定操作,比較容易記住。
/// <summary>
/// 綁定下拉列表控件為指定的數(shù)據(jù)字典列表
/// </summary>
/// <param name="combo">下拉列表控件</param>
/// <param name="dictTypeName">數(shù)據(jù)字典類(lèi)型名稱(chēng)</param>
/// <param name="defaultValue">控件默認(rèn)值</param>
public static void BindDictItems(this CustomGridLookUpEdit combo, string dictTypeName, string defaultValue, bool isCache = true)
{
string displayName = dictTypeName;
const string valueName = "值內(nèi)容";
const string pinyin = "拼音碼";
var dt = DataTableHelper.CreateTable(string.Format("{0},{1},{2}", displayName, valueName, pinyin));
var dict = GetDictByDictType(dictTypeName, isCache);
foreach (string key in dict.Keys)
{
var row = dt.NewRow();
row[displayName] = key;
row[valueName] = dict[key];
row[pinyin] = Pinyin.GetFirstPY(key);
dt.Rows.Add(row);
}
combo.Properties.ValueMember = valueName;
combo.Properties.DisplayMember = displayName;
combo.Properties.DataSource = dt;
combo.Properties.PopulateViewColumns();
combo.Properties.View.Columns[valueName].Visible = false;
combo.Properties.View.Columns[displayName].Width = 400;
combo.Properties.View.Columns[pinyin].Width = 200;
combo.Properties.PopupFormMinSize = new System.Drawing.Size(600, 0);
if (!string.IsNullOrEmpty(defaultValue))
{
combo.EditValue = defaultValue;
}
}
界面效果如下所示。

以上就是常規(guī)單機(jī)版數(shù)據(jù)綁定操作,以及基于網(wǎng)絡(luò)版緩存數(shù)據(jù)的數(shù)據(jù)字典綁定操作,我們?cè)诮缑娲a的處理上沒(méi)有任何差異,只是輔助擴(kuò)展函數(shù)做一些調(diào)整就可以很好的變化過(guò)來(lái)了,這樣對(duì)于我們界面代碼的重用或者調(diào)整是非常便利的,同時(shí)緩存的使用,對(duì)于網(wǎng)絡(luò)性能有所差異的地方,速度也會(huì)明細(xì)的有所提高。以上就是對(duì)于字典模塊的一些處理上的分享,希望對(duì)大家開(kāi)發(fā)Winform界面代碼有所幫助和啟發(fā)。