Comments (1)
Domain events are dispatched within the DispatchDomainEventsInterceptor
by overriding SavingChanges
and SavingChangesAsync
:
public override InterceptionResult<int> SavingChanges(DbContextEventData eventData, InterceptionResult<int> result)
{
DispatchDomainEvents(eventData.Context).GetAwaiter().GetResult();
return base.SavingChanges(eventData, result);
}
public override async ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default)
{
await DispatchDomainEvents(eventData.Context);
return await base.SavingChangesAsync(eventData, result, cancellationToken);
}
In this design, domain events are dispatched before saving changes, so the Id
has not yet been generated. If you need the Id
to be available in your domain events, you could use a unique identifier (UID) for the entity, independent of the database-generated Id
, or dispatch the domain events after saving changes. However, both approaches have challenges, such as handling transactional consistency and eventual consistency in case of failures.
Here's an example that illustrates the deferred approach to dispatching events, as described in .NET Microservices: Architecture for Containerized .NET Applications (see section The deferred approach to raise and dispatch events):
// EF Core DbContext
public class OrderingContext : DbContext, IUnitOfWork
{
// ...
public async Task<bool> SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken))
{
// Dispatch Domain Events collection.
// Choices:
// A) Right BEFORE committing data (EF SaveChanges) into the DB. This makes
// a single transaction including side effects from the domain event
// handlers that are using the same DbContext with Scope lifetime
// B) Right AFTER committing data (EF SaveChanges) into the DB. This makes
// multiple transactions. You will need to handle eventual consistency and
// compensatory actions in case of failures.
await _mediator.DispatchDomainEventsAsync(this);
// After this line runs, all the changes (from the Command Handler and Domain
// event handlers) performed through the DbContext will be committed
var result = await base.SaveChangesAsync();
}
}
In this example, when to dispatch domain events (before or after saving changes) depends on your specific requirements and the trade-offs you're willing to accept.
from cleanarchitecture.
Related Issues (20)
- What is the best method INNER JOIN for Users?
- Scaffold shows "Failed to compile the project in memory" error HOT 2
- Small bug, you can not reuse ValidationContext in ValidationBehaviour.cs
- ValidationException doesn't return the errors only a stacktrace HOT 1
- Referring Entityframework in Application layer or use case layer HOT 2
- No IServiceMetadata Generated When Adding .NET Aspire Support
- Scaffolding Identity UI. HOT 3
- Add Blazor Support HOT 7
- Users is missing in Swagger and API specification HOT 3
- It is impossible to launch a new project HOT 9
- Making the Application layer independent of any specific technology or external library. HOT 3
- angular UserService
- NullReferenceException for functional tests after creating a new project with the template HOT 1
- Introducing an AI-driven GPT Model for Automated EF Core Entity Generation HOT 1
- Add Web.GrpcServer [gRPC Server] HOT 1
- Migrations Issues
- Angular 17, standalone HOT 2
- Can't create migrations HOT 2
- Swagger Specifications are incomplete
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cleanarchitecture.