From 202098b723a15e8baed80e3234996c0739608079 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Tue, 28 May 2024 06:43:24 +0300 Subject: [PATCH] refactor: move the functionality to create a persistence database on Enpoint --- Persistence/DependencyInjection.cs | 54 ++++++++-------------------- Persistence/Properties/DbSettings.cs | 15 -------- 2 files changed, 14 insertions(+), 55 deletions(-) delete mode 100644 Persistence/Properties/DbSettings.cs diff --git a/Persistence/DependencyInjection.cs b/Persistence/DependencyInjection.cs index ed6f554..f98052c 100644 --- a/Persistence/DependencyInjection.cs +++ b/Persistence/DependencyInjection.cs @@ -1,54 +1,26 @@ 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) + public static IServiceCollection AddPersistence(this IServiceCollection services, string connection) { - var settings = configuration.GetSection(nameof(DbSettings)).Get(); - var connection = settings?.ConnectionStringSql; + 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); - 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); - - services.AddDbContext(dbConfig); - } - else - throw new NotSupportedException("Unsupported database type"); + services.AddDbContext(DbConfig); services.AddScoped(provider => provider.GetService()!); services.AddScoped(provider => provider.GetService()!); @@ -62,5 +34,7 @@ public static class DependencyInjection services.AddScoped(provider => provider.GetService()!); return services; + + void DbConfig(DbContextOptionsBuilder options) => options.UseSqlite(connection); } } \ No newline at end of file diff --git a/Persistence/Properties/DbSettings.cs b/Persistence/Properties/DbSettings.cs deleted file mode 100644 index 7ee06d9..0000000 --- a/Persistence/Properties/DbSettings.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Mirea.Api.DataAccess.Persistence.Properties; - -public enum DatabaseEnum -{ - Mysql, - Sqlite, - PostgresSql -} -public class DbSettings -{ - public bool IsDoneConfiguration { get; set; } - public DatabaseEnum TypeDatabase { get; set; } - public required string ConnectionStringSql { get; set; } - public DatabaseEnum? MigrateTo { get; set; } -} \ No newline at end of file