Tempest Models as Aggregates
Using Tempest active-record models as Ecotone Aggregates
Your Models as Aggregates
use Ecotone\Modelling\Attribute\Aggregate;
use Ecotone\Modelling\Attribute\CommandHandler;
use Ecotone\Modelling\Attribute\IdentifierMethod;
use Ecotone\Modelling\Attribute\QueryHandler;
use Tempest\Database\IsDatabaseModel;
use Tempest\Database\PrimaryKey;
#[Aggregate]
final class Product
{
use IsDatabaseModel;
public PrimaryKey $id;
public string $name;
public int $price;
#[CommandHandler] // 1. factory method
public static function register(RegisterProduct $command): self
{
$product = new self();
$product->name = $command->name;
$product->price = $command->price;
$product->save(); // 3. Saving
return $product;
}
#[CommandHandler('product.changePrice')] // 2. action method
public function changePrice(ChangePrice $command): void
{
$this->price = $command->price;
}
#[QueryHandler('product.getPrice')]
public function getPrice(): int
{
return $this->price;
}
#[IdentifierMethod('id')] // 4. expose the scalar identifier
public function getId(): int
{
return $this->id->value;
}
}Repository and Business Interface
Last updated
Was this helpful?