Skip to content

Commit

Permalink
Merge pull request #19 from cnblogs/fix-domain-event-generator
Browse files Browse the repository at this point in the history
fix: extract domain event publish method
  • Loading branch information
ikesnowy authored Feb 9, 2023
2 parents 6a61bae + 7c35890 commit a5cb14b
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr;
using System.Reflection;
using Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr;
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
using Cnblogs.Architecture.Ddd.EventBus.Dapr;
using Dapr.Client;
using MediatR;
using System.Reflection;

// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
Expand Down Expand Up @@ -36,5 +37,4 @@ public static IServiceCollection AddDaprEventBus(

return services;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ private static void EnsureEventBusRegistered(this IEndpointRouteBuilder builder,
}

daprOptions.IsEventBusRegistered = true;
return;
}

private static void EnsureDaprSubscribeHandlerMapped(this IEndpointRouteBuilder builder, DaprOptions daprOptions)
Expand All @@ -132,7 +131,6 @@ private static void EnsureDaprSubscribeHandlerMapped(this IEndpointRouteBuilder

builder.MapSubscribeHandler();
daprOptions.IsDaprSubscribeHandlerMapped = true;
return;
}

private static DaprOptions GetDaprOptions(this IEndpointRouteBuilder builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,24 +148,29 @@ private async Task<bool> SaveEntitiesInternalAsync(
bool dispatchDomainEventFirst = false,
CancellationToken cancellationToken = default)
{
var entities = Context.ExtractDomainEventSources();
var domainEvents = entities.SelectMany(x => x.DomainEvents!.OfType<DomainEvent>()).ToList();
entities.ForEach(x => x.ClearDomainEvents());
if (dispatchDomainEventFirst)
{
await ExtraAndPublishDomainEventsAsync();
await SaveChangesAsync(cancellationToken);
}

await BeforeDispatchDomainEventAsync(domainEvents, Context);
await _mediator.DispatchDomainEventsAsync(domainEvents);
if (dispatchDomainEventFirst == false)
else
{
await SaveChangesAsync(cancellationToken);
await ExtraAndPublishDomainEventsAsync();
}

return true;
}

private async Task ExtraAndPublishDomainEventsAsync()
{
var entities = Context.ExtractDomainEventSources();
var domainEvents = entities.SelectMany(x => x.DomainEvents!.OfType<DomainEvent>()).ToList();
entities.ForEach(x => x.ClearDomainEvents());
await BeforeDispatchDomainEventAsync(domainEvents, Context);
await _mediator.DispatchDomainEventsAsync(domainEvents);
}

private void CallBeforeUpdate()
{
var domainEntities = Context.ChangeTracker
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Diagnostics;
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
using Cnblogs.Architecture.TestIntegrationEvents;
using MediatR;

namespace Cnblogs.Architecture.IntegrationTestProject.EventHandlers;

Expand Down
1 change: 0 additions & 1 deletion test/Cnblogs.Architecture.IntegrationTests/DaprTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Cnblogs.Architecture.TestIntegrationEvents;
using FluentAssertions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
using Cnblogs.Architecture.IntegrationTestProject.EventHandlers;
using Cnblogs.Architecture.TestIntegrationEvents;
using FluentAssertions;
using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Xunit.Abstractions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Cnblogs.Architecture.TestIntegrationEvents;
using FluentAssertions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;

namespace Cnblogs.Architecture.UnitTests.EventBus;
Expand Down

0 comments on commit a5cb14b

Please sign in to comment.