some changes
This commit is contained in:
parent
4ec9720541
commit
b47bac67ca
37 changed files with 397 additions and 190 deletions
|
@ -1,34 +1,44 @@
|
|||
using Femto.Common.Infrastructure;
|
||||
using Femto.Common.Infrastructure.Outbox;
|
||||
using Femto.Common.Integration;
|
||||
using Femto.Modules.Auth.Data;
|
||||
using Femto.Modules.Auth.Infrastructure;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
|
||||
namespace Femto.Modules.Auth.Application;
|
||||
|
||||
public static class AuthStartup
|
||||
{
|
||||
public static void InitializeAuthenticationModule(
|
||||
this IServiceCollection rootContainer,
|
||||
string connectionString
|
||||
)
|
||||
public static void InitializeAuthenticationModule(this IServiceCollection rootContainer,
|
||||
string connectionString, IEventBus eventBus)
|
||||
{
|
||||
var hostBuilder = Host.CreateDefaultBuilder();
|
||||
hostBuilder.ConfigureServices(services => ConfigureServices(services, connectionString));
|
||||
hostBuilder.ConfigureServices(services => ConfigureServices(services, connectionString, eventBus));
|
||||
var host = hostBuilder.Build();
|
||||
rootContainer.AddScoped<IAuthModule>(_ => new AuthModule(host));
|
||||
rootContainer.AddHostedService(services => new AuthApplication(host));
|
||||
eventBus.Subscribe((evt, cancellationToken) => EventSubscriber(evt, host.Services, cancellationToken));
|
||||
}
|
||||
|
||||
private static void ConfigureServices(IServiceCollection services, string connectionString)
|
||||
private static void ConfigureServices(IServiceCollection services, string connectionString, IEventPublisher publisher)
|
||||
{
|
||||
services.AddDbContext<AuthContext>(builder =>
|
||||
{
|
||||
builder.UseNpgsql(connectionString);
|
||||
builder.UseSnakeCaseNamingConvention();
|
||||
});
|
||||
|
||||
services.AddQuartzHostedService(options =>
|
||||
{
|
||||
options.WaitForJobsToComplete = true;
|
||||
});
|
||||
|
||||
services.AddOutbox<AuthContext, OutboxMessageHandler>();
|
||||
|
||||
services.AddMediatR(c => c.RegisterServicesFromAssembly(typeof(AuthStartup).Assembly));
|
||||
|
||||
|
@ -41,9 +51,35 @@ public static class AuthStartup
|
|||
|
||||
services.ConfigureDomainServices<AuthContext>();
|
||||
|
||||
services.AddMediatR(c =>
|
||||
services.AddSingleton(publisher);
|
||||
}
|
||||
|
||||
private static async Task EventSubscriber(
|
||||
IEvent evt,
|
||||
IServiceProvider provider,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
using var scope = provider.CreateScope();
|
||||
|
||||
var context = scope.ServiceProvider.GetRequiredService<AuthContext>();
|
||||
var logger = scope.ServiceProvider.GetRequiredService<ILogger<AuthApplication>>();
|
||||
var publisher = scope.ServiceProvider.GetRequiredService<IPublisher>();
|
||||
|
||||
IEventHandler? handler = evt switch
|
||||
{
|
||||
c.RegisterServicesFromAssembly(typeof(AuthStartup).Assembly);
|
||||
});
|
||||
_ => 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue