SPMS_API/SPMS.Infrastructure/DependencyInjection.cs
2026-02-10 16:14:47 +09:00

82 lines
3.3 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SPMS.Application.Interfaces;
using SPMS.Application.Settings;
using SPMS.Domain.Interfaces;
using SPMS.Infrastructure.Auth;
using SPMS.Infrastructure.Caching;
using SPMS.Infrastructure.Messaging;
using SPMS.Infrastructure.Persistence;
using SPMS.Infrastructure.Push;
using SPMS.Infrastructure.Persistence.Repositories;
using SPMS.Infrastructure.Security;
using SPMS.Infrastructure.Services;
using SPMS.Infrastructure.Workers;
namespace SPMS.Infrastructure;
public static class DependencyInjection
{
public static IServiceCollection AddInfrastructure(
this IServiceCollection services,
IConfiguration configuration)
{
// DbContext
var connectionString = configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<AppDbContext>(options =>
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
// UnitOfWork & Repositories
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddScoped<IServiceRepository, ServiceRepository>();
services.AddScoped<IAdminRepository, AdminRepository>();
services.AddScoped<INoticeRepository, NoticeRepository>();
services.AddScoped<IBannerRepository, BannerRepository>();
services.AddScoped<IFaqRepository, FaqRepository>();
services.AddScoped<IAppConfigRepository, AppConfigRepository>();
services.AddScoped<IDeviceRepository, DeviceRepository>();
services.AddScoped<IFileRepository, FileRepository>();
services.AddScoped<IMessageRepository, MessageRepository>();
// External Services
services.AddScoped<IJwtService, JwtService>();
services.AddSingleton<IE2EEService, E2EEService>();
services.AddSingleton<ICredentialEncryptionService, CredentialEncryptionService>();
// File Storage
services.AddSingleton<IFileStorageService, LocalFileStorageService>();
// Redis
services.Configure<RedisSettings>(configuration.GetSection(RedisSettings.SectionName));
services.AddSingleton<RedisConnection>();
services.AddSingleton<IDuplicateChecker, DuplicateChecker>();
// RabbitMQ
services.Configure<RabbitMQSettings>(configuration.GetSection(RabbitMQSettings.SectionName));
services.AddSingleton<RabbitMQConnection>();
services.AddHostedService<RabbitMQInitializer>();
services.AddScoped<IPushQueueService, PushQueueService>();
// Push Senders
services.AddSingleton<IFcmSender, FcmSender>();
services.AddHttpClient("ApnsSender")
.ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler
{
EnableMultipleHttp2Connections = true
});
services.AddSingleton<IApnsSender, ApnsSender>();
// Workers
services.AddHostedService<PushWorker>();
// Token Store & Email Service
services.AddMemoryCache();
services.AddSingleton<ITokenStore, InMemoryTokenStore>();
services.AddSingleton<IEmailService, ConsoleEmailService>();
return services;
}
}