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
{
///
/// Adds dbcontext and all repositories in repository namespace
///
/// service collection
/// configuration
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());
collection.AddSingleton(p => p.GetRequiredService>());
// 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);
}
}
}
}