This article is reproduced from [how to decouple]:https://codedecoupled.com/php…
So what is the behavior with side effects? If the result of a behavior occurring once is different from that occurring many times, this behavior can be understood as having side effects. For example, sending an email to the user when the order is confirmed has side effects, because when the order is confirmed, the user will receive duplicate emails.
The following are the reactor use cases summarized from our practical experience.
Actual use case
Side effect behavior
Dealing with behaviors with side effects in the field is the most intuitive use case of reactor. For example, send an order email:
Inter aggregation communication
Sometimes two aggregations need to complete a simple process through event communication. At this time, we can use reactor. Suppose we establish two aggregations, order and bill. When the order is confirmed, bill will automatically generate an invoice. We can use reactor to complete the communication between the two aggregations:
There is a reason to emphasize the simplicity of this process. If a process is complex and long-term, we should adopt saga scheme because it supports rollback operation.
Translation domain events
When event traceability is mixed with other architecture modes, we have to deal with cross architecture communication. At this time, we can use reactor. For example, in crud architecture, we also need to listen to domain events in event traceability. If we listen directly in crud, there will be abstract leakage (domain events are abstract concepts in event traceability). At this time, we can use reactor to translate domain events into events in crud and send them:
Reactor is like a special projector. Like projector, it has a single and focused responsibility and is easy to write unit tests.
This article is reproduced from [how to decouple]:https://codedecoupled.com/php…If you are also interested in TDD, DDD and concise code, please follow the official account [how to decouple] to explore the way of software development.