使用简单的反射技术重构组合查询串功能
2011-03-06 14:15
316 查看
我现在需要做的一件事就是重构一下以前的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,有米有更好的解决方案呢?
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,有米有更好的解决方案呢?
相关文章推荐
- java反射技术的简单使用
- java反射技术的简单使用
- 使用JAVA反射技术实现代码零耦合与功能无限扩展!
- PHP实现依赖注入-使用反射机制和动态代理技术 - 简单思想(咋个办呢 zgbn)
- 黑马程序员-对eclipse代码补全功能原理的猜想及实验(对反射技术的一些使用)
- Java反射的简单使用
- SqlHelper简单实现(通过Expression和反射)10.使用方式
- 组合使用容器可以获得比multimap更多的功能
- 使用反射技术操作方法所有细节
- SpringMVC(26):简单的json数据校验示例(实现功能:使用jQuery框架的ajax()方法来实现 userCode的校验)
- Touch ID和Keychain的简单组合使用
- 使用java反射技术完成对象所有属性值的输出
- SQL SERVER 2005 同步复制技术 发布与订阅功能使用说明
- 使用Ajax技术通过XMLHttpRequest对象完成首页登录功能
- .net中使用反射的简单例子
- 使用UIScrollView和UIPageControl实现一个简单的图片浏览功能
- 使用Java Socket实现简单的聊天功能
- 使用反射技术获取指定类型中方法的完整信息
- java反射的简单使用
- 15_传智播客Spring2.5视频教程_使用JDK中的Proxy技术实现AOP功能