Skip to content

Commit

Permalink
Add mediatr domain event dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis committed Jul 25, 2023
1 parent b23c5f7 commit 34c06af
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Ardalis.SharedKernel/Ardalis.SharedKernel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<RepositoryUrl>https://github.com/ardalis/Ardalis.SharedKernel</RepositoryUrl>
<PackageTags>DDD;Shared Kernel;SharedKernel;Domain-Driven Design;Repository;Specification;ValueObject;Value Object;Ardalis;Clean;Clean Architecture;Clean Architecture Template</PackageTags>
<PackageIcon>icon.png</PackageIcon>
<Version>1.0.0</Version>
<Version>1.1.0</Version>
<PackageReleaseNotes>
* Initial Release
* Add MediatR Domain Event Dispatcher
</PackageReleaseNotes>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
Expand Down
1 change: 0 additions & 1 deletion src/Ardalis.SharedKernel/IDomainEventDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ public interface IDomainEventDispatcher
{
Task DispatchAndClearEvents(IEnumerable<EntityBase> entitiesWithEvents);
}

26 changes: 26 additions & 0 deletions src/Ardalis.SharedKernel/MediatRDomainEventDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using MediatR;

namespace Ardalis.SharedKernel;

public class MediatRDomainEventDispatcher : IDomainEventDispatcher
{
private readonly IMediator _mediator;

public MediatRDomainEventDispatcher(IMediator mediator)
{
_mediator = mediator;
}

public async Task DispatchAndClearEvents(IEnumerable<EntityBase> entitiesWithEvents)
{
foreach (var entity in entitiesWithEvents)
{
var events = entity.DomainEvents.ToArray();
entity.ClearDomainEvents();
foreach (var domainEvent in events)
{
await _mediator.Publish(domainEvent).ConfigureAwait(false);
}
}
}
}

0 comments on commit 34c06af

Please sign in to comment.