如何重构具有一个行差异的这些函数

时间:2011-02-02 22:34:50

标签: c#

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

3 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:7)

传入Func<string, string>以获取与给定名称相关联的值。在第一种情况下,只使用nvp的索引器;在第二个它会使用会话。您可以使用单独的方法来创建委托,也可以使用lambda表达式。 (我肯定会为第一个使用lambda表达式;我可能会为第二个使用单独的方法。)

答案 2 :(得分:2)

在我看来,最好的解决方案是使用lambda表达式。

而不是函数的第二个参数,将lambda放在string name转换为string value

相关问题