fix injectionses

This commit is contained in:
john 2025-05-21 00:19:49 +02:00
parent b93115d787
commit cd078ca643
11 changed files with 119 additions and 55 deletions

View file

@ -1,37 +1,29 @@
using Femto.Common.Domain;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Femto.Modules.Blog.Application;
internal class BlogModule(IHost host) : IBlogModule
internal class BlogModule(IMediator mediator) : IBlogModule
{
public async Task PostCommand(ICommand command, CancellationToken cancellationToken = default)
public async Task Command(ICommand command, CancellationToken cancellationToken = default)
{
using var scope = host.Services.CreateScope();
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
await mediator.Send(command, cancellationToken);
}
public async Task<TResponse> PostCommand<TResponse>(
public async Task<TResponse> Command<TResponse>(
ICommand<TResponse> command,
CancellationToken cancellationToken = default
)
{
using var scope = host.Services.CreateScope();
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
var response = await mediator.Send(command, cancellationToken);
return response;
}
public async Task<TResponse> PostQuery<TResponse>(
public async Task<TResponse> Query<TResponse>(
IQuery<TResponse> query,
CancellationToken cancellationToken = default
)
{
using var scope = host.Services.CreateScope();
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
var response = await mediator.Send(query, cancellationToken);
return response;
}

View file

@ -1,4 +1,5 @@
using System.Runtime.CompilerServices;
using Femto.Common;
using Femto.Common.Infrastructure;
using Femto.Common.Infrastructure.DbConnection;
using Femto.Common.Infrastructure.Outbox;
@ -20,20 +21,24 @@ public static class BlogStartup
public static void InitializeBlogModule(
this IServiceCollection rootContainer,
string connectionString,
IEventBus bus
IEventBus bus,
ILoggerFactory loggerFactory
)
{
var hostBuilder = Host.CreateDefaultBuilder();
hostBuilder.ConfigureServices(services =>
ConfigureServices(services, connectionString, bus)
ConfigureServices(services, connectionString, bus, loggerFactory)
);
var host = hostBuilder.Build();
rootContainer.AddHostedService(services => new BlogApplication(host));
rootContainer.AddHostedService(_ => new BlogApplication(host));
rootContainer.AddScoped<IBlogModule>(_ => new BlogModule(host));
rootContainer.AddScoped(_ => new ScopeBinding<IBlogModule>(host.Services.CreateScope()));
rootContainer.AddScoped(services =>
services.GetRequiredService<ScopeBinding<IBlogModule>>().GetService()
);
bus.Subscribe(
(evt, cancellationToken) => EventSubscriber(evt, host.Services, cancellationToken)
@ -43,7 +48,8 @@ public static class BlogStartup
private static void ConfigureServices(
this IServiceCollection services,
string connectionString,
IEventPublisher publisher
IEventPublisher publisher,
ILoggerFactory loggerFactory
)
{
services.AddTransient<IDbConnectionFactory>(_ => new DbConnectionFactory(connectionString));
@ -58,10 +64,12 @@ public static class BlogStartup
}
);
builder.UseSnakeCaseNamingConvention();
var loggerFactory = LoggerFactory.Create(b => { });
builder.UseLoggerFactory(loggerFactory);
});
services.AddSingleton(loggerFactory);
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
services.AddOutbox<BlogContext, OutboxMessageHandler>();
services.AddMediatR(c =>
@ -71,6 +79,8 @@ public static class BlogStartup
services.ConfigureDomainServices<BlogContext>();
services.AddScoped<IBlogModule, BlogModule>();
services.AddSingleton(publisher);
}

View file

@ -0,0 +1,5 @@
using Femto.Common.Domain;
namespace Femto.Modules.Blog.Application.Commands.DeletePost;
public record DeletePostCommand(Guid PostId, Guid InitiatingUserId) : ICommand;

View file

@ -4,14 +4,14 @@ namespace Femto.Modules.Blog.Application;
public interface IBlogModule
{
Task PostCommand(ICommand command, CancellationToken cancellationToken = default);
Task Command(ICommand command, CancellationToken cancellationToken = default);
Task<TResponse> PostCommand<TResponse>(
Task<TResponse> Command<TResponse>(
ICommand<TResponse> command,
CancellationToken cancellationToken = default
);
Task<TResponse> PostQuery<TResponse>(
Task<TResponse> Query<TResponse>(
IQuery<TResponse> query,
CancellationToken cancellationToken = default
);