Replaces reflection-based repository instantiation with unit of work property accessors

This commit is contained in:
BinaryAlley committed 2026-08-14 09:03:30 -07:00
1 parent 4fa9eb6e26
commit fee14afb89
87 files changed
+541 -735

No files matched your search

@@ -1,15 +0,0 @@
namespace Lumina.DataAccess.Core.Repositories.Common.Factory;
/// <summary>
/// Interface for the repositories factory.
/// </summary>
public interface IRepositoryFactory
{
/// <summary>
/// Creates a new repository of type <typeparamref name="TRepository"/>.
/// </summary>
/// <typeparam name="TRepository">The type of repository to create.</typeparam>
/// <returns>A repository of type <typeparamref name="TRepository"/>.</returns>
/// <exception cref="ArgumentException">Thrown when the type of the requested repository has not been registered.</exception>
TRepository CreateRepository<TRepository>() where TRepository : notnull;
}
@@ -1,64 +0,0 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
using System;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace Lumina.DataAccess.Core.Repositories.Common.Factory;
/// <summary>
/// Custom collection for storing repositories.
/// </summary>
public class RepositoryDictionary
{
private readonly List<KeyValuePair<Type, object>> _container = [];
/// <summary>
/// Gets the number of added repositories.
/// </summary>
public int Count => _container.Count;
/// <summary>
/// Resets the collection of repositories.
/// </summary>
public void Clear()
{
_container.Clear();
}
/// <summary>
/// Adds a repository of type <typeparamref name="TParam"/> to the collection.
/// </summary>
/// <typeparam name="TParam">The type of the repository to add to the collection.</typeparam>
/// <param name="value">The repository to add.</param>
public void Add<TParam>(TParam value) where TParam : notnull
{
// enforce adding rules
if (value == null)
throw new ArgumentException("Value cannot be null!");
if (_container.Any(e => e.Key == value.GetType()))
throw new ArgumentException("Duplicate values are not allowed!");
// check if the value implements an interface that implements IRepository
bool implementsRepository = value.GetType()
.GetInterfaces()
.Any(e => e.GetInterfaces()
.Any(i => i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof(IRepository<>)));
if (implementsRepository)
_container.Add(new KeyValuePair<Type, object>(value.GetType(), value));
else
throw new ArgumentException("Value must implement IRepository interface!");
}
/// <summary>
/// Gets a repository of type <typeparamref name="TResult"/> from the collection.
/// </summary>
/// <typeparam name="TResult">The type of the repository interface to get from the collection.</typeparam>
/// <param name="key">The concrete type of the repository interface to get.</param>
/// <returns>A repository implementing <typeparamref name="TResult"/>.</returns>
public TResult Get<TResult>(Type key)
{
return (TResult)_container.FirstOrDefault(e => e.Key == key).Value;
}
}
@@ -1,34 +0,0 @@
#region ========================================================================= USING =====================================================================================
using Microsoft.Extensions.DependencyInjection;
using System;
#endregion
namespace Lumina.DataAccess.Core.Repositories.Common.Factory;
/// <summary>
/// Concrete implementation for the repositories factory.
/// </summary>
public class RepositoryFactory : IRepositoryFactory
{
private readonly IServiceProvider _serviceProvider;
/// <summary>
/// Initializes a new instance of the <see cref="RepositoryFactory"/> class.
/// </summary>
/// <param name="serviceProvider">The service provider.</param>
public RepositoryFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
/// <summary>
/// Creates a new repository of type <typeparamref name="TRepository"/>.
/// </summary>
/// <typeparam name="TRepository">The type of repository to create.</typeparam>
/// <returns>A repository of type <typeparamref name="TRepository"/>.</returns>
/// <exception cref="ArgumentException">Thrown when the type of the requested repository has not been registered.</exception>
public TRepository CreateRepository<TRepository>() where TRepository : notnull
{
return _serviceProvider.GetRequiredService<TRepository>() ?? throw new ArgumentException();
}
}
@@ -44,7 +44,6 @@ public class DataSeedService : IDataSeedService
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
public async Task<Result<Created>> SetDefaultAuthorizationPermissionsAsync(Guid adminId, CancellationToken cancellationToken)
{
IPermissionRepository permissionRepository = _unitOfWork.GetRepository<IPermissionRepository>();
// create the default authorization permissions and add them to the repository.
PermissionEntity[] defaultPermissions =
[
@@ -55,7 +54,7 @@ public class DataSeedService : IDataSeedService
];
foreach (PermissionEntity permission in defaultPermissions)
{
Result<Created> insertPermissionResult = await permissionRepository.InsertAsync(permission, cancellationToken).ConfigureAwait(false);
Result<Created> insertPermissionResult = await _unitOfWork.PermissionRepository.InsertAsync(permission, cancellationToken).ConfigureAwait(false);
if (insertPermissionResult.IsFailure)
return insertPermissionResult.Errors;
}
@@ -71,8 +70,6 @@ public class DataSeedService : IDataSeedService
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
public async Task<Result<Created>> SetDefaultAuthorizationRolesAsync(Guid userId, CancellationToken cancellationToken)
{
IRoleRepository roleRepository = _unitOfWork.GetRepository<IRoleRepository>();
// create the default authorization roles and add them to the repository
RoleEntity[] defaultRoles =
[
@@ -80,7 +77,7 @@ public class DataSeedService : IDataSeedService
];
foreach (RoleEntity role in defaultRoles)
{
Result<Created> insertRoleResult = await roleRepository.InsertAsync(role, cancellationToken).ConfigureAwait(false);
Result<Created> insertRoleResult = await _unitOfWork.RoleRepository.InsertAsync(role, cancellationToken).ConfigureAwait(false);
if (insertRoleResult.IsFailure)
return insertRoleResult.Errors;
}
@@ -96,12 +93,8 @@ public class DataSeedService : IDataSeedService
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
public async Task<Result<Created>> SetAdminRolePermissionsAsync(Guid userId, CancellationToken cancellationToken)
{
IRolePermissionRepository rolePermissionRepository = _unitOfWork.GetRepository<IRolePermissionRepository>();
IRoleRepository roleRepository = _unitOfWork.GetRepository<IRoleRepository>();
IPermissionRepository permissionRepository = _unitOfWork.GetRepository<IPermissionRepository>();
// get the admin role
Result<RoleEntity?> getAdminRoleResult = await roleRepository.GetByNameAsync("Admin", cancellationToken).ConfigureAwait(false);
Result<RoleEntity?> getAdminRoleResult = await _unitOfWork.RoleRepository.GetByNameAsync("Admin", cancellationToken).ConfigureAwait(false);
if (getAdminRoleResult.IsFailure)
return getAdminRoleResult.Errors;
@@ -109,7 +102,7 @@ public class DataSeedService : IDataSeedService
return Errors.Authorization.AdminAccountNotFound;
// get all permissions
Result<IEnumerable<PermissionEntity>> getPermissionsResult = await permissionRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
Result<IEnumerable<PermissionEntity>> getPermissionsResult = await _unitOfWork.PermissionRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
if (getPermissionsResult.IsFailure)
return getPermissionsResult.Errors;
@@ -125,7 +118,7 @@ public class DataSeedService : IDataSeedService
CreatedBy = userId,
CreatedOnUtc = _dateTimeProvider.UtcNow
};
Result<Created> insertRolePermissionResult = await rolePermissionRepository.InsertAsync(rolePermissionEntity, cancellationToken).ConfigureAwait(false);
Result<Created> insertRolePermissionResult = await _unitOfWork.RolePermissionRepository.InsertAsync(rolePermissionEntity, cancellationToken).ConfigureAwait(false);
if (insertRolePermissionResult.IsFailure)
return insertRolePermissionResult.Errors;
}
@@ -141,13 +134,8 @@ public class DataSeedService : IDataSeedService
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
public async Task<Result<Created>> SetAdminRoleToAdministratorAccount(Guid userId, CancellationToken cancellationToken)
{
IUserRoleRepository userRoleRepository = _unitOfWork.GetRepository<IUserRoleRepository>();
IRoleRepository roleRepository = _unitOfWork.GetRepository<IRoleRepository>();
IUserRepository userRepository = _unitOfWork.GetRepository<IUserRepository>();
// get the admin role
Result<RoleEntity?> getAdminRoleResult = await roleRepository.GetByNameAsync("Admin", cancellationToken).ConfigureAwait(false);
Result<RoleEntity?> getAdminRoleResult = await _unitOfWork.RoleRepository.GetByNameAsync("Admin", cancellationToken).ConfigureAwait(false);
if (getAdminRoleResult.IsFailure)
return getAdminRoleResult.Errors;
@@ -155,7 +143,7 @@ public class DataSeedService : IDataSeedService
return Errors.Authorization.AdminRoleNotFound;
// get admin user
Result<UserEntity?> getUserResult = await userRepository.GetByIdAsync(userId, cancellationToken).ConfigureAwait(false);
Result<UserEntity?> getUserResult = await _unitOfWork.UserRepository.GetByIdAsync(userId, cancellationToken).ConfigureAwait(false);
if (getUserResult.IsFailure)
return getUserResult.Errors;
@@ -172,7 +160,7 @@ public class DataSeedService : IDataSeedService
User = getUserResult.Value,
UserId = getUserResult.Value.Id
};
Result<Created> insertUserRoleResult = await userRoleRepository.InsertAsync(userRole, cancellationToken).ConfigureAwait(false);
Result<Created> insertUserRoleResult = await _unitOfWork.UserRoleRepository.InsertAsync(userRole, cancellationToken).ConfigureAwait(false);
if (insertUserRoleResult.IsFailure)
return insertUserRoleResult.Errors;
+214 -66
View File
@@ -1,11 +1,17 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
using Lumina.Application.Common.DataAccess.Repositories.Books;
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
using Lumina.Application.Common.DataAccess.Repositories.Users;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.DataAccess.Core.Repositories.Common.Factory;
using Lumina.DataAccess.Core.Repositories.Authorization;
using Lumina.DataAccess.Core.Repositories.Books;
using Lumina.DataAccess.Core.Repositories.Libraries;
using Lumina.DataAccess.Core.Repositories.Plugins;
using Lumina.DataAccess.Core.Repositories.Users;
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -15,79 +21,188 @@ namespace Lumina.DataAccess.Core.UoW;
/// <summary>
/// Interaction boundary with the Data Access Layer.
/// </summary>
public class UnitOfWork : IUnitOfWork
public class UnitOfWork : IUnitOfWork, IDisposable
{
private readonly LuminaDbContext _luminaDbContext;
private readonly IRepositoryFactory _repositoryFactory;
private IDbContextTransaction? _transaction;
private IPermissionRepository? _permissionRepository;
private IRolePermissionRepository? _rolePermissionRepository;
private IRoleRepository? _roleRepository;
private IUserRoleRepository? _userRoleRepository;
private IBookRepository? _bookRepository;
private IDirectoryScanFingerprintRepository? _directoryScanFingerprintRepository;
private ILibraryRepository? _libraryRepository;
private ILibraryScanRepository? _libraryScanRepository;
private ILibraryScanSnapshotRepository? _libraryScanSnapshotRepository;
private ILibraryScanStagingResultsRepository? _libraryScanStagingResultsRepository;
private ILibraryMetadataProviderConfigurationRepository? _libraryMetadataProviderConfigurationRepository;
private IPluginRepository? _pluginRepository;
private IUserRepository? _userRepository;
/// <summary>
/// Gets or sets the collection of available repositories.
/// Gets the permission repository.
/// </summary>
internal RepositoryDictionary Repositories { get; private set; } = new RepositoryDictionary();
/// <summary>
/// Initializes a new instance of the <see cref="UnitOfWork"/> class.
/// </summary>
/// <param name="repositoryFactory">The factory used to generate repositories.</param>
/// <param name="luminaDbContext">Injected Entity Framework DbContext.</param>
public UnitOfWork(IRepositoryFactory repositoryFactory, LuminaDbContext luminaDbContext)
public IPermissionRepository PermissionRepository
{
_repositoryFactory = repositoryFactory;
_luminaDbContext = luminaDbContext;
AddRepositories();
}
/// <summary>
/// Adds all the repositories from the Data Access Layer so that they can be exposed to the Business Layer.
/// </summary>
internal void AddRepositories()
{
// get all the concrete implementations of IRepository<>
IEnumerable<Type> repositoryClassTypes =
Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => !t.IsInterface && !t.IsAbstract && t.GetInterfaces()
.Any(i => i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof(IRepository<>)));
// store all repositories
foreach (Type repositoryClassType in repositoryClassTypes)
get
{
// get the interface that implements IRepository<> of the currently iterated repository class
Type repositoryInterfaceType = repositoryClassType.GetInterfaces()
.Where(i => !i.IsGenericType &&
i.GetInterfaces()
.Any(a => a.GetGenericTypeDefinition() == typeof(IRepository<>)))
.First();
// ask the concrete type for the repository interface type from the repositories factory;
// because the method for creating a repository is generic and we need to call it with a runtime type, reflection is the only option
object repositoryClass = typeof(IRepositoryFactory).GetMethod("CreateRepository")!
.MakeGenericMethod(repositoryInterfaceType)
.Invoke(_repositoryFactory, null)!;
Repositories.Add(repositoryClass);
_permissionRepository ??= new PermissionRepository(_luminaDbContext);
return _permissionRepository;
}
}
/// <summary>
/// Exposes a repository of type <typeparamref name="TRepository"/> to the Business Logic Layer.
/// Gets the role permission repository.
/// </summary>
/// <typeparam name="TRepository">The type of the exposed repository.</typeparam>
/// <returns>A repository of type <typeparamref name="TRepository"/>.</returns>
public TRepository GetRepository<TRepository>()
public IRolePermissionRepository RolePermissionRepository
{
// get the repository type based on the type of the provided repository interface
Type repositoryType = Assembly.GetExecutingAssembly()
.GetTypes()
.Where(type => !type.IsInterface && !type.IsAbstract && typeof(TRepository).IsAssignableFrom(type))
.First();
return Repositories.Get<TRepository>(repositoryType);
get
{
_rolePermissionRepository ??= new RolePermissionRepository(_luminaDbContext);
return _rolePermissionRepository;
}
}
/// <summary>
/// Resets the list of repositories.
/// Gets the role repository.
/// </summary>
internal void ResetRepositories()
public IRoleRepository RoleRepository
{
Repositories.Clear();
get
{
_roleRepository ??= new RoleRepository(_luminaDbContext);
return _roleRepository;
}
}
/// <summary>
/// Gets the user role repository.
/// </summary>
public IUserRoleRepository UserRoleRepository
{
get
{
_userRoleRepository ??= new UserRoleRepository(_luminaDbContext);
return _userRoleRepository;
}
}
/// <summary>
/// Gets the book repository.
/// </summary>
public IBookRepository BookRepository
{
get
{
_bookRepository ??= new BookRepository(_luminaDbContext);
return _bookRepository;
}
}
/// <summary>
/// Gets the directory scan fingerprint repository.
/// </summary>
public IDirectoryScanFingerprintRepository DirectoryScanFingerprintRepository
{
get
{
_directoryScanFingerprintRepository ??= new DirectoryScanFingerprintRepository(_luminaDbContext);
return _directoryScanFingerprintRepository;
}
}
/// <summary>
/// Gets the library repository.
/// </summary>
public ILibraryRepository LibraryRepository
{
get
{
_libraryRepository ??= new LibraryRepository(_luminaDbContext);
return _libraryRepository;
}
}
/// <summary>
/// Gets the library scan repository.
/// </summary>
public ILibraryScanRepository LibraryScanRepository
{
get
{
_libraryScanRepository ??= new LibraryScanRepository(_luminaDbContext);
return _libraryScanRepository;
}
}
/// <summary>
/// Gets the library scan snapshot repository.
/// </summary>
public ILibraryScanSnapshotRepository LibraryScanSnapshotRepository
{
get
{
_libraryScanSnapshotRepository ??= new LibraryScanSnapshotRepository(_luminaDbContext);
return _libraryScanSnapshotRepository;
}
}
/// <summary>
/// Gets the library scan staging results repository.
/// </summary>
public ILibraryScanStagingResultsRepository LibraryScanStagingResultsRepository
{
get
{
_libraryScanStagingResultsRepository ??= new LibraryScanStagingResultsRepository(_luminaDbContext);
return _libraryScanStagingResultsRepository;
}
}
/// <summary>
/// Gets the library metadata provider configuration repository.
/// </summary>
public ILibraryMetadataProviderConfigurationRepository LibraryMetadataProviderConfigurationRepository
{
get
{
_libraryMetadataProviderConfigurationRepository ??= new LibraryMetadataProviderConfigurationRepository(_luminaDbContext);
return _libraryMetadataProviderConfigurationRepository;
}
}
/// <summary>
/// Gets the plugin repository.
/// </summary>
public IPluginRepository PluginRepository
{
get
{
_pluginRepository ??= new PluginRepository(_luminaDbContext);
return _pluginRepository;
}
}
/// <summary>
/// Gets the user repository.
/// </summary>
public IUserRepository UserRepository
{
get
{
_userRepository ??= new UserRepository(_luminaDbContext);
return _userRepository;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="UnitOfWork"/> class.
/// </summary>
/// <param name="luminaDbContext">Injected Entity Framework DbContext.</param>
public UnitOfWork(LuminaDbContext luminaDbContext)
{
_luminaDbContext = luminaDbContext;
}
/// <summary>
@@ -100,18 +215,51 @@ public class UnitOfWork : IUnitOfWork
}
/// <summary>
/// Opens a transaction.
/// Begins a new database transaction.
/// </summary>
public void OpenTransaction()
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public async Task BeginTransactionAsync(CancellationToken cancellationToken)
{
throw new NotImplementedException();
_transaction = await _luminaDbContext.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Commits a transaction.
/// Commits the current transaction.
/// </summary>
public void CommitTransaction()
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public async Task CommitTransactionAsync(CancellationToken cancellationToken)
{
throw new NotImplementedException();
if (_transaction is not null)
{
await _transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
await _transaction.DisposeAsync().ConfigureAwait(false);
_transaction = null;
}
}
/// <summary>
/// Rolls back the current transaction.
/// </summary>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public async Task RollbackTransactionAsync(CancellationToken cancellationToken)
{
if (_transaction is not null)
{
await _transaction.RollbackAsync(cancellationToken).ConfigureAwait(false);
await _transaction.DisposeAsync().ConfigureAwait(false);
_transaction = null;
}
}
/// <summary>
/// Disposes the unit of work and its resources.
/// </summary>
public void Dispose()
{
_transaction?.Dispose();
_luminaDbContext.Dispose();
}
}