From 931403a5a1e635f549159a5ac226240bc094f51a Mon Sep 17 00:00:00 2001 From: demidov-ad Date: Tue, 12 Mar 2024 21:06:15 +0300 Subject: [PATCH] Add support for dispatching events with a non-int id --- .../IDomainEventDispatcher.cs | 1 + .../MediatRDomainEventDispatcher.cs | 14 ++++++++ .../DispatchAndClearEventsWithGuidId.cs | 36 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 tests/Ardalis.SharedKernel.UnitTests/MediatRDomainEventDispatcherTests/DispatchAndClearEventsWithGuidId.cs diff --git a/src/Ardalis.SharedKernel/IDomainEventDispatcher.cs b/src/Ardalis.SharedKernel/IDomainEventDispatcher.cs index 76425c6..cfc9153 100644 --- a/src/Ardalis.SharedKernel/IDomainEventDispatcher.cs +++ b/src/Ardalis.SharedKernel/IDomainEventDispatcher.cs @@ -6,4 +6,5 @@ public interface IDomainEventDispatcher { Task DispatchAndClearEvents(IEnumerable entitiesWithEvents); + Task DispatchAndClearEvents(IEnumerable> entitiesWithEvents) where TId : struct, IEquatable; } diff --git a/src/Ardalis.SharedKernel/MediatRDomainEventDispatcher.cs b/src/Ardalis.SharedKernel/MediatRDomainEventDispatcher.cs index 210d9ce..c781b7d 100644 --- a/src/Ardalis.SharedKernel/MediatRDomainEventDispatcher.cs +++ b/src/Ardalis.SharedKernel/MediatRDomainEventDispatcher.cs @@ -24,5 +24,19 @@ public async Task DispatchAndClearEvents(IEnumerable entitiesWithEve } } } + + public async Task DispatchAndClearEvents(IEnumerable> entitiesWithEvents) + where TId : struct, IEquatable + { + foreach (var entity in entitiesWithEvents) + { + var events = entity.DomainEvents.ToArray(); + entity.ClearDomainEvents(); + foreach (var domainEvent in events) + { + await _mediator.Publish(domainEvent).ConfigureAwait(false); + } + } + } } diff --git a/tests/Ardalis.SharedKernel.UnitTests/MediatRDomainEventDispatcherTests/DispatchAndClearEventsWithGuidId.cs b/tests/Ardalis.SharedKernel.UnitTests/MediatRDomainEventDispatcherTests/DispatchAndClearEventsWithGuidId.cs new file mode 100644 index 0000000..c07eb39 --- /dev/null +++ b/tests/Ardalis.SharedKernel.UnitTests/MediatRDomainEventDispatcherTests/DispatchAndClearEventsWithGuidId.cs @@ -0,0 +1,36 @@ +using FluentAssertions; +using MediatR; +using Moq; +using Xunit; + +namespace Ardalis.SharedKernel.UnitTests.MediatRDomainEventDispatcherTests; + +public class DispatchAndClearEventsWithGuidId +{ + private class TestDomainEvent : DomainEventBase { } + private class TestEntity : EntityBase + { + public void AddTestDomainEvent() + { + var domainEvent = new TestDomainEvent(); + RegisterDomainEvent(domainEvent); + } + } + + [Fact] + public async void CallsPublishAndClearDomainEvents() + { + // Arrange + var mediatorMock = new Mock(); + var domainEventDispatcher = new MediatRDomainEventDispatcher(mediatorMock.Object); + var entity = new TestEntity(); + entity.AddTestDomainEvent(); + + // Act + await domainEventDispatcher.DispatchAndClearEvents(new List> { entity }); + + // Assert + mediatorMock.Verify(m => m.Publish(It.IsAny(), It.IsAny()), Times.Once); + entity.DomainEvents.Should().BeEmpty(); + } +}