From 806c3eeb17d403fa27970fa68b10996a4930af98 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Fri, 26 Jan 2024 19:39:59 +0300 Subject: [PATCH] feat: add an initial DI container for working with Persistence --- Persistence/DependencyInjection.cs | 64 ++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Persistence/DependencyInjection.cs diff --git a/Persistence/DependencyInjection.cs b/Persistence/DependencyInjection.cs new file mode 100644 index 0000000..9ec71f1 --- /dev/null +++ b/Persistence/DependencyInjection.cs @@ -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(); + var connection = settings?.ConnectionStringSql; + + Dictionary> 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(dbConfig); + services.AddDbContext(dbConfig); + services.AddDbContext(dbConfig); + services.AddDbContext(dbConfig); + services.AddDbContext(dbConfig); + services.AddDbContext(dbConfig); + services.AddDbContext(dbConfig); + services.AddDbContext(dbConfig); + services.AddDbContext(dbConfig); + + services.AddDbContext(dbConfig); + } + else + throw new NotSupportedException("Unsupported database type"); + + services.AddScoped(provider => provider.GetService()!); + services.AddScoped(provider => provider.GetService()!); + services.AddScoped(provider => provider.GetService()!); + services.AddScoped(provider => provider.GetService()!); + services.AddScoped(provider => provider.GetService()!); + services.AddScoped(provider => provider.GetService()!); + services.AddScoped(provider => provider.GetService()!); + services.AddScoped(provider => provider.GetService()!); + services.AddScoped(provider => provider.GetService()!); + + return services; + } +} \ No newline at end of file