Files
prefab-page-detail/Prefab.Catalog.Api/Module.cs
2025-10-27 17:39:18 -04:00

51 lines
1.8 KiB
C#

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<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.AddDbContext<IPrefabDb, AppDb>(options =>
options.UseSqlServer(writeConnection, sqlOptions => sqlOptions.EnableRetryOnFailure()));
builder.Services.AddDbContext<IPrefabDbReadOnly, AppDbReadOnly>(options =>
options.UseSqlServer(readConnection, sqlOptions => sqlOptions.EnableRetryOnFailure()));
builder.Services.AddDbContextFactory<AppDb>(options =>
options.UseSqlServer(writeConnection, sqlOptions => sqlOptions.EnableRetryOnFailure()), ServiceLifetime.Scoped);
builder.Services.AddDbContextFactory<AppDbReadOnly>(options =>
options.UseSqlServer(readConnection, sqlOptions => sqlOptions.EnableRetryOnFailure()), ServiceLifetime.Scoped);
builder.Services.AddModuleDbInterfaces(typeof(AppDb));
builder.Services.AddModuleDbInterfaces(typeof(AppDbReadOnly));
builder.Services.AddScoped<ICatalogDbContextFactory, CatalogDbContextFactory>();
//builder.Services.AddHostedService<DataSeeder>();
return builder;
}
public WebApplication Configure(WebApplication app)
{
return app;
}
}