Skip to content

Commit

Permalink
feat: register DaprEventBus with integration event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
cnblogs-dudu committed Feb 8, 2023
1 parent 0bdda4e commit 95082ec
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
using Cnblogs.Architecture.Ddd.EventBus.Dapr;
using Dapr.Client;
using MediatR;
using System.Reflection;

namespace Microsoft.Extensions.DependencyInjection;

Expand All @@ -14,12 +16,23 @@ public static class DaprEventBusServiceCollectionExtensions
/// </summary>
/// <param name="services"><see cref="IServiceCollection"/></param>
/// <param name="appName">The app name used when publishing integration events.</param>
/// <param name="assemblies">Assemblies to scan by MediatR</param>
/// <returns></returns>
public static IServiceCollection AddDaprEventBus(this IServiceCollection services, string appName)
public static IServiceCollection AddDaprEventBus(
this IServiceCollection services,
string appName,
params Assembly[] assemblies)
{
services.Configure<DaprOptions>(o => o.AppName = appName);
services.AddControllers().AddDapr();
services.AddScoped<IEventBus, DaprEventBus>();

if (assemblies.Length > 0)
{
services.AddMediatR(assemblies);
}

return services;
}

}
8 changes: 3 additions & 5 deletions test/Cnblogs.Architecture.IntegrationTestProject/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddCqrs(
Assembly.GetExecutingAssembly(),
typeof(TestIntegrationEvent).Assembly)
.AddDefaultDateTimeAndRandomProvider();
builder.Services.AddDaprEventBus(Constants.AppName);
builder.Services.AddCqrs(Assembly.GetExecutingAssembly(), typeof(TestIntegrationEvent).Assembly)
.AddDefaultDateTimeAndRandomProvider()
.AddDaprEventBus(Constants.AppName);
builder.Services.AddControllers().AddCqrsModelBinderProvider();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,29 @@
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;

namespace Cnblogs.Architecture.IntegrationTests;

[Collection(IntegrationTestCollection.Name)]
public class IntegrationEventHandlerTests
{
private readonly IntegrationTestFactory _factory;

public IntegrationEventHandlerTests(IntegrationTestFactory factory)
{
_factory = factory;
}

[Fact]
public async Task IntegrationEventHandler_TestIntegrationEvent_SuccessAsync()
{
// Arrange
var client = _factory.CreateClient();
var builder = WebApplication.CreateBuilder();
builder.Services
.AddDaprEventBus(nameof(IntegrationEventHandlerTests), typeof(TestIntegrationEventHandler).Assembly)
.AddHttpContextAccessor();
builder.WebHost.UseTestServer();
var app = builder.Build();
app.Subscribe<TestIntegrationEvent>();
await app.StartAsync();
var client = app.GetTestClient();

// Act
var subscriptions = await client.GetFromJsonAsync<Subscription[]>("/dapr/subscribe");
Expand Down

0 comments on commit 95082ec

Please sign in to comment.