具有以下结构:
演示级别:
Web 界面、REST API 和命令提示符 - 全部仅调用 OrderService。
应用层:
class OrderService { private $em; private $repository; private $calculator; public function __construct( \Doctrine\ORM\EntityManagerInterface $em; ClientRepositoryInterface $repository, cumulativeDiscountCalculator $calculator } { $this->em = $em; $this->repository = $repository; $this->calculator = $calculator; } public function calculateCumulativeDiscount($id) { $this->em->beginTransaction(); try { $client = $this->repository->findClient(); $this->calculator->calculate($client); $this->em->flush(); $this->em->commit(); } catch (\Exception $e) { $this->em->rollback(); throw $e; } } }
模型层:
interface ClientInterface { public function setDiscount($discount); } interface ClientRepositoryInterface { public function findClient($id); public function findClientsByDataRange($from, $to); } class cumulativeDiscountCalculator { public function calculate(ClientInterface $client) { $client->setDiscount(mt_rand(1, 50)); } }
基础设施层:
PHP Doctrine 2 - 实现 ClientRepositoryInterface。
我的任务 - 为一组客户执行计算折扣。 (方法ClientRepositoryInterface::findClientsByDataRange返回集合进行处理)
问题是我需要处理最多 100,000 条记录。我知道如何在技术上做到这一点,但如何在 DDD 方面做到这一点?出现以下问题:
- 使用哪一层进行批处理?
- 如何收集操作结果:错误、成功客户数量等?
- 在哪里设置事务边界(每 N 个客户端 - 提交并开始一个新事务)?
- 我有大约 10-20 个批量操作,开发任何结构可能有意义吗?
最佳答案
在我看来,您应该将批处理操作视为您域的一部分,而不仅仅是一些“琐碎”操作。写下需求,您会发现它也需要一些领域建模。例如。您需要存储有关每个批处理运行的基本数据(类型、时间、处理的记录数、结果、相关错误等),然后您需要具有预览和计划它们的功能(何时、哪个批处理运行、重新运行等) )。您可能需要一些工具来监控它们的时间或资源(每次运行需要多长时间,需要多少内存等)。
根据您上面提到的内容,我可以想象这样的类(class):
- 批处理
- 批处理接口(interface)
- ClientDiscountBatch { $scheduleDay, $scheduleTime }
- BatchResultEntity { $itemProcessed, $itemErrors, $maxMemory, $持续时间}
- BatchResultRepository
- ...
然后您的每个批处理操作都将实现 BatchInterface 并由 BatchRunner 管理,结果将由 BatchResultRepository 等保存。
所有操作都将使用其他域类,例如您上面提到的。累积折扣计算器。
就事务边界而言,您继续使用现有边界 - 例如。聚合根。每次迭代后,您都会增加结果数量或记录错误。
关于php - 领域驱动设计和批处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28245559/