34 lines
1.1 KiB
C#
Executable File
34 lines
1.1 KiB
C#
Executable File
using AssetManager.Data.Repositories;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using SqlSugar;
|
||
|
||
namespace AssetManager.Data;
|
||
|
||
public static class DatabaseExtensions
|
||
{
|
||
public static IServiceCollection AddDatabase(this IServiceCollection services)
|
||
{
|
||
// 使用 Singleton 注册 SqlSugarScope(线程安全,内部使用 AsyncLocal)
|
||
// SqlSugarScope 设计上支持并发,会自动管理连接池
|
||
services.AddSingleton<ISqlSugarClient>(s =>
|
||
{
|
||
return SqlSugarConfig.GetSqlSugarClient();
|
||
});
|
||
|
||
services.AddScoped<DatabaseService>();
|
||
|
||
// Repository 层
|
||
services.AddScoped<IPortfolioRepository, PortfolioRepository>();
|
||
services.AddScoped<IMarketDataRepository, MarketDataRepository>();
|
||
|
||
return services;
|
||
}
|
||
|
||
public static void InitializeDatabase(this IServiceProvider serviceProvider)
|
||
{
|
||
using var scope = serviceProvider.CreateScope();
|
||
var dbService = scope.ServiceProvider.GetRequiredService<DatabaseService>();
|
||
dbService.InitializeDatabase();
|
||
}
|
||
}
|