Domain events and domain events

I am new to DDD and now I am reading articles to get more information. One of the articles is about domain (DE) events. For example, sending email is a domain event that occurs after some criteria are met when part of the code is executed.

The sample code shows one way to handle domain events, followed by this paragraph.

Keep in mind that the above code will run in the same thread in the same transaction as normal domain operation, so you should avoid taking any blocking actions, for example, using SMTP or web services. Instead, prefer to use one-way messaging to communicate with something else that makes these blocking actions.

My questions

  • Is this a common problem when processing DE? Or is it just a solution problem in the mentioned article?
  • If domain events occur in a transaction and the system will not process them synchronously, how to handle them?
  • When I decide to serialize these events and let the scheduler (or any other mechanism) execute them, what happens when a transaction rolls back? (in the event of the article is the code executed in the transaction) that will cancel them (when they are not stored in the database)?

thanks

+7
source share
1 answer

This is a common period of the problem, not to mention DDD

In general, on any system that requires a reaction (such as a web server), any lengthy actions must be processed asynchronously for the startup process.

This means a queue.

Rolling back your transaction should remove the item from the queue.

Of course, now you need additional mechanisms to handle the situation when the item in the queue is not processed - that is, e-mail is not sent - you also need to enable this in your startup codes - followed by the RELY process on the previous process that already happened, in some then the moment there will be a problem.

In short, your queue mechanism must be transactional and allow retries, and you need to think of the whole chain of events as a workflow.

+8
source

All Articles