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

feat: register DaprEventBus with integration event handlers #16

Merged
merged 1 commit into from
Feb 8, 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
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;

namespace Microsoft.Extensions.DependencyInjection;

Expand All @@ -10,16 +13,28 @@ namespace Microsoft.Extensions.DependencyInjection;
public static class DaprEventBusServiceCollectionExtensions
{
/// <summary>
/// Register <see cref="DaprClient"/> and <see cref="IEventBus"/>.
/// Register <see cref="DaprClient"/> and <see cref="IEventBus"/>.
/// The alternative is using services.AddCqrs().AddDaprEventBus() in <see cref="CqrsInjectorExtensions"/>.
/// </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
@@ -1,28 +1,40 @@
using System.Net.Http.Json;
using Cnblogs.Architecture.IntegrationTestProject;
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;

namespace Cnblogs.Architecture.IntegrationTests;

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

public IntegrationEventHandlerTests(IntegrationTestFactory factory, ITestOutputHelper testOutputHelper)
public IntegrationEventHandlerTests(ITestOutputHelper testOutputHelper)
{
_factory = factory;
_testOutputHelper = testOutputHelper;
}

[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();
var @event = new TestIntegrationEvent(Guid.NewGuid(), DateTimeOffset.Now, "Hello World!");

// Act
Expand Down