some changes
This commit is contained in:
parent
4ec9720541
commit
b47bac67ca
37 changed files with 397 additions and 190 deletions
|
@ -1,4 +1,5 @@
|
|||
using Femto.Common.Infrastructure.Outbox;
|
||||
using Femto.Modules.Blog.Domain.Authors;
|
||||
using Femto.Modules.Blog.Domain.Posts;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
|
@ -7,6 +8,7 @@ namespace Femto.Modules.Blog.Application;
|
|||
internal class BlogContext(DbContextOptions<BlogContext> options) : DbContext(options), IOutboxContext
|
||||
{
|
||||
public virtual DbSet<Post> Posts { get; set; }
|
||||
public virtual DbSet<Author> Authors { get; set; }
|
||||
public virtual DbSet<OutboxEntry> Outbox { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
using Femto.Modules.Blog.Domain.Authors;
|
||||
using Femto.Modules.Blog.Domain.Posts;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Femto.Modules.Blog.Application.Configurations;
|
||||
|
||||
internal class AuthorConfiguration : IEntityTypeConfiguration<Author>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Author> table)
|
||||
{
|
||||
table.ToTable("author");
|
||||
}
|
||||
}
|
|
@ -9,7 +9,5 @@ internal class OutboxEntryConfiguration : IEntityTypeConfiguration<OutboxEntry>
|
|||
public void Configure(EntityTypeBuilder<OutboxEntry> builder)
|
||||
{
|
||||
builder.ToTable("outbox");
|
||||
|
||||
builder.Property(x => x.Payload).HasColumnType("jsonb");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue