在WCF數(shù)據(jù)訪問中使用緩存提高Winform字段中文顯示速度的方法
本文較為詳細(xì)的講述了在WCF數(shù)據(jù)訪問中使用緩存提高Winform字段中文顯示速度的方法,分享給大家供大家參考之用。具體方法如下:
在我們開發(fā)基于WCF訪問方式的Winform程序的時(shí)候,一般情況下需要對(duì)界面顯示的字段進(jìn)行中文顯示的解析。如果是硬編碼進(jìn)行中文顯示,那么除了不方便調(diào)整及代碼臃腫外,性能上沒有什么問題,但是不建議這樣處理;一般情況下,我們把中文對(duì)照信息放到業(yè)務(wù)類里面去統(tǒng)一解析,但是這樣會(huì)導(dǎo)致每次WCF訪問方式請(qǐng)求解析中文化的操作耗費(fèi)一定的響應(yīng)時(shí)間。如果使用緩存存儲(chǔ)中文字段的對(duì)照表,那么就不用每次請(qǐng)求WCF的數(shù)據(jù)訪問,減少一些響應(yīng)時(shí)間的消耗,提高用戶體驗(yàn)效果。
1、使用硬編碼方式的中文化解析操作
硬編碼的方式,中文化字段的操作,是在本地進(jìn)行的,一般響應(yīng)會(huì)比較快,如下代碼所示。
public void BindData()
{
#region 添加別名解析
this.winGridViewPager1.DisplayColumns = "ID,User_ID,LoginName,FullName,Note,IPAddress,MacAddress,SystemType_ID,LastUpdated";
this.winGridViewPager1.AddColumnAlias("ID", "編號(hào)");
this.winGridViewPager1.AddColumnAlias("User_ID", "登錄用戶ID");
this.winGridViewPager1.AddColumnAlias("LoginName", "登錄名");
this.winGridViewPager1.AddColumnAlias("FullName", "真實(shí)名稱");
this.winGridViewPager1.AddColumnAlias("Note", "日志描述");
this.winGridViewPager1.AddColumnAlias("IPAddress", "IP地址");
this.winGridViewPager1.AddColumnAlias("MacAddress", "Mac地址");
this.winGridViewPager1.AddColumnAlias("LastUpdated", "記錄日期");
this.winGridViewPager1.AddColumnAlias("SystemType_ID", "系統(tǒng)類型");
#endregion
string where = GetConditionSql();
PagerInfo pagerInfo = this.winGridViewPager1.PagerInfo;
List<LoginLogInfo> list = CallerFactory<ILoginLogService>.Instance.FindWithPager(where, ref pagerInfo);
this.winGridViewPager1.DataSource = new WHC.Pager.WinControl.SortableBindingList<LoginLogInfo>(list);
}
只是這種方式彈性化不太好,如果字段比較多,在界面里面就有很多這樣的代碼,而且如果多處有這樣的解析,就不好控制解析字段名稱的一致性。
2、中文化統(tǒng)一解析操作
為了克服第一種方案的弊端,我們可以把中文化參考的操作移到底層DAL去實(shí)現(xiàn),高一層的接口,只需要調(diào)用它進(jìn)行解析(方法GetColumnNameAlias)就可以了。
/// <summary>
/// 綁定列表數(shù)據(jù)
/// </summary>
private void BindData()
{
this.winGridViewPager1.DisplayColumns = "HandNo,CardNo,CardStatus,CardGrade,Name,Sex,Telephone,Mobile,OpenDate,ValidateDate,Discount,Balance,MaxCount,Creator,CreateTime";
this.winGridViewPager1.ColumnNameAlias = CallerFactory<IMemberService>.Instance.GetColumnNameAlias();//字段列顯示名稱轉(zhuǎn)義
string where = GetConditionSql();
List<MemberInfo> list = CallerFactory<IMemberService>.Instance.FindWithPager(where, this.winGridViewPager1.PagerInfo);
this.winGridViewPager1.DataSource = new WHC.Pager.WinControl.SortableBindingList<MemberInfo>(list);
this.winGridViewPager1.PrintTitle = "會(huì)員信息報(bào)表";
}
這樣處理后,解析的統(tǒng)一性提高了,代碼也簡(jiǎn)化了很多,基本上達(dá)到了我們期望的效果。但是唯一的問題就是如果是WCF的數(shù)據(jù)訪問方式,那么每次訪問都會(huì)耗費(fèi)一定的處理時(shí)間。
如果我們使用緩存,第二次直接從本地獲取,那么速度會(huì)提高很多,特別是表的字段參照對(duì)象比較多的時(shí)候,性能提高更加明顯。
3、使用緩存的操作處理
由于.NET提供了MemoryCache對(duì)象給我們進(jìn)行緩存的處理,我們利用它就可以很好實(shí)現(xiàn)了,為了方便,我們可以對(duì)它進(jìn)行一定的封裝后在使用。
首先,我們希望封裝后提供一個(gè)通用的對(duì)字段中文化的處理函數(shù),傳入相應(yīng)的參數(shù)就可以了。因此先封裝好一個(gè)輔助類。
/// <summary>
/// 提供一些常見操作的緩存處理
/// </summary>
public class CacheDataUtil<T> where T : BaseEntity
{
/// <summary>
/// 獲取指定對(duì)象的別名
/// </summary>
/// <typeparam name="T">實(shí)體類信息</typeparam>
/// <param name="service">接口服務(wù)對(duì)象</param>
/// <returns></returns>
public static Dictionary<string, string> GetColumnNameAlias(IBaseService<T> service)
{
System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod();
string key = string.Format("{0}-{1}-{2}", method.DeclaringType.FullName, method.Name, typeof(T).Name);
return MemoryCacheHelper.GetCacheItem<Dictionary<string, string>>(
key,
delegate() { return service.GetColumnNameAlias(); },
new TimeSpan(24, 0, 0));//24小時(shí),即1天后過期
}
}
然后在主體界面里面,我們綁定分頁控件的處理代碼如下所示即可。
/// <summary>
/// 綁定列表數(shù)據(jù)
/// </summary>
private void BindData()
{
//entity
this.winGridViewPager1.DisplayColumns = displayColumns;
//this.winGridViewPager1.ColumnNameAlias = CallerFactory<ICustomerService>.Instance.GetColumnNameAlias();//字段列顯示名稱轉(zhuǎn)義
//使用緩存存儲(chǔ)表的別名,可以有效提高二次顯示速度
this.winGridViewPager1.ColumnNameAlias = CacheDataUtil<CustomerInfo>.GetColumnNameAlias(CallerFactory<ICustomerService>.Instance);//字段列顯示名稱轉(zhuǎn)義
string where = GetConditionSql();
PagerInfo pagerInfo = this.winGridViewPager1.PagerInfo;
List<CustomerInfo> list = CallerFactory<ICustomerService>.Instance.FindWithPager(where, ref pagerInfo);
this.winGridViewPager1.DataSource = new WHC.Pager.WinControl.SortableBindingList<CustomerInfo>(list);
this.winGridViewPager1.PrintTitle = "客戶信息列表";
}
運(yùn)行如下圖所示:

相關(guān)文章
C# BeginInvoke實(shí)現(xiàn)異步編程方式
這篇文章主要介紹了C# BeginInvoke實(shí)現(xiàn)異步編程方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
WPF自定義控件實(shí)現(xiàn)ItemsControl魚眼效果
這篇文章主要為大家詳細(xì)介紹了WPF如何通過自定義控件實(shí)現(xiàn)ItemsControl魚眼效果,文中的示例代碼講解詳細(xì),需要的可以參考一下2024-01-01

