Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
refactor: Move 'LoadDbSettings' method to 'Kaizen.Domain.Extensions'
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosPavajeau committed Jan 29, 2021
1 parent 7b1963f commit 994481b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
23 changes: 9 additions & 14 deletions Domain.Test/Data/ProvidersTest.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Kaizen.Domain.Data.Configuration.Data>(c =>
{
c.Provider = (DataProvider)Enum.Parse(typeof(DataProvider), configuration["Data:Provider"]);
});
services.Configure<ConnectionStrings>(configuration.GetSection("ConnectionStrings"));
services.LoadDbSettings(configuration);
services.RegisterDbContext(configuration);

ServiceProvider serviceProvider = services.BuildServiceProvider();
dbContext = serviceProvider.GetService<ApplicationDbContext>();
_dbContext = serviceProvider.GetService<ApplicationDbContext>();
}

[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());
}
}
}
1 change: 1 addition & 0 deletions Domain/Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="5.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.1" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.0-alpha.2" />
</ItemGroup>
Expand Down
18 changes: 15 additions & 3 deletions Domain/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,33 @@
using System.Linq;
using System.Reflection;
using Kaizen.Domain.Data;
using Kaizen.Domain.Data.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

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<Data.Configuration.Data>(c =>
{
c.Provider = (DataProvider) Enum.Parse(typeof(DataProvider), configuration["Data:Provider"]);
});
services.Configure<ConnectionStrings>(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<IDataProvider> dataProviders = currentAssembly.GetImplementationsOf<IDataProvider>();

IDataProvider dataProvider = dataProviders.SingleOrDefault(x => x.Provider.ToString() == dataProviderConfig);
IDataProvider dataProvider =
dataProviders.SingleOrDefault(x => x.Provider.ToString() == dataProviderConfig);

return dataProvider?.RegisterDbContext(services, connectionStringConfig);
}
Expand All @@ -28,7 +40,7 @@ private static IEnumerable<T> GetImplementationsOf<T>(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();
}
}
}
10 changes: 0 additions & 10 deletions Infrastructure/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -143,15 +142,6 @@ public static void ConfigureTokenGenerator(this IServiceCollection services)
services.AddScoped<ITokenGenerator, TokenGenerator>();
}

public static void LoadDbSettings(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<Data>(c =>
{
c.Provider = (DataProvider)Enum.Parse(typeof(DataProvider), configuration["Data:Provider"]);
});
services.Configure<ConnectionStrings>(configuration.GetSection("ConnectionStrings"));
}

public static void LoadMailSettings(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<MailSettings>(m =>
Expand Down

0 comments on commit 994481b

Please sign in to comment.