feat: tenant门店管理首批接口落地
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 30s
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 30s
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using TakeoutSaaS.Domain.Stores.Repositories;
|
||||
using TakeoutSaaS.Infrastructure.App.Persistence;
|
||||
using TakeoutSaaS.Infrastructure.App.Repositories;
|
||||
|
||||
namespace TakeoutSaaS.Infrastructure.App.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// 门店模块基础设施注入扩展。
|
||||
/// </summary>
|
||||
public static class AppServiceCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 注册门店模块所需的 DbContext 与仓储。
|
||||
/// </summary>
|
||||
/// <param name="services">服务集合。</param>
|
||||
/// <param name="configuration">配置源。</param>
|
||||
/// <returns>服务集合。</returns>
|
||||
public static IServiceCollection AddAppInfrastructure(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
// 1. 读取业务库连接串
|
||||
var connectionString = ResolveAppDatabaseConnectionString(configuration);
|
||||
|
||||
// 2. 注册门店业务 DbContext
|
||||
services.AddDbContext<TakeoutTenantAppDbContext>(options =>
|
||||
{
|
||||
options.UseNpgsql(connectionString);
|
||||
});
|
||||
|
||||
// 3. 注册门店仓储
|
||||
services.AddScoped<IStoreRepository, EfStoreRepository>();
|
||||
return services;
|
||||
}
|
||||
|
||||
private static string ResolveAppDatabaseConnectionString(IConfiguration configuration)
|
||||
{
|
||||
// 1. 优先读取新结构配置
|
||||
var writeConnection = configuration["Database:DataSources:AppDatabase:Write"];
|
||||
if (!string.IsNullOrWhiteSpace(writeConnection))
|
||||
{
|
||||
return writeConnection;
|
||||
}
|
||||
|
||||
// 2. 兼容 ConnectionStrings 配置
|
||||
var fallbackConnection = configuration.GetConnectionString("AppDatabase");
|
||||
if (!string.IsNullOrWhiteSpace(fallbackConnection))
|
||||
{
|
||||
return fallbackConnection;
|
||||
}
|
||||
|
||||
// 3. 未配置时抛出异常
|
||||
throw new InvalidOperationException("缺少业务库连接配置:Database:DataSources:AppDatabase:Write");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user