From b8728cd4907fac550f71a2649fe5dccf40d3bdd4 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Thu, 30 May 2024 20:17:36 +0300 Subject: [PATCH] feat: add factory DbContext for configuration by provider --- SqlData/Persistence/DependencyInjection.cs | 50 ++++++++++++++++------ 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/SqlData/Persistence/DependencyInjection.cs b/SqlData/Persistence/DependencyInjection.cs index 2ebae49..d200bfe 100644 --- a/SqlData/Persistence/DependencyInjection.cs +++ b/SqlData/Persistence/DependencyInjection.cs @@ -1,24 +1,41 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; +using Mirea.Api.DataAccess.Domain.Schedule; +using Mirea.Api.DataAccess.Persistence.Common; using Mirea.Api.DataAccess.Persistence.Contexts.Schedule; +using Mirea.Api.DataAccess.Persistence.EntityTypeConfigurations; +using System; +using System.Linq; +using System.Reflection; namespace Mirea.Api.DataAccess.Persistence; public static class DependencyInjection { - public static IServiceCollection AddPersistence(this IServiceCollection services, string connection) + public static IServiceCollection AddPersistence(this IServiceCollection services, DatabaseProvider dbProvider, string connection) { - 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(options => + UseDatabase(options).CreateDbContext(dbProvider)); + services.AddDbContext(options => + UseDatabase(options).CreateDbContext(dbProvider)); + services.AddDbContext(options => + UseDatabase(options).CreateDbContext(dbProvider)); + services.AddDbContext(options => + UseDatabase(options).CreateDbContext(dbProvider)); + services.AddDbContext(options => + UseDatabase(options).CreateDbContext(dbProvider)); + services.AddDbContext(options => + UseDatabase(options).CreateDbContext(dbProvider)); + services.AddDbContext(options => + UseDatabase(options).CreateDbContext(dbProvider)); + services.AddDbContext(options => + UseDatabase(options).CreateDbContext(dbProvider)); + services.AddDbContext(options => + UseDatabase(options).CreateDbContext(dbProvider)); + services.AddDbContext(options => + UseDatabase(options).CreateDbContext(dbProvider)); + services.AddDbContext(DbConfig); @@ -35,6 +52,15 @@ public static class DependencyInjection return services; - void DbConfig(DbContextOptionsBuilder options) => options.UseSqlite(connection); + DbContextOptionsBuilder UseDatabase(DbContextOptionsBuilder options) + { + return dbProvider switch + { + DatabaseProvider.Sqlite => options.UseSqlite(connection), + DatabaseProvider.Mysql => options.UseMySql(connection, ServerVersion.AutoDetect(connection)), + DatabaseProvider.Postgresql => options.UseNpgsql(connection), + _ => throw new ArgumentException("Unsupported database provider", Enum.GetName(dbProvider)) + }; + } } } \ No newline at end of file