内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/4880719/how-to-refactor-these-functions-which-have-one-line-difference
我有3个功能,其中唯一的区别是我指出的值与评论
//-- point of difference
所有这三个功能的大部分功能是一样的. “干”因素正在困扰我的睡眠:).我在想;这些可以容易地和可读地并入吗?
我以前有这样的情况,我希望在这里学习东西.
private string RenderRequestType(string render, NameValueCollection nvp, string prefix, string regexWild, string suffix) { string regex = prefix + regexWild + suffix; MatchCollection matches = Regex.Matches(render, regex); foreach (Match match in matches) { foreach (Capture capture in match.Captures) { string name = capture.Value.Replace(prefix, "", StringComparison.CurrentCultureIgnoreCase).Replace(suffix, "", StringComparison.CurrentCultureIgnoreCase); //-- point of difference string value = nvp[name]; render = render.Replace(capture.Value, value); } } return render; } private string RenderSessionType(string render, HttpContext httpContext, string prefix, string regexWild, string suffix) { string regex = prefix + regexWild + suffix; MatchCollection matches = Regex.Matches(render, regex); foreach (Match match in matches) { foreach (Capture capture in match.Captures) { string name = capture.Value.Replace(prefix, "", StringComparison.CurrentCultureIgnoreCase).Replace(suffix, "", StringComparison.CurrentCultureIgnoreCase); //-- point of difference object session = httpContext.Session[name]; string value = (session != null ? session.ToString() : ""); render = render.Replace(capture.Value, value); } } return render; } private string RenderCookieType(string render, HttpContext httpContext, string prefix, string regexWild, string suffix) { string regex = prefix + regexWild + suffix; MatchCollection matches = Regex.Matches(render, regex); foreach (Match match in matches) { foreach (Capture capture in match.Captures) { string name = capture.Value.Replace(prefix, "", StringComparison.CurrentCultureIgnoreCase).Replace(suffix, "", StringComparison.CurrentCultureIgnoreCase); //-- point of difference HttpCookie cookie = httpContext.Request.Cookies[name]; string value = (cookie != null ? cookie.Value : ""); render = render.Replace(capture.Value, value); } } return render; }
您可以修改函数以获取Func<
string,string>做查找:
private string RenderType(string render, Func<string, string> lookupFunc, string prefix, string regexWild, string suffix) { string regex = prefix + regexWild + suffix; MatchCollection matches = Regex.Matches(render, regex); foreach (Match match in matches) { foreach (Capture capture in match.Captures) { string name = capture.Value.Replace(prefix, "", StringComparison.CurrentCultureIgnoreCase).Replace(suffix, "", StringComparison.CurrentCultureIgnoreCase); //-- point of difference string value = lookupFunc(name); render = render.Replace(capture.Value, value); } } return render; }
然后根据这个功能编写你的功能,例如:
private string RenderRequestType(string render, NameValueCollection nvp, string prefix, string regexWild, string suffix) { return RenderType(render, name => nvp[name], prefix, regexWild, suffix); }
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/4880719/how-to-refactor-these-functions-which-have-one-line-difference
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Markdown 在线编辑器
Markdown 在线编辑器
HSV CMYK 转换工具
HSV CMYK互换工具