Event Sourcing: Where to Place Business Logic

I can think of two places where to put the domain logic in the system of event sources or there is a drawback.

  • In an Aggregate event handler that is called locally after an event is raised. (This is what I saw in most examples , although most of them have very simplified logic)
    Problem . An event stored in the event store and published to subscribers does not include this processed data, and therefore, the same logic should be applied to the event during design.
  • Before creating an event. Now the processed data can be stored in the event, and the projection should not know anything about the business logic. (I have not seen this method in the examples)
    Problem . In this case, although the event includes only processed data, which can lead to loss of information.
    Even worse: I also lose the opportunity to correct the wrong business logic by replaying events because the event data has already been calculated.

Example : calculating a metric from some data.
 Either I have to calculate the metric twice (once in the domain model, once in the projection)
 or I have to calculate it before sending the event and turning it on there.

+4
2

:

  • , , , , .
  • ( , ) , .
  • , .

  • When Apply

  • , !

.

- , , , . . . ( ), , if-throw?

, :

  • ( )
  • ,

, . , , , , .

+4

, - . . .
( ) . , , .
. - -, Aggregate Query, -, .
. , -.

0

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


All Articles