使用简单的反射技术重构组合查询串功能

我现在需要做的一件事就是重构一下以前的Legacy code稍稍重构下,那代码非常不容易读,做的事就是高级查询,用户选什么就提取什么,然后使用”|“符号分隔并入库,这样的话,就有很多个不同的组合,维护起来不容易而且代码重复性很高,我只是想小小的重构一下,重做了这个类,用了点反射的技术。

1.  我定义了一个枚举存放高级查询的组合类别,比如”高级查询“和”简易查询“;
2. 使用Generic List将对应的属性名称存进组合类别对应的函数中
3. 定义ToString(),使用反射得到组合中的属性字符串对应的真实值,并组合生成bar-separated string
4.在构造函数或对象初始器(Object Initializer)中,设定相对应组合中的属性字符串对应的属性值
5.使用LINQ的ForEach遍历List值(再一次让我想到jQuery中的each,类似)

Code Snippet
  1. public class UserSearch
  2. {
  3.     private const char _delimetor = '|';
  4.     public enum SearchMode
  5.     {
  6.         SimpleSearch,
  7.         AdvancedSearch
  8.     }
  9.  
  10.     public bool IsAdvancedSearch { get; set; }
  11.  
  12.     // Items (leave all properties 'string' type)
  13.     // ETODO: make it generic in Type
  14.     public string ProductFamily { get; set; }
  15.     public string ProductGroup { get; set; }
  16.     public string ProductSubGroup { get; set; }
  17.     public string ProductName { get; set; }
  18.     public string DocNumber { get; set; }
  19.     public string DocTitle { get; set; }
  20.     public string DocType { get; set; }
  21.     public string ReleaseDTOp { get; set; }
  22.     public string ReleaseDTStartString { get; set; }
  23.     public string ReleaseDTEndString { get; set; }
  24.     public string ContentSearch { get; set; }
  25.     public string SearchTypeIdString { get; set; }
  26.     public string SearchText { get; set; }
  27.  
  28.     public UserSearch()
  29.     {
  30.     }
  31.  
  32.     /// <summary>
  33.     /// Note: sequence and nameings are important
  34.     /// </summary>
  35.     private static List<string> ClassPropertiesForAdvancedSearch
  36.     {
  37.         get
  38.         {
  39.             return new List<string>()
  40.                        {
  41.                            "ProductFamily",
  42.                            "ProductGroup",
  43.                            "ProductName",
  44.                            "DocNumber",
  45.                            "DocTitle",
  46.                            "DocType",
  47.                            "ReleaseDTOp",
  48.                            "ReleaseDTStartString",
  49.                            "ReleaseDTEndString",
  50.                            "ProductSubGroup",
  51.                            "ContentSearch"
  52.                        };
  53.         }
  54.     }
  55.  
  56.     /// <summary>
  57.     /// Note: sequence and nameings are important
  58.     /// </summary>
  59.     private static List<string> ClassPropertiesForSimpleSearch
  60.     {
  61.         get
  62.         {
  63.             return new List<string>()
  64.                        {
  65.                            "SearchTypeIdString",
  66.                            "SearchText"
  67.                        };
  68.         }
  69.     }
  70.  
  71.     private static List<string> GetListBySearchMode(SearchMode searchMode)
  72.     {
  73.         List<string> properties;
  74.         switch ((int)searchMode)
  75.         {
  76.             case (int)SearchMode.SimpleSearch:
  77.                 properties = ClassPropertiesForSimpleSearch;
  78.                 break;
  79.  
  80.             case (int)SearchMode.AdvancedSearch:
  81.                 properties = ClassPropertiesForAdvancedSearch;
  82.                 break;
  83.  
  84.             default:
  85.                 properties = new List<string>();
  86.                 break;
  87.         }
  88.  
  89.         return properties;
  90.     }
  91.  
  92.     /// <summary>
  93.     /// Get bar-separated UIString from UserSearch object
  94.     /// </summary>
  95.     /// <returns>bar-separated UIString</returns>
  96.     /// <remarks>
  97.     /// ETODO: refactor 'FrDocument.aspx.cs' -> BtnSearch_Click
  98.     /// </remarks>
  99.     public string GetUIString(SearchMode searchMode)
  100.     {
  101.         string userSearchString = string.Empty;
  102.  
  103.         List<string> properties = GetListBySearchMode(searchMode);
  104.         properties.ForEach(d =>
  105.         {
  106.             var property = this.GetType().GetProperty(d);
  107.             userSearchString += "|" + property.GetValue(this, null).ToString();
  108.         });
  109.  
  110.         return userSearchString.Trim(_delimetor);
  111.     }
  112.  
  113.     /// <summary>
  114.     /// Get UserSearch object from UIString (bar-separated string)
  115.     /// </summary>
  116.     /// <returns>UserSearch</returns>
  117.     public static UserSearch GetFromUIString(SearchMode searchMode, string UIString)
  118.     {
  119.         UIString = UIString.Trim(_delimetor);
  120.  
  121.         UserSearch userSearch = new UserSearch();
  122.         string[] ar = UIString.Split(_delimetor);
  123.         List<string> properties = GetListBySearchMode(searchMode);
  124.  
  125.         int i = 0;
  126.         properties.ForEach(d =>
  127.         {
  128.             var property = userSearch.GetType().GetProperty(d);
  129.             property.SetValue(userSearch, ar[i], null);
  130.  
  131.             i++;
  132.         });
  133.  
  134.         return userSearch;
  135.     }

设置完后,如下使用:

Code Snippet
  1. if (ActiveNodePrefix == WebLib.DownloadMenuItem.SimpleSearchPrefix)
  2. {
  3.     string searchString = ActiveNode.Substring(ActiveNode.IndexOf('|'), ActiveNode.Length - ActiveNode.IndexOf('|'));
  4.  
  5.     UserSearch userSearch = UserSearch.GetFromUIString(
  6.         UserSearch.SearchMode.SimpleSearch,
  7.         searchString);
  8.     string SearchSelectionString = userSearch.SearchTypeIdString;
  9.     searchSelectionId = SearchSelectionString == "" ? 0 : Convert.ToInt32(SearchSelectionString);
  10.     string SearchString = userSearch.SearchText;
  11.  
  12.     List<xSearchType> selectTypes = UserSearch.GetSearchTypesList(true);
  13.  
  14.     foreach (var selectType in selectTypes)
  15.     {
  16.         if (searchSelectionId == selectType.SearchTypeId)
  17.         {
  18.             LblTitle.Text = string.Format("Search {0}: {1}", selectType.SearchTypeName, SearchString + "");
  19.             break;
  20.         }
  21.     }



这么怎么着都比以前看着清爽容易理解了,而且维护接口集中 Smile
其实还可以优化,将属性存为枚举,这样以后字符串有改动,比较容易track,有米有更好的解决方案呢?

原文地址:https://www.cnblogs.com/syveen/p/1972269.html