简体   繁体   English

域驱动设计和批处理

[英]Domain Driven Design and batch processing

Has the following structure: 具有以下结构:

  1. Presentation Level: 演讲水平:

    Web Interface, REST API and command prompt - all call only OrderService. Web界面,REST API和命令提示符-全部仅调用OrderService。

  2. Application Layer: 应用层:

     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; } } } 
  3. Model layer: 模型层:

     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)); } } 
  4. Infrastructure Layer: 基础设施层:

    PHP Doctrine 2 - implement ClientRepositoryInterface. PHP原理2-实现ClientRepositoryInterface。

My task - perform calculation discounts for a collection of clients. 我的任务-为一组客户执行计算折扣。 (method ClientRepositoryInterface::findClientsByDataRange returns collection for processing) (方法ClientRepositoryInterface :: findClientsByDataRange返回要处理的集合)

The problem is that I need to handle up to 100,000 records. 问题是我需要处理多达100,000条记录。 I know how to do this technically, but how to do it in terms of DDD? 我知道如何从技术上做到这一点,但是如何按照DDD做到这一点? The following questions arise: 出现以下问题:

  • Which layer to use for batch processing? 批处理使用哪一层?
  • How to collect the results of actions: errors, count successful clients, etc? 如何收集行动的结果:错误,计算成功的客户等?
  • Where to set transaction boundaries(every N clients - commit and begin a new transaction)? 在哪里设置事务边界(每N个客户-提交并开始新事务)?
  • I have about 10-20 batch operations, it may make sense any structure to develop? 我大约有10到20个批处理操作,开发任何结构可能有意义吗?

In my opinion you should consider the batch operation as part of your domain not just like some "trivial" operation aside. 我认为您应该将批处理操作视为域的一部分,而不只是考虑一些“琐碎”的操作。 Write down the requirements and you will see that it requires some domain modelling too. 写下需求,您将看到它也需要一些领域建模。 Eg. 例如。 you need to store basic data about every batch run (type, when, how many records processed, results, related errors etc), then you need to have the functionality to preview and schedule them (when, which batch run, re-run etc). 您需要存储有关每个批处理运行的基本数据(类型,时间,处理的记录数量,结果,相关错误等),然后需要具有预览和计划它们的功能(何时,哪个批处理运行,重新运行等) )。 You might want to have some tool to monitor them in terms of time or resources (how long takes every run, how much memory it takes, etc). 您可能需要一些工具来监视它们的时间或资源(每次运行需要花费多长时间,花费多少内存等)。

From what you mention above I can imagine classes like: 从您上面提到的内容,我可以想象像这样的类:

  • BatchRunner BatchRunner
  • BatchInterface BatchInterface
  • ClientDiscountBatch { $scheduleDay, $scheduleTime } ClientDiscountBatch {$ scheduleDay,$ scheduleTime}
  • BatchResultEntity { $itemProcessed, $itemErrors, $maxMemory, $duration } BatchResultEntity {$ itemProcessed,$ itemErrors,$ maxMemory,$ duration}
  • BatchResultRepository BatchResultRepository
  • ... ...

Then each of your batch operations will implement the BatchInterface and will be managed by BatchRunner and results will be persisted by BatchResultRepository etc. 然后,每个批处理操作将实现BatchInterface并由BatchRunner管理,结果将由BatchResultRepository等保存。

All the operations will be using other Domain classes like you mentioned above eg. 所有操作都将使用上面提到的其他Domain类,例如。 CumulativeDiscountCalculator. CumulativeDiscountCalculator。

In the terms of transaction boundaries you keep using the existing boundaries - eg. 在交易边界方面,您继续使用现有边界-例如 Aggregate root. 聚集根。 After every iteration you increase the number of results or log an error. 每次迭代后,您增加结果数或记录一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM