using Microsoft.EntityFrameworkCore;
using FluentValidation;
using MediatR;
using ColaFlow.Modules.ProjectManagement.Application.Behaviors;
using ColaFlow.Modules.ProjectManagement.Application.Commands.CreateProject;
using ColaFlow.Modules.ProjectManagement.Domain.Repositories;
using ColaFlow.Modules.ProjectManagement.Infrastructure.Persistence;
using ColaFlow.Modules.ProjectManagement.Infrastructure.Repositories;
using ColaFlow.Modules.IssueManagement.Application.Commands.CreateIssue;
using ColaFlow.Modules.IssueManagement.Infrastructure.Persistence;
using ColaFlow.Modules.IssueManagement.Infrastructure.Persistence.Repositories;
using Microsoft.Extensions.Hosting;
namespace ColaFlow.API.Extensions;
///
/// Extension methods for registering modules
///
public static class ModuleExtensions
{
///
/// Register ProjectManagement Module
///
public static IServiceCollection AddProjectManagementModule(
this IServiceCollection services,
IConfiguration configuration,
IHostEnvironment? environment = null)
{
// Only register PostgreSQL DbContext in non-Testing environments
// In Testing environment, WebApplicationFactory will register InMemory provider
if (environment == null || environment.EnvironmentName != "Testing")
{
// Register DbContext
var connectionString = configuration.GetConnectionString("PMDatabase");
services.AddDbContext(options =>
options.UseNpgsql(connectionString));
}
// Register repositories
services.AddScoped();
services.AddScoped();
// Register MediatR handlers from Application assembly (v13.x syntax)
services.AddMediatR(cfg =>
{
cfg.LicenseKey = configuration["MediatR:LicenseKey"];
cfg.RegisterServicesFromAssembly(typeof(CreateProjectCommand).Assembly);
});
// Register FluentValidation validators
services.AddValidatorsFromAssembly(typeof(CreateProjectCommand).Assembly);
// Register pipeline behaviors
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
Console.WriteLine("[ProjectManagement] Module registered");
return services;
}
///
/// Register IssueManagement Module
///
public static IServiceCollection AddIssueManagementModule(
this IServiceCollection services,
IConfiguration configuration,
IHostEnvironment? environment = null)
{
// Only register PostgreSQL DbContext in non-Testing environments
if (environment == null || environment.EnvironmentName != "Testing")
{
// Register DbContext
var connectionString = configuration.GetConnectionString("IMDatabase");
services.AddDbContext(options =>
options.UseNpgsql(connectionString));
}
// Register repositories
services.AddScoped();
services.AddScoped();
// Register MediatR handlers from Application assembly
services.AddMediatR(cfg =>
{
cfg.LicenseKey = configuration["MediatR:LicenseKey"];
cfg.RegisterServicesFromAssembly(typeof(CreateIssueCommand).Assembly);
});
// Register FluentValidation validators
services.AddValidatorsFromAssembly(typeof(CreateIssueCommand).Assembly);
// Register pipeline behaviors
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ColaFlow.Modules.IssueManagement.Application.Behaviors.ValidationBehavior<,>));
Console.WriteLine("[IssueManagement] Module registered");
return services;
}
}