some changes

This commit is contained in:
john 2025-05-17 23:47:19 +02:00
parent 4ec9720541
commit b47bac67ca
37 changed files with 397 additions and 190 deletions

View file

@ -1,6 +1,11 @@
using Femto.Common.Infrastructure;
using System.Runtime.CompilerServices;
using Femto.Common.Infrastructure;
using Femto.Common.Infrastructure.DbConnection;
using Femto.Common.Infrastructure.Outbox;
using Femto.Common.Integration;
using Femto.Modules.Auth.Contracts.Events;
using Femto.Modules.Blog.Handlers;
using Femto.Modules.Blog.Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
@ -14,21 +19,32 @@ public static class BlogStartup
{
public static void InitializeBlogModule(
this IServiceCollection rootContainer,
string connectionString
string connectionString,
IEventBus bus
)
{
var hostBuilder = Host.CreateDefaultBuilder();
hostBuilder.ConfigureServices(services => ConfigureServices(services, connectionString));
hostBuilder.ConfigureServices(services =>
ConfigureServices(services, connectionString, bus)
);
var host = hostBuilder.Build();
rootContainer.AddHostedService(services => new BlogApplication(host));
rootContainer.AddScoped<IBlogModule>(_ => new BlogModule(host));
bus.Subscribe(
(evt, cancellationToken) => EventSubscriber(evt, host.Services, cancellationToken)
);
}
private static void ConfigureServices(this IServiceCollection services, string connectionString)
private static void ConfigureServices(
this IServiceCollection services,
string connectionString,
IEventPublisher publisher
)
{
services.AddTransient<IDbConnectionFactory>(_ => new DbConnectionFactory(connectionString));
@ -46,6 +62,8 @@ public static class BlogStartup
builder.UseLoggerFactory(loggerFactory);
builder.EnableSensitiveDataLogging();
});
services.AddOutbox<BlogContext, OutboxMessageHandler>();
services.AddMediatR(c =>
{
@ -53,5 +71,42 @@ public static class BlogStartup
});
services.ConfigureDomainServices<BlogContext>();
services.AddSingleton(publisher);
}
private static async Task EventSubscriber(
IEvent evt,
IServiceProvider provider,
CancellationToken cancellationToken
)
{
using var scope = provider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<BlogContext>();
var loggerFactory = scope.ServiceProvider.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger<BlogApplication>();
var publisher = scope.ServiceProvider.GetRequiredService<IPublisher>();
// todo inject these
IEventHandler? handler = evt switch
{
UserWasCreatedIntegrationEvent => new UserCreatedEventHandler(
context,
loggerFactory.CreateLogger<UserCreatedEventHandler>()
),
_ => null,
};
if (handler is null)
return;
await handler.Handle(evt, cancellationToken);
if (context.ChangeTracker.HasChanges())
{
await context.EmitDomainEvents(logger, publisher, cancellationToken);
await context.SaveChangesAsync(cancellationToken);
}
}
}