using Microsoft.EntityFrameworkCore; using Prefab.Catalog.Data; using Prefab.Catalog.Api.Data; using Prefab.Data; using Prefab.Module; namespace Prefab.Catalog.Api; public class Module : IModule { public WebApplicationBuilder Build(WebApplicationBuilder builder) { var writeConnection = builder.Configuration.GetConnectionString("PrefabDb") ?? throw new InvalidOperationException("Connection string 'PrefabDb' not found. Ensure the Aspire AppHost exposes the SQL resource."); var readConnection = builder.Configuration.GetConnectionString("PrefabDbReadOnly") ?? writeConnection; builder.Services.AddSingleton(); builder.Services.AddDbContext(options => options.UseSqlServer(writeConnection, sqlOptions => sqlOptions.EnableRetryOnFailure())); builder.Services.AddDbContext(options => options.UseSqlServer(readConnection, sqlOptions => sqlOptions.EnableRetryOnFailure())); builder.Services.AddDbContextFactory(options => options.UseSqlServer(writeConnection, sqlOptions => sqlOptions.EnableRetryOnFailure()), ServiceLifetime.Scoped); builder.Services.AddDbContextFactory(options => options.UseSqlServer(readConnection, sqlOptions => sqlOptions.EnableRetryOnFailure()), ServiceLifetime.Scoped); builder.Services.AddModuleDbInterfaces(typeof(AppDb)); builder.Services.AddModuleDbInterfaces(typeof(AppDbReadOnly)); builder.Services.AddScoped(); //builder.Services.AddHostedService(); return builder; } public WebApplication Configure(WebApplication app) { return app; } }