State-Stored Aggregate

This chapter will cover the basics on how to implement an Aggregate. For more details on what an Aggregate is read the DDD and CQRS concepts page.

State-Stored Aggregate

An Aggregate is a regular object, which contains state and methods to alter that state. It can be described as Entity, which carry set of behaviours. When creating the Aggregate object, you are creating the Aggregate Root.

/**
* @Aggregate() //1
*/
class Product
{
/**
* @AggregateIdentifier() //2
*/
private string $productId;
private string $name;
private integer $priceAmount;
private function __construct(string $orderId, string $name, int $priceAmount)
{
$this->productId = $orderId;
$this->name = $name;
$this->priceAmount = $priceAmount;
}
/**
* @CommandHandler() //3
*/
public static function register(RegisterProductCommand $command) : self
{
return new self(
$command->getProductId(),
$command->getName(),
$command->getPriceAmount()
);
}
/**
* @CommandHandler //4
*/
public function changePrice(ChangePriceCommand $command) : void
{
$this->priceAmount = $command->getPriceAmount();
}
}
  1. @Aggregate tells Ecotone, that this class should be registered as Aggregate Root.

  2. @AggregateIdentifier is the external reference point Aggregate.

    This field tells Ecotone to which Aggregate a given Command is targeted.

  3. @CommandHandler defined on static method acts as factory method. Given command it should return new instance of specific aggregate, in that case new Product.

  4. @CommandHandler defined on non static class method is place where you would put business logic and state changes