Utilities for connecting CQRS with EF Core and MediatR.
- BaseEntity: domain events collection and
UpdatedAtmaintenance. - IDomainEvent: marker interface extending MediatR
INotification. - IUnitOfWork: abstraction for committing changes (
SaveChangesAsync). - CqrsDbContext: abstract context that dispatches domain events after persistence, with cycle protection and a virtual
MaxEventDispatchIterations.
Add the package from NuGet (after publishing):
dotnet add package Dzeta.Extensions.CQRS- Inherit your entities from
Dzeta.Extensions.CQRS.BaseEntityand raise events:
public sealed class Wallet : BaseEntity
{
public Guid Id { get; private set; }
public decimal Balance { get; private set; }
public void Deposit(decimal amount)
{
Balance += amount;
AddDomainEvent(new DepositHappened(Id, amount));
}
}-
Implement events as
IDomainEventand MediatR handlers in your app layer. -
Derive your EF Core context from
CqrsDbContext<TContext>and callSaveChangesAsyncas usual. Events will be dispatched viaIPublisherafter each successful save until the queue is empty. OverrideMaxEventDispatchIterationsif needed.
This project is configured to generate a NuGet package on build. Run:
dotnet build -c ReleaseOr to pack explicitly:
dotnet pack -c ReleaseMIT