2011-02-02 168 views
14

我有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; 
} 
+0

這個問題很簡單,但我對於許多初學者來說,這對他們很有用+1 – 2011-02-02 22:44:00

回答

12

你可以修改函數採取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); 
} 
+0

是的,就是這樣。正是我上面寫的。 :-) – 2011-02-02 22:42:40

7

傳入Func<string, string>以獲取與給定名稱關聯的值。在第一種情況下,只會使用nvp的索引器;在第二個會使用會話。您可以使用單獨的方法來創建委託或lambda表達式。 (我肯定會爲第一個使用lambda表達式;我可能會爲第二個使用單獨的方法)。

2

在我看來,最好的解決方案是使用lambda表達式。

而不是你的函數的第二個參數,把那裏lambda將變換string namestring value