0

更新

我有一个 RiskReport 类型,它从 IReportRepository 获取数据,操作数据,并根据预定义的公式计算风险。

有人可能会争辩说,RiskReport 类型应该以准确的格式获取数据,而不是执行数据操作。RiskReport 应该只关心如何根据公式计算数据,而 IReportRepository 应该只返回 RiskReport 类所需的数据。

是否应该在 IReportRepository 和 RiskReport 之间引入一个新类?因为,目前,从 IReportRepository 返回的数据被处理为计算风险所需的格式。

class RiskReport 
{
    private IReportRepository reportRepository;

    public RiskReport(IReportRepository reportRepository)
    {
        this.reportRepository = reportRepository;
    }




    public decimal CalculateDataBasedOnFormula()
    {
        var result = from d in reportRepository.GetReportRelatedData()
                     group d by d.Id into dgp   //potentially complex grouping
                     select new
                                {
                                    TotalPage = dgp.Sum(x=>x.Pages)  //potentially complex projection
                                };


        decimal risk=  //use the result variable to calculate data based on complex formula not shown here

        return risk;

    }
}


interface IReportRepository
{
    IEnumerable<ReportRelatedData> GetReportRelatedData();
}

public class ReportRepository: IReportRepository
{

    public IEnumerable<ReportRelatedData> GetReportRelatedData()
    {
       //return data from underlying data source
        return new BindingList<ReportRelatedData>();
    }
}

public class ReportRelatedData
{
    public int Id { get; set; }
    public int Name { get; set; }
    public int Pages { get; set; }
    //... more properties here
}

任何想法将不胜感激!

4

2 回答 2

1

I have a Report type, which gets data from IReportRepository, manipulates the data, and calculates rate according to predefined formula.

I think the answer is in your first sentence. If you want the code to be good, make it SOLID. "S" stands for Single Responsibility Principle. In other words, if you describe what a class does, don't use the word "and". Change your design accordingly.

于 2012-04-24T06:05:49.427 回答
0

我认为这是其中一个问题,如果你问 1000 个开发人员,你可以获得 1000 个答案,但是是的,我会说应该使用另一个类。这是我的理由:

  1. 可以独立测试“数学”类
  2. 可以重用一个单独的类,保持代码的其余部分 DRY
  3. 如果公式发生变化,重构将不会嵌入到您的报告代码中

如果我必须继承代码库,我希望在这里看到三个类,所以如果我正在开发它,我想留给下一个开发者。

干杯。

于 2012-04-24T02:54:44.010 回答