diff --git a/Domain.Test/Data/ProvidersTest.cs b/Domain.Test/Data/ProvidersTest.cs index e100a3cc..f61dab97 100644 --- a/Domain.Test/Data/ProvidersTest.cs +++ b/Domain.Test/Data/ProvidersTest.cs @@ -1,6 +1,4 @@ -using System; using Kaizen.Domain.Data; -using Kaizen.Domain.Data.Configuration; using Kaizen.Domain.Extensions; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; @@ -12,34 +10,31 @@ namespace Domain.Test.Data [TestFixture] public class ProvidersTest { - ApplicationDbContext dbContext; + private ApplicationDbContext _dbContext; [OneTimeSetUp] public void Init() { IConfiguration configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json", false, true) - .AddEnvironmentVariables() - .Build(); + .AddEnvironmentVariables() + .Build(); ServiceCollection services = new ServiceCollection(); services.AddOptions(); - services.Configure(c => - { - c.Provider = (DataProvider)Enum.Parse(typeof(DataProvider), configuration["Data:Provider"]); - }); - services.Configure(configuration.GetSection("ConnectionStrings")); + services.LoadDbSettings(configuration); services.RegisterDbContext(configuration); ServiceProvider serviceProvider = services.BuildServiceProvider(); - dbContext = serviceProvider.GetService(); + _dbContext = serviceProvider.GetService(); } [Test] public void CheckDbContext() { - Assert.NotNull(dbContext); - Assert.AreEqual("Microsoft.EntityFrameworkCore.Sqlite", dbContext.Database.ProviderName, "The ApplicationDbContext mush be have a Sqlite provider"); - Assert.IsTrue(dbContext.Database.IsSqlite()); + Assert.NotNull(_dbContext); + Assert.AreEqual("Microsoft.EntityFrameworkCore.Sqlite", _dbContext.Database.ProviderName, + "The ApplicationDbContext mush be have a Sqlite provider"); + Assert.IsTrue(_dbContext.Database.IsSqlite()); } } } diff --git a/Domain/Domain.csproj b/Domain/Domain.csproj index dfdc71e4..7c2789d0 100644 --- a/Domain/Domain.csproj +++ b/Domain/Domain.csproj @@ -22,6 +22,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Domain/Extensions/ServiceCollectionExtensions.cs b/Domain/Extensions/ServiceCollectionExtensions.cs index 1ab12215..50b84397 100644 --- a/Domain/Extensions/ServiceCollectionExtensions.cs +++ b/Domain/Extensions/ServiceCollectionExtensions.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Reflection; using Kaizen.Domain.Data; +using Kaizen.Domain.Data.Configuration; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -10,14 +11,25 @@ namespace Kaizen.Domain.Extensions { public static class ServiceCollectionExtensions { - public static IServiceCollection RegisterDbContext(this IServiceCollection services, IConfiguration configuration) + public static void LoadDbSettings(this IServiceCollection services, IConfiguration configuration) + { + services.Configure(c => + { + c.Provider = (DataProvider) Enum.Parse(typeof(DataProvider), configuration["Data:Provider"]); + }); + services.Configure(configuration.GetSection("ConnectionStrings")); + } + + public static IServiceCollection RegisterDbContext(this IServiceCollection services, + IConfiguration configuration) { string dataProviderConfig = configuration.GetSection("Data")["Provider"]; string connectionStringConfig = configuration.GetConnectionString("DefaultConnection"); Assembly currentAssembly = typeof(ServiceCollectionExtensions).GetTypeInfo().Assembly; IEnumerable dataProviders = currentAssembly.GetImplementationsOf(); - IDataProvider dataProvider = dataProviders.SingleOrDefault(x => x.Provider.ToString() == dataProviderConfig); + IDataProvider dataProvider = + dataProviders.SingleOrDefault(x => x.Provider.ToString() == dataProviderConfig); return dataProvider?.RegisterDbContext(services, connectionStringConfig); } @@ -28,7 +40,7 @@ private static IEnumerable GetImplementationsOf(this Assembly assembly) .Where(t => t.GetTypeInfo().IsClass && !t.GetTypeInfo().IsAbstract && typeof(T).IsAssignableFrom(t)) .ToList(); - return types.Select(type => (T)Activator.CreateInstance(type)).ToList(); + return types.Select(type => (T) Activator.CreateInstance(type)).ToList(); } } } diff --git a/Infrastructure/Extensions/ServiceCollectionExtensions.cs b/Infrastructure/Extensions/ServiceCollectionExtensions.cs index 44ffae9e..436c21e8 100644 --- a/Infrastructure/Extensions/ServiceCollectionExtensions.cs +++ b/Infrastructure/Extensions/ServiceCollectionExtensions.cs @@ -4,7 +4,6 @@ using Kaizen.Core.Security; using Kaizen.Core.Services; using Kaizen.Domain.Data; -using Kaizen.Domain.Data.Configuration; using Kaizen.Domain.Entities; using Kaizen.Domain.Repositories; using Kaizen.Infrastructure.Identity; @@ -143,15 +142,6 @@ public static void ConfigureTokenGenerator(this IServiceCollection services) services.AddScoped(); } - public static void LoadDbSettings(this IServiceCollection services, IConfiguration configuration) - { - services.Configure(c => - { - c.Provider = (DataProvider)Enum.Parse(typeof(DataProvider), configuration["Data:Provider"]); - }); - services.Configure(configuration.GetSection("ConnectionStrings")); - } - public static void LoadMailSettings(this IServiceCollection services, IConfiguration configuration) { services.Configure(m =>