Skip to content

Commit

Permalink
refactor: introduce EnsureEventBusRegistered
Browse files Browse the repository at this point in the history
  • Loading branch information
cnblogs-dudu committed Feb 6, 2023
1 parent 61a5b69 commit 5270c2b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/Cnblogs.Architecture.Ddd.EventBus.Dapr/DaprOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public class DaprOptions
/// <summary>
/// 是否调用过 <c>app.MapSubscribeHandler()</c>
/// </summary>
internal static bool IsDaprSubscribeHandlerMapped { get; set; }
internal bool IsDaprSubscribeHandlerMapped { get; set; }

internal bool IsEventBusRegistered { get; set; }
}
35 changes: 24 additions & 11 deletions src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Cnblogs.Architecture.Ddd.EventBus.Dapr;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

// ReSharper disable once CheckNamespace
namespace Microsoft.AspNetCore.Routing;
Expand Down Expand Up @@ -62,14 +63,9 @@ public static IEndpointConventionBuilder Subscribe<TEvent>(
string appName)
where TEvent : IntegrationEvent
{
EnsureDaprSubscribeHandlerMapped(builder);

var serviceCheck = builder.ServiceProvider.GetRequiredService<IServiceProviderIsService>();
if (!serviceCheck.IsService(typeof(IEventBus)))
{
throw new InvalidOperationException(
$"{nameof(IEventBus)} has not been registered. Did you forget to call IServiceCollection.AddDaprEventBus()?");
}
var daprOptions = builder.ServiceProvider.GetRequiredService<IOptions<DaprOptions>>().Value;
EnsureDaprSubscribeHandlerMapped(builder, daprOptions);
EnsureEventBusRegistered(builder, daprOptions);

var result = builder
.MapPost(route, (TEvent receivedEvent, IEventBus eventBus) => eventBus.ReceiveAsync(receivedEvent))
Expand Down Expand Up @@ -104,9 +100,26 @@ public static void Subscribe(this IEndpointRouteBuilder builder, params Assembly
}
}

private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder builder)
private static void EnsureEventBusRegistered(IEndpointRouteBuilder builder, DaprOptions daprOptions)
{
if (daprOptions.IsEventBusRegistered)
{
return;
}

var serviceCheck = builder.ServiceProvider.GetRequiredService<IServiceProviderIsService>();
if (!serviceCheck.IsService(typeof(IEventBus)))
{
throw new InvalidOperationException(
$"{nameof(IEventBus)} has not been registered. Did you forget to call IServiceCollection.AddDaprEventBus()?");
}

daprOptions.IsEventBusRegistered = true;
}

private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder builder, DaprOptions daprOptions)
{
if (DaprOptions.IsDaprSubscribeHandlerMapped)
if (daprOptions.IsDaprSubscribeHandlerMapped)
{
return;
}
Expand All @@ -117,6 +130,6 @@ private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder build
}

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

0 comments on commit 5270c2b

Please sign in to comment.