Domain Driven Design and batch processing

stalxed

Has the following structure:

  1. Presentation Level:

    Web Interface, REST API and command prompt - all call only 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.

My task - perform calculation discounts for a collection of clients. (method ClientRepositoryInterface::findClientsByDataRange returns collection for processing)

The problem is that I need to handle up to 100,000 records. I know how to do this technically, but how to do it in terms of 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)?
  • I have about 10-20 batch operations, it may make sense any structure to develop?
Tomas Dermisek

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
  • BatchInterface
  • ClientDiscountBatch { $scheduleDay, $scheduleTime }
  • BatchResultEntity { $itemProcessed, $itemErrors, $maxMemory, $duration }
  • 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.

All the operations will be using other Domain classes like you mentioned above eg. 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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Layers in Domain Driven Design

From Dev

Domain Driven Design - Domain or Security

From Dev

Access Control in Domain Driven Design

From Dev

Implementing Domain Driven Design Cost

From Dev

Implementing Domain Driven Design Cost

From Dev

Domain Driven Design Bounded Context Domain Objects

From Dev

Domain Driven Design: infrastructure concern or domain concern?

From Dev

Domain Driven Design: infrastructure concern or domain concern?

From Dev

Domain Driven Design. Entity type design

From Dev

Implementing Paging and Sorting with Domain Driven Design

From Dev

Implementing Domain-Driven Design and Transactions

From Dev

Domain driven design repository implementation in infrastructure layer

From Dev

Domain Driven Design in Node.js Application

From Dev

Fetching associated aggregates in Domain driven Design

From Dev

Domain Driven Design - CQRS + ES usage

From Dev

Where is the call to persistence in the domain driven design

From Dev

Implementing Domain Driven Design Book Confusion

From Dev

Domain Driven Design (DDD) and database generated reports

From Dev

Domain-Driven-Design Entities and Value Objects

From Dev

Proper way to get aggregates in Domain Driven Design

From Dev

Domain Driven Design Auto Incremented Entity Key

From Dev

Fetching associated aggregates in Domain driven Design

From Dev

Where to find Domain Driven Design consultants?

From Dev

How to add scheduled jobs in domain driven design

From Dev

How to handle Domain Driven Design when domain is dynamic / changes

From Dev

Is Domain-Driven Design a right fit for a product in Enterprise Architecture Domain?

From Dev

Domain Driven Design: Can Infrastructure or Repositories use Domain objects?

From Dev

Do I need to test the domain services in Domain driven design?

From Dev

Populating Domain objects with data in a Domain Driven Design architecture

Related Related

  1. 1

    Layers in Domain Driven Design

  2. 2

    Domain Driven Design - Domain or Security

  3. 3

    Access Control in Domain Driven Design

  4. 4

    Implementing Domain Driven Design Cost

  5. 5

    Implementing Domain Driven Design Cost

  6. 6

    Domain Driven Design Bounded Context Domain Objects

  7. 7

    Domain Driven Design: infrastructure concern or domain concern?

  8. 8

    Domain Driven Design: infrastructure concern or domain concern?

  9. 9

    Domain Driven Design. Entity type design

  10. 10

    Implementing Paging and Sorting with Domain Driven Design

  11. 11

    Implementing Domain-Driven Design and Transactions

  12. 12

    Domain driven design repository implementation in infrastructure layer

  13. 13

    Domain Driven Design in Node.js Application

  14. 14

    Fetching associated aggregates in Domain driven Design

  15. 15

    Domain Driven Design - CQRS + ES usage

  16. 16

    Where is the call to persistence in the domain driven design

  17. 17

    Implementing Domain Driven Design Book Confusion

  18. 18

    Domain Driven Design (DDD) and database generated reports

  19. 19

    Domain-Driven-Design Entities and Value Objects

  20. 20

    Proper way to get aggregates in Domain Driven Design

  21. 21

    Domain Driven Design Auto Incremented Entity Key

  22. 22

    Fetching associated aggregates in Domain driven Design

  23. 23

    Where to find Domain Driven Design consultants?

  24. 24

    How to add scheduled jobs in domain driven design

  25. 25

    How to handle Domain Driven Design when domain is dynamic / changes

  26. 26

    Is Domain-Driven Design a right fit for a product in Enterprise Architecture Domain?

  27. 27

    Domain Driven Design: Can Infrastructure or Repositories use Domain objects?

  28. 28

    Do I need to test the domain services in Domain driven design?

  29. 29

    Populating Domain objects with data in a Domain Driven Design architecture

HotTag

Archive