更新
我有一个 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
}
任何想法将不胜感激!