Marco Veenendaal Marco Veenendaal - 1 month ago 7
PHP Question

How to handle relationships with a command bus approach using DTO's

In a new greenfield project I'm using a command bus approach, problem I'm encountering is how I should handle relationships.

For example, an Order has multiple OrderLines, a Shipping and Invoice address.

Since I'm using a command bus approach I want to pass a DTO to my command bus holding all the information necessary to create an Order.

Should this command also hold relationships (i.e. an CreateOrderTask, CreateOrderAddress)?

My directory/file structure looks like this:

- Infrastructure
-- Ui
--- Web
---- CreateOrderController.php
-- ....
- Application
-- CreateOrder
--- CreateOrder.php
--- CreateOrderHandler.php
--- CreateOrderLine.php (?)
- Domain
-- Order
--- Order.php
--- OrderLine.php
--- Address.php
-- ...

So should the CreateOrder class look like this:

// CreateOrder.php


class CreateOrder {

/** @var CreateOrderLine[] */
protected $createOrderLines;

public function __construct(array $createOrderLines)
$this->createOrderLines = $createOrderLines;

public function getCreateOrderLines()
return $this->createOrderLines;

Although I think there are many correct answers, I'm looking for a best practice. What are your thoughts?

Answer Source

It depends on your transaction boundaries.

If you need that all the commands must succeed or fail together then you in fact must have a bigger command with all the data needed.

If any of the sub-commands fails and the system is still in valid state then you don't need a bigger command and you may send multiple commands in some kind of batch, each one having its own transaction boundary.