dotnet / dotnet/docs

Event handlers may cause multiple transactions to be executed in the proposed approach instead of one

Open
#27,904 1 comment 0 reactions 0 assignees View on GitHub
:watch: Not Triaged dotnet-architecture/svc microservices/subsvc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
19h 10m
Merged PRs (30d)
268

Description

So I understand that an entity has a collection of events. They are dispatched before saving changes on the DbContext (which implements IUnitOfWork) when the SaveEntitiesAsync method is called. Let's assume that they are dispatched before, not after saving changes by the base SaveChangesAsync method call.

That said, let's consider an example. There is a command handler that modifies an aggregate in such a way that it generates at least TWO events in its event collection. The command handler then calls the SaveEntitiesAsync method on IUnitOfWork to persist that change.

Now the events in the aggregate's internal collection are processed sequentially in a loop, right? I'm referring to the _mediator.DispatchDomainEventsAsync(this) method call, but don't know its implementation. Let's say that each of them is handled by event handlers that make changes on other aggregates in the same domain.

The first event handler modifies an aggregate and calls SaveEntitiesAsync on the same IUnitOfWork instance, which calls SaveChangesAsync internally on the DbContext. This causes the current transaction to be committed on the database (I mean all changes in the change tracker of the DbContext). The changes include both the changes made by the command handler and by the current event handler.

Because there are two events in the original aggregate, we follow to the second iteration inside the DispatchDomainEventsAsync method, and publish the second event. Its handler may also make some aggregate changes and call SaveEntitiesAsync on the same IUnitOfWork instance.

Finally, we call SaveChangesAsync from the top-level SaveEntitiesAsync method call because we have processed both the events of the source aggregate. But there are no changes to commit because they have already been committed by the two event handlers, **in two independent transactions**.

Please correct me if my understanding is wrong. It is based strictly on the example SaveEntitiesAsync method and the ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler event handler implementation.

Thanks!

[BTW, my assumption is that there is no transaction created explicitly for the DbContext]

---
#### Document Details

⚠ *Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.*

* ID: d432d5bb-24a7-3bec-1e17-910c8129226e
* Version Independent ID: e41af96d-f015-a8e0-4449-7a508d7bccc8
* Content: [Domain events. design and implementation](https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/domain-events-design-implementation)
* Content Source: [docs/architecture/microservices/microservice-ddd-cqrs-patterns/domain-events-design-implementation.md](https://github.com/dotnet/docs/blob/main/docs/architecture/microservices/microservice-ddd-cqrs-patterns/domain-events-design-implementation.md)
* Product: **dotnet-architecture**
* Technology: **microservices**
* GitHub Login: @nishanil
* Microsoft Alias: **nanil**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.