1
0
mirror of synced 2025-02-17 19:19:18 +01:00
TaikoLocalServer/Infrastructure/DependencyInjection.cs
2024-11-20 20:05:53 +08:00

34 lines
1.2 KiB
C#

using Application.Interfaces;
using Domain.Common;
using Infrastructure.Persistence;
using Infrastructure.Services;
using Infrastructure.Utils;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Infrastructure;
public static class DependencyInjection
{
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
{
services.AddSingleton<IJwtTokenService, JwtTokenService>();
services.AddSingleton<IGameDataService, GameDataService>();
services.AddDbContext<TaikoDbContext>(option =>
{
var dbName = configuration["DbFileName"];
if (string.IsNullOrEmpty(dbName))
{
dbName = Constants.DefaultDbName;
}
var path = Path.Combine(PathHelper.GetRootPath(), dbName);
option.UseSqlite($"Data Source={path}");
});
services.AddScoped<ITaikoDbContext, TaikoDbContext>(provider =>
provider.GetService<TaikoDbContext>() ?? throw new InvalidOperationException());
return services;
}
}