How to use caching in WCF data access to improve the display speed of WinForm fields in Chinese

Source: Internet
Author: User

This article describes in more detail the use of cache in WCF data access to improve the speed of WinForm field display in Chinese, share for everyone for reference. Here's how:

When we develop the WinForm program based on the WCF access method, it is generally necessary to parse the display of the interface in Chinese. If it is hard-coded to display in Chinese, then in addition to inconvenient adjustment and code bloat, there is no problem in performance, but it is not recommended; In general, we put the Chinese control information into the business class to unify the analysis, However, this results in a certain amount of response time for the culture operation in each WCF access request resolution. If you use the table of text segments in cache storage, you do not need to request WCF data access every time, reducing the consumption of some response time and improving the user experience.


1, the use of hard-coded methods of cultural analysis of the operation

Hard-coded way, the operation of the culture field is done locally, the general response will be faster, as shown in the following code.

public void Binddata () {#region Add alias Resolution This.winGridViewPager1.DisplayColumns = "id,user_id,loginname,fullname,note,  Ipaddress,macaddress,systemtype_id,lastupdated ";  This.winGridViewPager1.AddColumnAlias ("id", "number");  This.winGridViewPager1.AddColumnAlias ("user_id", "Login user ID");  This.winGridViewPager1.AddColumnAlias ("LoginName", "Login name");  This.winGridViewPager1.AddColumnAlias ("FullName", "real name");  This.winGridViewPager1.AddColumnAlias ("Note", "Log description");  This.winGridViewPager1.AddColumnAlias ("IPAddress", "IP Address");  This.winGridViewPager1.AddColumnAlias ("MacAddress", "MAC Address");  This.winGridViewPager1.AddColumnAlias ("LastUpdated", "Record Date");  This.winGridViewPager1.AddColumnAlias ("systemtype_id", "System type");  #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);} 

Just this way the elasticity is not very good, if the field is more, there are many such code in the interface, and if there are many such parsing, it is not good to control the consistency of resolving field names.


2, Chinese Culture Unified analysis operation

In order to overcome the disadvantage of the first scheme, we can move the Chinese cultural reference operation to the underlying DAL to achieve, the interface of a layer, only need to call it to parse (method Getcolumnnamealias) on it.

<summary>///binding list data//</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 ();//Field column Display name Escape  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 = "Member Information Report";}

After this processing, the unity of the resolution improved, the code also simplified a lot, basically achieved the effect we expect. But the only problem is that if it is WCF's way of accessing data, then each access will take a certain amount of processing time.

If we use the cache, the second time to get directly from the local, then the speed is much higher, especially when the table field reference objects more time, performance is more obvious.


3. Handling with cached operations

Because. NET provides a MemoryCache object for us to cache the processing, we use it can be very well implemented, for convenience, we can be used in a certain package after it.

First, we want to provide a generic processing function for the culture in the field after encapsulation, passing in the corresponding parameters. Therefore, a helper class is encapsulated first.

<SUMMARY>///provides cache handling for some common operations//</summary>public class cachedatautil<t> where t:baseentity{  / <summary>////  get aliases for the specified object///</summary>//  <typeparam name= "T" > entity class information </typeparam >//  <param name= "Service" > Interface service object </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 hours, i.e. expires after 1 days  }}

And then, in the main interface, the processing code for the paging control we are bound to is as follows.

<summary>///binding list data//</summary>private void Binddata () {  //entity  This.winGridViewPager1.DisplayColumns = Displaycolumns;  This.winGridViewPager1.ColumnNameAlias = Callerfactory<icustomerservice>. Instance.getcolumnnamealias ();///Field column Display name escape  //using the Cache storage table alias, can effectively increase the display speed  of two times This.winGridViewPager1.ColumnNameAlias = Cachedatautil<customerinfo>. Getcolumnnamealias (CALLERFACTORY<ICUSTOMERSERVICE>. Instance);//Field column Display name Escape  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 = "Customer Information list";}

Run as shown:

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
How to use caching in WCF data access to improve the display speed of WinForm fields in Chinese

This address: http://www.paobuke.com/develop/c-develop/pbk23537.html






Related Content C # multi-thread threading control in C # Delegate, event, and asynchronous C # implementation BASE64 processing of cryptographic decryption, encoding and decoding example using the @ Declaration variable example (verbatim identifier) in C #
C # Displays all picture files under folders in C #??? Úxa ¢ó? ¨?ê?ac# recursive generation XML Instance const usage in C #

How to use caching in WCF data access to improve the display speed of WinForm fields in Chinese

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.