Skip to content

Commit a17eaec

Browse files
committed
refactor: refactor Subscribe extensions
1 parent cbd8093 commit a17eaec

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,7 @@ public static IEndpointConventionBuilder Subscribe<TEvent>(
6363
string appName)
6464
where TEvent : IntegrationEvent
6565
{
66-
var daprOptions = builder.ServiceProvider.GetRequiredService<IOptions<DaprOptions>>().Value;
67-
EnsureDaprSubscribeHandlerMapped(builder, daprOptions);
68-
EnsureEventBusRegistered(builder, daprOptions);
69-
70-
var result = builder
66+
var result = builder.EnsureProvision()
7167
.MapPost(route, (TEvent receivedEvent, IEventBus eventBus) => eventBus.ReceiveAsync(receivedEvent))
7268
.WithTopic(DaprOptions.PubSubName, DaprUtils.GetDaprTopicName<TEvent>(appName));
7369
return result;
@@ -80,6 +76,8 @@ public static IEndpointConventionBuilder Subscribe<TEvent>(
8076
/// <param name="assemblies"><see cref="Assembly"/></param>
8177
public static void Subscribe(this IEndpointRouteBuilder builder, params Assembly[] assemblies)
8278
{
79+
builder.EnsureProvision();
80+
8381
var method = typeof(EndPointExtensions).GetMethod(
8482
nameof(Subscribe),
8583
new[] { typeof(IEndpointRouteBuilder), typeof(string) })!;
@@ -100,11 +98,11 @@ public static void Subscribe(this IEndpointRouteBuilder builder, params Assembly
10098
}
10199
}
102100

103-
private static void EnsureEventBusRegistered(IEndpointRouteBuilder builder, DaprOptions daprOptions)
101+
private static DaprOptions EnsureEventBusRegistered(this DaprOptions daprOptions, IEndpointRouteBuilder builder)
104102
{
105103
if (daprOptions.IsEventBusRegistered)
106104
{
107-
return;
105+
return daprOptions;
108106
}
109107

110108
var serviceCheck = builder.ServiceProvider.GetRequiredService<IServiceProviderIsService>();
@@ -115,13 +113,14 @@ private static void EnsureEventBusRegistered(IEndpointRouteBuilder builder, Dapr
115113
}
116114

117115
daprOptions.IsEventBusRegistered = true;
116+
return daprOptions;
118117
}
119118

120-
private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder builder, DaprOptions daprOptions)
119+
private static DaprOptions EnsureDaprSubscribeHandlerMapped(this DaprOptions daprOptions, IEndpointRouteBuilder builder)
121120
{
122121
if (daprOptions.IsDaprSubscribeHandlerMapped)
123122
{
124-
return;
123+
return daprOptions;
125124
}
126125

127126
if (builder is IApplicationBuilder app)
@@ -131,5 +130,17 @@ private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder build
131130

132131
builder.MapSubscribeHandler();
133132
daprOptions.IsDaprSubscribeHandlerMapped = true;
133+
return daprOptions;
134+
}
135+
136+
private static DaprOptions GetDaprOptions(this IEndpointRouteBuilder builder)
137+
=> builder.ServiceProvider.GetRequiredService<IOptions<DaprOptions>>().Value;
138+
139+
private static IEndpointRouteBuilder EnsureProvision(this IEndpointRouteBuilder builder)
140+
{
141+
builder.GetDaprOptions()
142+
.EnsureDaprSubscribeHandlerMapped(builder)
143+
.EnsureEventBusRegistered(builder);
144+
return builder;
134145
}
135146
}

test/Cnblogs.Architecture.IntegrationTests/DaprTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,27 @@ public async Task Dapr_SubscribeEndpoint_OkAsync()
3434
// Assert
3535
response.StatusCode.Should().Be(HttpStatusCode.OK);
3636
var responseText = await response.Content.ReadAsStringAsync();
37-
responseText.Should().Contain("pubsub");
37+
responseText.Should().Contain(nameof(TestIntegrationEvent));
38+
}
39+
40+
[Fact]
41+
public async Task Dapr_Subscribe_Without_Any_Assembly_OkAsync()
42+
{
43+
// Arrange
44+
var builder = WebApplication.CreateBuilder();
45+
builder.Services.AddDaprEventBus(nameof(DaprTests));
46+
builder.WebHost.UseTestServer();
47+
48+
var app = builder.Build();
49+
app.Subscribe();
50+
await app.StartAsync();
51+
var httpClient = app.GetTestClient();
52+
53+
// Act
54+
var response = await httpClient.GetAsync("/dapr/subscribe");
55+
56+
// Assert
57+
response.StatusCode.Should().Be(HttpStatusCode.OK);
3858
}
3959

4060
[Fact]

0 commit comments

Comments
 (0)