Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix issues-594, Event bus and UnitOfWork are optional #618

Merged
merged 1 commit into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/Contrib/Ddd/Domain/Masa.Contrib.Ddd.Domain/DomainEventBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ namespace Masa.Contrib.Ddd.Domain;

public class DomainEventBus : IDomainEventBus
{
private readonly IEventBus _eventBus;
private readonly IIntegrationEventBus _integrationEventBus;
private readonly IUnitOfWork _unitOfWork;
private readonly IEventBus? _eventBus;
private readonly IIntegrationEventBus? _integrationEventBus;
private readonly IUnitOfWork? _unitOfWork;

private readonly ConcurrentQueue<IDomainEvent> _eventQueue = new();

public DomainEventBus(
IEventBus eventBus,
IIntegrationEventBus integrationEventBus,
IUnitOfWork unitOfWork)
IEventBus? eventBus = null,
IIntegrationEventBus? integrationEventBus = null,
IUnitOfWork? unitOfWork = null)
{
_eventBus = eventBus;
_integrationEventBus = integrationEventBus;
Expand All @@ -30,10 +30,15 @@ public async Task PublishAsync<TEvent>(TEvent @event, CancellationToken cancella
if (@event is IIntegrationEvent integrationEvent)
{
integrationEvent.UnitOfWork ??= _unitOfWork;

MasaArgumentException.ThrowIfNull(_integrationEventBus);

await _integrationEventBus.PublishAsync(integrationEvent, cancellationToken);
}
else
{
MasaArgumentException.ThrowIfNull(_eventBus);

await _eventBus.PublishAsync(@event, cancellationToken);
}

Expand Down Expand Up @@ -74,5 +79,8 @@ public Task<bool> AnyQueueAsync()
}

public async Task CommitAsync(CancellationToken cancellationToken = default)
=> await _unitOfWork.CommitAsync(cancellationToken);
{
MasaArgumentException.ThrowIfNull(_unitOfWork);
await _unitOfWork.CommitAsync(cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,75 @@ public void TestCheckRequiredService(
Assert.AreEqual(expectedTimer, ExecuteTimer);
}

[DataRow(true, true, true, true, false, false)]
[DataRow(true, true, true, false, false, false)]
[DataRow(true, true, false, false, false, true)]
[DataRow(true, false, true, true, false, false)]
[DataRow(true, false, false, true, false, true)]
[DataRow(true, false, false, true, false, true)]
[DataRow(false, true, true, true, true, false)]
[DataRow(true, false, true, false, true, false)]
[DataTestMethod]
public async Task PublishAndUnitOfWorkAsync(
bool isAddEventBus,
bool isAddIntegrationEventBus,
bool isAddUnitOfWork,
bool isLocalEvent,
bool expectedThrowExceptionByPublish,
bool expectedThrowExceptionByCommit)
{
IEventBus? eventBus = isAddEventBus ? _eventBus.Object : null;
IIntegrationEventBus? integrationEventBus = isAddIntegrationEventBus ? _integrationEventBus.Object : null;
var unitOfWorkMock = new Mock<IUnitOfWork>();
IUnitOfWork? unitOfWork = isAddUnitOfWork ? unitOfWorkMock.Object : null;

var domainEventBus = new DomainEventBus(eventBus, integrationEventBus, unitOfWork);

if (isLocalEvent)
{
var registerUserEvent = new RegisterUserEvent();

if (expectedThrowExceptionByPublish)
{
await Assert.ThrowsExceptionAsync<MasaArgumentException>(() => domainEventBus.PublishAsync(registerUserEvent));
}
else
{
await domainEventBus.PublishAsync(registerUserEvent);

_integrationEventBus.Verify(bus => bus.PublishAsync(It.IsAny<IIntegrationEvent>(), default), Times.Never);
_eventBus.Verify(bus => bus.PublishAsync(It.IsAny<IEvent>(), default), Times.Once);
}
}
else
{
var changeOrderStateIntegrationEvent = new ChangeOrderStateIntegrationEvent();

if (expectedThrowExceptionByPublish)
{
await Assert.ThrowsExceptionAsync<MasaArgumentException>(()
=> domainEventBus.PublishAsync(changeOrderStateIntegrationEvent));
}
else
{
await _domainEventBus.PublishAsync(changeOrderStateIntegrationEvent);

_integrationEventBus.Verify(bus => bus.PublishAsync(It.IsAny<IIntegrationEvent>(), default), Times.Once);
_eventBus.Verify(bus => bus.PublishAsync(It.IsAny<IEvent>(), default), Times.Never);
}
}

if (expectedThrowExceptionByCommit)
{
await Assert.ThrowsExceptionAsync<MasaArgumentException>(() => domainEventBus.CommitAsync());
}
else
{
await domainEventBus.CommitAsync();
unitOfWorkMock.Verify(uow => uow.CommitAsync(default), Times.Once);
}
}

private static ConcurrentQueue<IDomainEvent> GetEventQueue(DomainEventBus domainEventBus)
{
var eventQueue = EventQueueFileInfo.GetValue(domainEventBus) as ConcurrentQueue<IDomainEvent>;
Expand Down