使用 DRY 原则重构 C# 代码

Refactor C# code using DRY principle

我似乎不知道如何重构代码中的特定部分。我已将它从两个单独的方法缩减为一个方法,并将一个附加变量 (TopOrBottomPercent) 作为标志传递以确定适当的 OrderByWhere 子句(因为它们是唯一的两种方法的区别)。现在我只剩下两种相同的方式来加载 DataTable(过滤后)。

相关代码如下:

    private object LoadPercentDiffPoP(String TopOrBottomPercent, DateTime prevPeriodStart, DateTime prevPeriodEnd, DateTime currPeriodStart, DateTime currPeriodEnd) {

        ...

        // Populating data from dictionary to grid
        if (TopOrBottomPercent.ToLower() == "top") {
            foreach (KeyValuePair<string, ReportData> keyValuePair in ReportDataElements.Where(kvp => kvp.Value.CommPercentDiff > 0)
                .OrderByDescending(P => P.Value.CommPercentDiff).Take(itemsToReturn)) {
                detailTable.Rows.Add(new object[]{
                    keyValuePair.Value.LocationVar,
                    keyValuePair.Value.PropertyID,
                    keyValuePair.Value.Property,
                    keyValuePair.Value.IndividualID,
                    keyValuePair.Value.Individual,
                    keyValuePair.Value.PrevPeriodComm,
                    keyValuePair.Value.CurrPeriodComm,
                    keyValuePair.Value.CommPercentDiff
                });
            }
        } else if (TopOrBottomPercent.ToLower() == "lower") {
            foreach (KeyValuePair<string, ReportData> keyValuePair in ReportDataElements.Where(kvp => kvp.Value.CommPercentDiff < 0 
                && kvp.Value.CommPercentDiff >= Convert.ToDecimal(maxPercentDisplayed)).OrderBy(P => P.Value.CommPercentDiff).Take(itemsToReturn)) {
                detailTable.Rows.Add(new object[]{
                    keyValuePair.Value.LocationVar,
                    keyValuePair.Value.PropertyID,
                    keyValuePair.Value.Property,
                    keyValuePair.Value.IndividualID,
                    keyValuePair.Value.Individual,
                    keyValuePair.Value.PrevPeriodComm,
                    keyValuePair.Value.CurrPeriodComm,
                    keyValuePair.Value.CommPercentDiff
                });
            }
        }

        return detailTable;

    }

感谢任何帮助,谢谢!

您可以逐步构建表达式,而不是作为单个语句:

private object LoadPercentDiffPoP(String TopOrBottomPercent, DateTime prevPeriodStart, DateTime prevPeriodEnd, DateTime currPeriodStart, DateTime currPeriodEnd) {

  ...

  // Populating data from dictionary to grid

  IEnumerable<KeyValuePair<string, ReportData>> query = ReportDataElements;

  if (TopOrBottomPercent.ToLower() == "top") {
    query = query
      .Where(kvp => kvp.Value.CommPercentDiff > 0)
      .OrderByDescending(P => P.Value.CommPercentDiff);
  } else {
    query = query
      .Where(kvp => kvp.Value.CommPercentDiff < 0 && kvp.Value.CommPercentDiff >= Convert.ToDecimal(maxPercentDisplayed))
      .OrderBy(P => P.Value.CommPercentDiff);
  }

  foreach (KeyValuePair<string, ReportData> keyValuePair in query.Take(itemsToReturn)) {
    detailTable.Rows.Add(new object[]{
      keyValuePair.Value.LocationVar,
      keyValuePair.Value.PropertyID,
      keyValuePair.Value.Property,
      keyValuePair.Value.IndividualID,
      keyValuePair.Value.Individual,
      keyValuePair.Value.PrevPeriodComm,
      keyValuePair.Value.CurrPeriodComm,
      keyValuePair.Value.CommPercentDiff
    });
  }
  return detailTable;
}