DDD - How to obtain information from another domain with just the ID?

27 Views Asked by At

How can I get more data from another domain in DDD when I only have its ID.

Scenario: In an ordering domain, I have items that have a property called 'product_id' that references a product that belongs to another domain (CatalogProduct).

But when returning the order item data, I would like to have more information about the referenced product, such as name, sku, current stock, etc.

Should I do this at the DTO layer? In other words, when assembling "outputDto", do I make a call to another domain and add the data I need?

Below is an example of the problem written in PHP in a simplified form:

class Order
{
    private int $id;

    /** @var OrderItem[] */
    private array $items = [];
}
class OrderItem
{
    private int $id;
    private int $productId;
    private float $price;
}
  • In the 'Order' class I have an array of items In the class

  • In the 'OrderItem' class I have the ID of the referenced product

To obtain extra data from the product that is in another domain (CatalogProduct), I currently do it this way. Is correct? Can I improve with another pattern?

class OrderItemOutputDto
{
    public int $id;
    public int $productId;
    public ?ProductOutputDto $productDto = null;
    public float $price;

    /**
     * @param int $id
     * @param int $productId
     * @param float $price
     */
    public function __construct(int $id, int $productId, float $price)
    {
        $this->id = $id;
        $this->productId = $productId;
        $this->price = $price;

        /**
         * I make a call to the 'CatalogProduct' facade to get a product.
         * If I find it, I fill it in with the data
         */
        $product = CatalogProductFacade::getById($productId);
        if (!empty($product)){
            $this->productDto = $product;
        }
    }
}
0

There are 0 best solutions below