#region ========================================================================= USING ===================================================================================== using Lumina.DataAccess.Core.UoW; using Microsoft.Extensions.DependencyInjection; using Microsoft.EntityFrameworkCore; using System.Reflection; using Lumina.Application.Common.DataAccess.Repositories.Common.Base; using Lumina.DataAccess.Core.Repositories.Common.Factory; using Lumina.Application.Common.DataAccess.UoW; #endregion namespace Lumina.DataAccess.Common.DependencyInjection; /// /// Contains all services of the DataAccess layer. /// public static class DataAccessLayerServices { #region ===================================================================== METHODS =================================================================================== /// /// Extension method for adding the DataAccess layer services to the DI container. /// /// The service collection to add the services to. /// The updated . public static void AddDataAccessLayerServices(this IServiceCollection services) { var basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); if (!Directory.Exists(basePath)) throw new DirectoryNotFoundException($"The base path '{basePath}' does not exist."); services.AddDbContext(options => { options.UseSqlite($"Data Source={Path.Combine(basePath, "Lumina.db")}"); }); services.AddScoped(); Type[]? dataAccessLayerTypes = Assembly.GetExecutingAssembly().GetTypes(); Type? genericRepositoryType = typeof(IRepository<>); Type? repositoryFactoryType = typeof(RepositoryFactory); Type? iRepositoryFactoryType = typeof(IRepositoryFactory); services.AddScoped(iRepositoryFactoryType!, repositoryFactoryType!); // get all classes implementing IRepository (all repository classes) and register them as their corresponding repository interface IEnumerable repositoryTypes = dataAccessLayerTypes.Where(t => !t.IsInterface && t.GetInterfaces() .Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == genericRepositoryType)); foreach (Type type in repositoryTypes) { services.AddScoped(type.GetInterfaces() // TODO: change to scoped when DbContext is implemented .Where(i => !i.IsGenericType && i.GetInterfaces() .Any(j => j.GetGenericTypeDefinition() == genericRepositoryType)) .First(), type); } } #endregion }