我现在需要做的一件事就是重构一下以前的Legacy code稍稍重构下,那代码非常不容易读,做的事就是高级查询,用户选什么就提取什么,然后使用”|“符号分隔并入库,这样的话,就有很多个不同的组合,维护起来不容易而且代码重复性很高,我只是想小小的重构一下,重做了这个类,用了点反射的技术。
1. 我定义了一个枚举存放高级查询的组合类别,比如”高级查询“和”简易查询“;
2. 使用Generic List将对应的属性名称存进组合类别对应的函数中
3. 定义ToString(),使用反射得到组合中的属性字符串对应的真实值,并组合生成bar-separated string
4.在构造函数或对象初始器(Object Initializer)中,设定相对应组合中的属性字符串对应的属性值
5.使用LINQ的ForEach遍历List值(再一次让我想到jQuery中的each,类似)
Code Snippet
- public class UserSearch
- {
- private const char _delimetor = '|';
- public enum SearchMode
- {
- SimpleSearch,
- AdvancedSearch
- }
- public bool IsAdvancedSearch { get; set; }
- // Items (leave all properties 'string' type)
- // ETODO: make it generic in Type
- public string ProductFamily { get; set; }
- public string ProductGroup { get; set; }
- public string ProductSubGroup { get; set; }
- public string ProductName { get; set; }
- public string DocNumber { get; set; }
- public string DocTitle { get; set; }
- public string DocType { get; set; }
- public string ReleaseDTOp { get; set; }
- public string ReleaseDTStartString { get; set; }
- public string ReleaseDTEndString { get; set; }
- public string ContentSearch { get; set; }
- public string SearchTypeIdString { get; set; }
- public string SearchText { get; set; }
- public UserSearch()
- {
- }
- /// <summary>
- /// Note: sequence and nameings are important
- /// </summary>
- private static List<string> ClassPropertiesForAdvancedSearch
- {
- get
- {
- return new List<string>()
- {
- "ProductFamily",
- "ProductGroup",
- "ProductName",
- "DocNumber",
- "DocTitle",
- "DocType",
- "ReleaseDTOp",
- "ReleaseDTStartString",
- "ReleaseDTEndString",
- "ProductSubGroup",
- "ContentSearch"
- };
- }
- }
- /// <summary>
- /// Note: sequence and nameings are important
- /// </summary>
- private static List<string> ClassPropertiesForSimpleSearch
- {
- get
- {
- return new List<string>()
- {
- "SearchTypeIdString",
- "SearchText"
- };
- }
- }
- private static List<string> GetListBySearchMode(SearchMode searchMode)
- {
- List<string> properties;
- switch ((int)searchMode)
- {
- case (int)SearchMode.SimpleSearch:
- properties = ClassPropertiesForSimpleSearch;
- break;
- case (int)SearchMode.AdvancedSearch:
- properties = ClassPropertiesForAdvancedSearch;
- break;
- default:
- properties = new List<string>();
- break;
- }
- return properties;
- }
- /// <summary>
- /// Get bar-separated UIString from UserSearch object
- /// </summary>
- /// <returns>bar-separated UIString</returns>
- /// <remarks>
- /// ETODO: refactor 'FrDocument.aspx.cs' -> BtnSearch_Click
- /// </remarks>
- public string GetUIString(SearchMode searchMode)
- {
- string userSearchString = string.Empty;
- List<string> properties = GetListBySearchMode(searchMode);
- properties.ForEach(d =>
- {
- var property = this.GetType().GetProperty(d);
- userSearchString += "|" + property.GetValue(this, null).ToString();
- });
- return userSearchString.Trim(_delimetor);
- }
- /// <summary>
- /// Get UserSearch object from UIString (bar-separated string)
- /// </summary>
- /// <returns>UserSearch</returns>
- public static UserSearch GetFromUIString(SearchMode searchMode, string UIString)
- {
- UIString = UIString.Trim(_delimetor);
- UserSearch userSearch = new UserSearch();
- string[] ar = UIString.Split(_delimetor);
- List<string> properties = GetListBySearchMode(searchMode);
- int i = 0;
- properties.ForEach(d =>
- {
- var property = userSearch.GetType().GetProperty(d);
- property.SetValue(userSearch, ar[i], null);
- i++;
- });
- return userSearch;
- }
设置完后,如下使用:
Code Snippet
- if (ActiveNodePrefix == WebLib.DownloadMenuItem.SimpleSearchPrefix)
- {
- string searchString = ActiveNode.Substring(ActiveNode.IndexOf('|'), ActiveNode.Length - ActiveNode.IndexOf('|'));
- UserSearch userSearch = UserSearch.GetFromUIString(
- UserSearch.SearchMode.SimpleSearch,
- searchString);
- string SearchSelectionString = userSearch.SearchTypeIdString;
- searchSelectionId = SearchSelectionString == "" ? 0 : Convert.ToInt32(SearchSelectionString);
- string SearchString = userSearch.SearchText;
- List<xSearchType> selectTypes = UserSearch.GetSearchTypesList(true);
- foreach (var selectType in selectTypes)
- {
- if (searchSelectionId == selectType.SearchTypeId)
- {
- LblTitle.Text = string.Format("Search {0}: {1}", selectType.SearchTypeName, SearchString + "");
- break;
- }
- }
这么怎么着都比以前看着清爽容易理解了,而且维护接口集中 其实还可以优化,将属性存为枚举,这样以后字符串有改动,比较容易track,有米有更好的解决方案呢?