Configure ASP.NET for the API to work #7
64
Persistence/DependencyInjection.cs
Normal file
64
Persistence/DependencyInjection.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user