52 lines
2.4 KiB
C#
52 lines
2.4 KiB
C#
using System.Reflection;
|
|
using DBConnection.Contexts;
|
|
using DBConnection.Repositories.Interfaces;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace DBConnection.Extensions;
|
|
|
|
public static class BuilderExtensions
|
|
{
|
|
/// <summary>
|
|
/// Adds dbcontext and all repositories in repository namespace
|
|
/// </summary>
|
|
/// <param name="collection">service collection</param>
|
|
/// <param name="config">configuration</param>
|
|
public static void AddDbServices(this IServiceCollection collection, IConfiguration config)
|
|
{
|
|
// Add appropriate DbContext
|
|
// Contexts are linked to providers by trimming the 'AppDbContext' portion of their name.
|
|
// So 'PostgresSqlAppDbContext' is selected with provider 'PostgresSql'
|
|
var providerTypes = Assembly.GetExecutingAssembly().GetTypes()
|
|
.Where(t => (t.Namespace?.Contains(nameof(Contexts)) ?? false) && typeof(AppDbContext).IsAssignableFrom(t) && !t.IsAbstract && t.Name.EndsWith(nameof(AppDbContext)));
|
|
var providers = providerTypes.ToDictionary(t => t.Name.Replace(nameof(AppDbContext), ""), t => t);
|
|
var selectedProvider = config["DatabaseProvider"];
|
|
|
|
//add dboptions
|
|
collection.AddSingleton(new DbContextOptions<AppDbContext>());
|
|
collection.AddSingleton<DbContextOptions>(p => p.GetRequiredService<DbContextOptions<AppDbContext>>());
|
|
|
|
// add our provider dbcontext
|
|
collection.AddScoped(typeof(AppDbContext), providers[selectedProvider]);
|
|
|
|
// Add db repositories
|
|
Type[] repositories = Assembly.GetExecutingAssembly().GetTypes()
|
|
.Where(t => t.IsClass && !t.IsAbstract && (t.Namespace?.Contains(nameof(DBConnection.Repositories)) ?? false) && typeof(IRepository).IsAssignableFrom(t)).ToArray();
|
|
foreach (var repo in repositories)
|
|
{
|
|
var repoInterface = repo.GetInterfaces()
|
|
.FirstOrDefault(repoInterface => typeof(IRepository).IsAssignableFrom(repoInterface) && repoInterface != typeof(IRepository) && !repoInterface.IsGenericType);
|
|
if (repoInterface != null)
|
|
{
|
|
collection.AddScoped(repoInterface, repo);
|
|
}
|
|
else
|
|
{
|
|
collection.AddScoped(repo);
|
|
}
|
|
}
|
|
|
|
}
|
|
} |