目前,我正在开发一个系统,它执行相当多的报告功能,消耗许多不同的数据点并将它们转换成更大的,有时是扁平化的输出。我的应用程序大部分建立在存储库模式的变体之上。由于这个原因,我有一套用于测试场景的模拟存储库。我遇到的问题是,这些数据点之间的交互非常复杂,以至于维护“模拟数据”很快就成为了一场维护噩梦。以下是一个模拟示例:
public class SomeReportingEntity
{
private IProductRepo ProductRepo;
private IManagerRepo ManagerRepo;
private ILocationRepo LocationRepo;
private IOrdersService OrdersService;
private IEmployeeRepo EmployeeRepo;
public ReportingEntity(IProductRepo ipr, IManagerRepo imr, ILocationRepo ilr, IOrdersService ios,
IEmployeeRepo ier){
//Load these to private vars...
}
//This is the function that I want to test...
public SomeReportingEntity GetManagerSalesByRegionReport()
{
//Make a complex join on all sub collections. These
//sub collections are all under test individually.
var MangerSalesByRegionItems = From x in ProductRepo.CurrentProducts()
Join y in OrdersService.FutureOrders() On ...
Join z in EmployeeRepo.ActiveEmployees() On ...
Join a in LocationRepo.GetAllRegions() On ...
Join b In ManagerRepo.GetActiveManagers On ...
Select new SomeReportingEntity() With { ... }
return MangerSalesByRegionItems.ToList();
}
无可否认,这只是一个非常假造的例子,但我想强调的基本思想是,我有几个要加入的存储库,并且需要创建许多测试以确保此复杂查询按预期执行。由于连接操作非常复杂,使得模拟数据非常难以保持一致 - 特别是当我不得不添加更多关联并测试其他点时。此外,我需要能够将特定记录状态输入到模拟中(例如缺少分配经理的员工),以验证查询是否适当处理这些情况。
所以这里是我的问题:
- 最佳方法是什么,可以“模拟”这些数据,以避免维护的噩梦?许多人建议构建支持此的内存数据库。
- 我真的在这里遭受架构问题吗?在报告方案中,我经常发现自己处于这种模式中,其中我将许多不相关的数据点合并成一个新的混合实体。随着Linq的出现,这很容易做到,并且具有高度的意图清晰度,但有时感觉有点作弊。