Aggregate Event Handlers
DDD PHP
Last updated
Was this helpful?
DDD PHP
Last updated
Was this helpful?
Read sections first to get more details about Aggregates.
To tell Ecotone
to retrieve Events from your Aggregate add trait WithEvents
which contains two methods: recordThat
and getRecordedEvents
.
After importing trait, Events will be automatically retrieved and published after handling Command in your Aggregate.
Using recordThat
will delay sending an event till the moment your Aggregate is saved in the Repository. This way you ensure that no Event Handlers will be called before the state is actually stored.
Sometimes you may have situation, where Event from one Aggregate will actually change another Aggregate. In those situations you may actually subscribe to the Event directly from Aggregate, to avoid creating higher level boilerplate code.
In those situations however you need to ensure event contains of reference id, so Ecotone knows which Aggregate to load from the database.
You may subscribe to Events by names, instead of the class itself. This is useful in cases where we want to decoupled the modules more, or we are not interested with the Event Payload at all.
For Events published from your Aggregate, it's enough to provide NamedEvent
attribute with the name of your event.
And then you can subscribe to the Event using name
For more sophisticated scenarios, where there is no direct identifier in corresponding event, you may use of identifier mapping. You can read about it more in