feat: add an initial DI container for working with Persistence
All checks were successful
.NET Test Pipeline / build-and-test (pull_request) Successful in 1m19s

This commit is contained in:
Polianin Nikita 2024-01-26 19:39:59 +03:00
parent 3294325ad0
commit 806c3eeb17

View File

@ -0,0 +1,64 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule;
using Mirea.Api.DataAccess.Persistence.Contexts.Schedule;
using Mirea.Api.DataAccess.Persistence.Properties;
using System;
using System.Collections.Generic;
namespace Mirea.Api.DataAccess.Persistence;
public static class DependencyInjection
{
public static IServiceCollection AddPersistence(this IServiceCollection services, IConfiguration configuration)
{
var settings = configuration.GetSection(nameof(DbSettings)).Get<DbSettings>();
var connection = settings?.ConnectionStringSql;
Dictionary<DatabaseEnum, Action<DbContextOptionsBuilder>> dbConfigurations = new()
{
{
DatabaseEnum.Mysql,
options => options.UseMySql(connection, ServerVersion.AutoDetect(connection))
},
{
DatabaseEnum.Sqlite,
options => options.UseSqlite(connection)
},
{
DatabaseEnum.PostgresSql,
options => options.UseNpgsql(connection)
}
};
if (dbConfigurations.TryGetValue((DatabaseEnum)settings?.TypeDatabase!, out var dbConfig))
{
services.AddDbContext<CampusDbContext>(dbConfig);
services.AddDbContext<DisciplineDbContext>(dbConfig);
services.AddDbContext<FacultyDbContext>(dbConfig);
services.AddDbContext<GroupDbContext>(dbConfig);
services.AddDbContext<LectureHallDbContext>(dbConfig);
services.AddDbContext<LessonAssociationDbContext>(dbConfig);
services.AddDbContext<ProfessorDbContext>(dbConfig);
services.AddDbContext<LessonDbContext>(dbConfig);
services.AddDbContext<TypeOfOccupationDbContext>(dbConfig);
services.AddDbContext<UberDbContext>(dbConfig);
}
else
throw new NotSupportedException("Unsupported database type");
services.AddScoped<ICampusDbContext>(provider => provider.GetService<CampusDbContext>()!);
services.AddScoped<IDisciplineDbContext>(provider => provider.GetService<DisciplineDbContext>()!);
services.AddScoped<IFacultyDbContext>(provider => provider.GetService<FacultyDbContext>()!);
services.AddScoped<IGroupDbContext>(provider => provider.GetService<GroupDbContext>()!);
services.AddScoped<ILectureHallDbContext>(provider => provider.GetService<LectureHallDbContext>()!);
services.AddScoped<ILessonAssociationDbContext>(provider => provider.GetService<LessonAssociationDbContext>()!);
services.AddScoped<IProfessorDbContext>(provider => provider.GetService<ProfessorDbContext>()!);
services.AddScoped<ILessonDbContext>(provider => provider.GetService<LessonDbContext>()!);
services.AddScoped<ITypeOfOccupationDbContext>(provider => provider.GetService<TypeOfOccupationDbContext>()!);
return services;
}
}