Repository navigation
Description
We enrich our Events when we reference another aggregate by ID e.g. employerId by adding a name employerName.
This allows us to directly update the read model.
A+ES:
final class EmploymentContractSigned extends AggregateChanged
{
/** @var EmploymentContractId $contractId */
private $contractId;
/** @var EmployerId */
private $employerId;
}Projector:
final class EmploymentContractDetailsProjection implements ReadModelProjection
{
public function project(ReadModelProjector $projector): ReadModelProjector
{
$projector->fromStream('employment_contract_stream')
->when([
EmploymentContractSigned::class => function ($state, EmploymentContractSigned $event) {
/** @var EmploymentContractDetailsReadModel $readModel */
$readModel = $this->readModel();
$readModel->stack('sign', [
'contract_id' => $event->contractId()->toString(),
'employer_id' => $event->employerId()->toString(),
'employer_name' => $event->employerName(),
'created_at' => $event->createdAt()->format('Y-m-d H:i:s'),
'created_by_user_id' => $event->metadata()['created_by_user_id'],
'created_by_username' => $event->metadata()['created_by_username'],
'created_by_full_name' => $event->metadata()['created_by_full_name']
]);
},
]);
return $projector;
}
}Sometimes an Employer name can change. Normally this is not relevant to historical data. But for active Contracts the read model should always have the current Employer name.
Normally you could simply update a table:
UPDATE `employment_contract_details` SET employer_name = 'New Acme' where employer_id = '...';But of course this data will get lost as soon as we reply the event stream. The original Employer name will be set and the UPDATE is lost.
One possibility to to keep the change is to fire an event on the aggregate:
final class EmploymentContractSigned extends AggregateChanged
{
public function recognizeEmployerChange($newEmployerName): void
{
$this->recordThat(EmployerChangeRecognized::with($this->contractId, $this->employerId, $newEmployerName));
}"If an external change from another context is relevant to the state of the current context then the state change should be recorded."
But in this case we are not changing the actual state. That would require the Employer (ID) itself to change e.g.:
final class EmploymentContractSigned extends AggregateChanged
{
public function transferToNewEmployer($newEmployerId, $newEmployerName): void
{
$this->recordThat(TransferredToNewEmployer::with($this->contractId, $newEmployerId, $newEmployerName));
}But in our case we are simply changing a property that was already enriched for read-model purposes.
It is convenient to have a Projector fill a single table for the read model. But updating the read model data by going through the aggregate feels strange.
How do you guys handle these external changes that do no seem relevant to the state of the aggregate?
Return of the Join
This is our current READ Model repository implementation:
final class EmploymentContractDetailsRepository extends DbalReadModelRepository implements DetailsRepository
{
public function ofId(string $id): Details
{
$stmt = $this->getConnection()->prepare(sprintf(
"SELECT * FROM `%s` WHERE contract_id = :id",
Table::EMPLOYMENT_CONTRACT_DETAILS
));
$stmt->bindValue('id', $id);
$stmt->execute();
$result = $stmt->fetch();
if (false === $result) {
throw new \Exception('Contract not found');
}
return Details::fromArray($result);
}
}Is this a valid use case to use a JOIN on a local copy of the data that has to be up-to-date?
final class EmploymentContractDetailsRepository extends DbalReadModelRepository implements DetailsRepository
{
public function ofId(string $id): Details
{
$stmt = $this->getConnection()->prepare(sprintf(
"SELECT * FROM `%s` JOIN `employers` USING (employer_id) WHERE contract_id = :id",
Table::EMPLOYMENT_CONTRACT_DETAILS
));
$stmt->bindValue('id', $id);
$stmt->execute();
$result = $stmt->fetch();
if (false === $result) {
throw new \Exception('Contract not found');
}
return Details::fromArray($result);
}
}Advantage:
Whether to use the current or the old Employer name can still be handled per READ (VIEW) model (UI Use Case).
Activity