Doctrine ORM
Ecotone comes with out of the box Doctrine ORM support for Aggregates.
class EcotoneConfiguration
{
#[ServiceContext]
public function getDbalConfiguration(): DbalConfiguration
{
return DbalConfiguration::createWithDefaults()
->withDoctrineORMRepositories(true);
}
}
Then you mark your Entities with Aggregate attribute and set up Command Handlers.
#[Aggregate]
class User
{
use WithEvents;
#[AggregateIdentifier]
private string $userId;
private string $name;
private bool $isActive;
private function __construct(string $userId, string $name)
{
$this->userId = $userId;
$this->name = $name;
$this->isActive = false;
$this-recordThat(new UserRegistered($userId)); // 3. Event Publishing
}
#[CommandHandler] // 1. Factory method register
public static function register(RegisterUser $command): static
{
return new static(Uuid::uuid4()->toString(), $command->name);
}
#[CommandHandler("activate")] // 2. Action method "activate"
public function activate(): void
{
$this->isActive = true;
}
}
- 1.Calling factory method "register":
$this->commandBus->send(new RegisterUser('Johny'));
- 2.Calling action method "activate":
$this->commandBus->sendWithRouting("activate", metadata: ["aggregate.id" => $id]);
- 3.By importing trait
WithEvents
we can publish events from our Aggregate usingrecordThat
method.
In case of Ecotone you may use routing for your Message Handlers or direct Message Classes. It's up to you to decide whatever works best in your context.
Last modified 2mo ago