In ES + CQRS + DDD, can an event not update any real state of the domain at all?

Would it be nice to have events in the flow of events that do not affect any population in the state of the domain?

Take, for example, an event such as AllCompletedTodosPurged, which does nothing more than change the read model with active todos, deleting all completed todos.

+5
source share
2 answers

No, everything will be fine. A domain event is generated when the state of an aggregate changes. If nothing has changed, there is no domain event.

You can also use events outside the domain, but they will not be part of the domain and, obviously, are not part of the flow of events.

In your scenario, if the event is not generated as an effect of the cumulative change, why should it be contained in any aggregate? And technically speaking, in what stream of events will you add this event if it does not belong to anything? Will you add this event to all involved ToDos? It makes no sense.

Iโ€™m not sure that the cleanup is part of your domain, but if it is, it means that all completed todos are โ€œdeletedโ€, since each involved unit already has a ToDoDeleted event in its collection. AllCompletedTodosPurged is just an event that is useful for updating the reading model, but that is. It should not affect the domain model.

+1
source

Without affecting the population, I assume that you mean private variables within the aggregate class itself? If you meant anything else, ignore this answer.

I find that developers who implement ES for the first time tend to deal with this issue. The first time I did an end-to-end reference implementation, I had the same question.

I was really surprised to find that the local aggregated state is not as useful as I thought it would be - this state is almost always more naturally expressed in the form of predicted reading models. The only time you really need a local state is when you need to handle a single event (for example, to check).

Therefore, I would say that this is not only acceptable for supporting events that do not change the local aggregated state, but these types of events are most likely more common than events that change the local state.

0
source

Source: https://habr.com/ru/post/1232447/


All Articles