如何重构此代码以删除重复的代码

时间:2015-06-12 02:37:52

标签: c# refactoring

我正在写一个c#程序,我有以下方法。 我看到我在这个方法中多次编写相同的代码。我无法将代码分解为可重用的部分。有人可以看一看并提出建议吗?

private static IOrderedQueryable<T> FormatQuery<T>(IQueryable<T> query, Expression property, ParameterExpression sortParam, SortFilter sortFilter, ref IOrderedQueryable<T> orderedQuery)
{
    if (property.Type == typeof(int))
    {
        var sortExpr = Expression.Lambda<Func<T, int>>(property, new[] { sortParam });
        orderedQuery = !sortFilter.IsDescending 
            ? (orderedQuery == null ? query.OrderBy(sortExpr) : orderedQuery.ThenBy(sortExpr)) 
            : (orderedQuery == null ? query.OrderByDescending(sortExpr) : orderedQuery.ThenByDescending(sortExpr));
    }
    else if (property.Type == typeof(bool))
    {
        var sortExpr = Expression.Lambda<Func<T, bool>>(property, new[] { sortParam });
        orderedQuery = !sortFilter.IsDescending 
            ? (orderedQuery == null ? query.OrderBy(sortExpr) : orderedQuery.ThenBy(sortExpr))
            : (orderedQuery == null ? query.OrderByDescending(sortExpr) : orderedQuery.ThenByDescending(sortExpr));
    }
    else if (property.Type == typeof(DateTimeOffset?))
    {
        var sortExpr = Expression.Lambda<Func<T, DateTimeOffset>>(
        Expression.Coalesce(property, Expression.Constant(DateTimeOffset.MaxValue)), new[] { sortParam });
        orderedQuery = !sortFilter.IsDescending 
            ? (orderedQuery == null ? query.OrderBy(sortExpr) : orderedQuery.ThenBy(sortExpr))
            : (orderedQuery == null ? query.OrderByDescending(sortExpr) : orderedQuery.ThenByDescending(sortExpr));
    }
    else
    {
        var sortExpr = Expression.Lambda<Func<T, object>>(property, new[] { sortParam });
        orderedQuery = !sortFilter.IsDescending 
            ? (orderedQuery == null ? query.OrderBy(sortExpr) : orderedQuery.ThenBy(sortExpr))
            : (orderedQuery == null ? query.OrderByDescending(sortExpr) : orderedQuery.ThenByDescending(sortExpr));
    }
    return orderedQuery;
}

提前致谢。

2 个答案:

答案 0 :(得分:1)

这有帮助吗?

    private static IOrderedQueryable<T> FormatQuery<T>(IQueryable<T> query, Expression property, ParameterExpression sortParam, SortFilter sortFilter, ref IOrderedQueryable<T> orderedQuery)
    {
        if (property.Type == typeof(int))
        {
            FormatQueryExtracted(query, GetSortExpr<T, int>(property, sortParam), sortFilter, ref orderedQuery);
        }
        else if (property.Type == typeof(bool))
        {
            FormatQueryExtracted(query, GetSortExpr<T, bool>(property, sortParam), sortFilter, ref orderedQuery);
        }
        else if (property.Type == typeof(DateTimeOffset?))
        {
            FormatQueryExtracted(query, GetSortExpr<T, DateTimeOffset>(Expression.Coalesce(property, Expression.Constant(DateTimeOffset.MaxValue)), sortParam), sortFilter, ref orderedQuery);
        }
        else
        {
            FormatQueryExtracted(query, GetSortExpr<T, object>(property, sortParam), sortFilter, ref orderedQuery);
        }
        return orderedQuery;
    }

    private static Expression<Func<T, P>> GetSortExpr<T, P>(Expression property, ParameterExpression sortParam)
    {
        return Expression.Lambda<Func<T, P>>(property, new[] { sortParam });
    }

    private static void FormatQueryExtracted<T, P>(IQueryable<T> query, Expression<Func<T, P>> sortExpr, SortFilter sortFilter, ref IOrderedQueryable<T> orderedQuery)
    {
        orderedQuery = !sortFilter.IsDescending
            ? (orderedQuery == null ? query.OrderBy(sortExpr) : orderedQuery.ThenBy(sortExpr))
            : (orderedQuery == null ? query.OrderByDescending(sortExpr) : orderedQuery.ThenByDescending(sortExpr));
    }

答案 1 :(得分:0)

将属性设为<T2>而不是Expression并将<T, T2>放在params之前,在参数parenthesys之后,您可以放置​​where T2 : Expression,现在你可以只使用T2作为属性打字,抱歉,当我回到家时,我会写更多信息,我正在通过手机进行此操作

编辑:好了,现在回家看看:

private static IOrderedQueryable<T> FormatQuery<T, T2>(IQueryable<T> query, T2 property, ParameterExpression sortParam, SortFilter sortFilter, ref IOrderedQueryable<T> orderedQuery) where T2 : Expression
{
    var sortExpr = Expression.Lambda<Func<T, T2>>(property, new[] { sortParam });
    return !sortFilter.IsDescending 
        ? (orderedQuery == null ? query.OrderBy(sortExpr) : orderedQuery.ThenBy(sortExpr)) 
        : (orderedQuery == null ? query.OrderByDescending(sortExpr) : orderedQuery.ThenByDescending(sortExpr));
}