using EventSubscriber.Application; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; namespace Projection.ReadModel; public static class ServiceCollectionExtensions { /// Register the projection against Postgres. public static IServiceCollection AddProjectionReadModel(this IServiceCollection services, string connectionString) => services.AddDbContext(o => o.UseNpgsql(connectionString)); /// Register the write-side ports (projector store + notification log) used by the Event Subscriber. public static IServiceCollection AddProjectionWriteSide(this IServiceCollection services) { services.AddScoped(); services.AddScoped(); services.AddScoped(); return services; } /// Apply any pending EF migrations. Called once on service start so a fresh stack /// reaches a usable schema without a manual migration step (DoD: compose up reaches green). public static async Task MigrateProjectionAsync(this IServiceProvider services, CancellationToken ct = default) { await using var scope = services.CreateAsyncScope(); var db = scope.ServiceProvider.GetRequiredService(); await db.Database.MigrateAsync(ct); } }