diff --git a/src/Repository/AsyncQueryableRepository.cs b/src/Repository/AsyncQueryableRepository.cs index 49d1b9b..d263737 100644 --- a/src/Repository/AsyncQueryableRepository.cs +++ b/src/Repository/AsyncQueryableRepository.cs @@ -1,17 +1,640 @@ -using System.Diagnostics.CodeAnalysis; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using eQuantic.Core.Data.EntityFramework.Repository.Read; +using eQuantic.Core.Data.EntityFramework.Repository.Write; using eQuantic.Core.Data.Repository; using eQuantic.Core.Data.Repository.Config; +using eQuantic.Core.Data.Repository.Read; +using eQuantic.Core.Data.Repository.Write; +using eQuantic.Linq.Specification; namespace eQuantic.Core.Data.EntityFramework.Repository; [ExcludeFromCodeCoverage] public class AsyncQueryableRepository : - AsyncRepository, TEntity, TKey>, + QueryableRepository, + IAsyncRepository, TEntity, TKey>, IAsyncQueryableRepository - where TUnitOfWork : IQueryableUnitOfWork + where TUnitOfWork : class, IQueryableUnitOfWork where TEntity : class, IEntity, new() { + private readonly IAsyncQueryableReadRepository _asyncReadRepository; + private readonly IAsyncWriteRepository _asyncWriteRepository; + private bool _disposed; + public AsyncQueryableRepository(TUnitOfWork unitOfWork) : base(unitOfWork) { + this._asyncReadRepository = new AsyncQueryableReadRepository(unitOfWork); + this._asyncWriteRepository = new AsyncWriteRepository(unitOfWork); + } + + public Task AddAsync(TEntity item) + { + return this._asyncWriteRepository.AddAsync(item); + } + + public Task> AllMatchingAsync( + ISpecification specification, + Action> configuration = default) + { + return this._asyncReadRepository.AllMatchingAsync(specification, configuration); + } + + public Task> AllMatchingAsync( + ISpecification specification, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.AllMatchingAsync(specification, configuration, cancellationToken); + } + public Task> AllMatchingAsync( + ISpecification specification, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.AllMatchingAsync(specification, cancellationToken); + } + + public Task CountAsync(CancellationToken cancellationToken = default) + { + return this._asyncReadRepository.CountAsync(cancellationToken); + } + + public Task CountAsync( + ISpecification specification, + CancellationToken cancellationToken = default) + { + return this._asyncReadRepository.CountAsync(specification, cancellationToken); + } + + public Task CountAsync( + Expression> filter, + CancellationToken cancellationToken = default) + { + return this._asyncReadRepository.CountAsync(filter, cancellationToken); + } + + public Task AllAsync( + ISpecification specification, + Action> configuration = default) + { + return this._asyncReadRepository.AllAsync(specification, configuration); + } + + public Task AllAsync( + ISpecification specification, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.AllAsync(specification, configuration, cancellationToken); + } + + public Task AllAsync( + ISpecification specification, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.AllAsync(specification, cancellationToken); + } + + public Task AllAsync( + Expression> filter, + Action> configuration = default) + { + return this._asyncReadRepository.AllAsync(filter, configuration); + } + + public Task AllAsync( + Expression> filter, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.AllAsync(filter, configuration, cancellationToken); + } + + public Task AllAsync( + Expression> filter, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.AllAsync(filter, cancellationToken); + } + + public Task AnyAsync( + Action> configuration = default, + CancellationToken cancellationToken = default) + { + return this._asyncReadRepository.AnyAsync(configuration, cancellationToken); + } + + public Task AnyAsync( + ISpecification specification, + Action> configuration = default) + { + return this._asyncReadRepository.AnyAsync(specification, configuration); + } + + public Task AnyAsync( + ISpecification specification, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.AnyAsync(specification, configuration, cancellationToken); + } + + public Task AnyAsync( + ISpecification specification, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.AnyAsync(specification, cancellationToken); + } + + public Task AnyAsync( + Expression> filter, + Action> configuration = default) + { + return this._asyncReadRepository.AnyAsync(filter, configuration); + } + + public Task AnyAsync( + Expression> filter, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.AnyAsync(filter, configuration, cancellationToken); + } + + public Task AnyAsync( + Expression> filter, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.AnyAsync(filter, cancellationToken); + } + + public Task DeleteManyAsync( + Expression> filter, + CancellationToken cancellationToken = default) + { + return this._asyncWriteRepository.DeleteManyAsync(filter, cancellationToken); + } + + public Task DeleteManyAsync( + ISpecification specification, + CancellationToken cancellationToken = default) + { + return this._asyncWriteRepository.DeleteManyAsync(specification, cancellationToken); + } + + public Task> GetAllAsync( + Action> configuration = default) + { + return this._asyncReadRepository.GetAllAsync(configuration); + } + + public Task> GetAllAsync( + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetAllAsync(configuration, cancellationToken); + } + + public Task> GetAllAsync( + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetAllAsync(cancellationToken); + } + + public Task GetAsync( + TKey id, + Action> configuration = default) + { + return this._asyncReadRepository.GetAsync(id, configuration); + } + + public Task GetAsync( + TKey id, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetAsync(id, configuration, cancellationToken); + } + + public Task GetAsync( + TKey id, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetAsync(id, cancellationToken); + } + + public Task> GetMappedAsync( + Expression> filter, + Expression> map, + Action> configuration = default) + { + return this._asyncReadRepository.GetMappedAsync(filter, map, configuration); + } + + public Task> GetMappedAsync( + Expression> filter, + Expression> map, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetMappedAsync(filter, map, configuration, cancellationToken); + } + + public Task> GetMappedAsync( + Expression> filter, + Expression> map, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetMappedAsync(filter, map, cancellationToken); + } + + public Task> GetMappedAsync( + ISpecification specification, + Expression> map, + Action> configuration = default) + { + return this._asyncReadRepository.GetMappedAsync(specification, map, configuration); + } + + public Task> GetMappedAsync( + ISpecification specification, + Expression> map, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetMappedAsync(specification, map, configuration, cancellationToken); + } + + public Task> GetMappedAsync( + ISpecification specification, + Expression> map, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetMappedAsync(specification, map, cancellationToken); + } + + public Task> GetFilteredAsync( + Expression> filter, + Action> configuration = default) + { + return this._asyncReadRepository.GetFilteredAsync(filter, configuration); + } + + public Task> GetFilteredAsync( + Expression> filter, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetFilteredAsync(filter, configuration, cancellationToken); + } + + public Task> GetFilteredAsync( + Expression> filter, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetFilteredAsync(filter, cancellationToken); + } + + public Task GetFirstAsync( + Expression> filter, + Action> configuration = default) + { + return this._asyncReadRepository.GetFirstAsync(filter, configuration); + } + + public Task GetFirstAsync( + Expression> filter, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetFirstAsync(filter, configuration, cancellationToken); + } + + public Task GetFirstAsync( + Expression> filter, + CancellationToken cancellationToken = default) + { + return this._asyncReadRepository.GetFirstAsync(filter, cancellationToken); + } + + public Task GetFirstAsync( + ISpecification specification, + Action> configuration = default) + { + return this._asyncReadRepository.GetFirstAsync(specification, configuration); + } + + public Task GetFirstAsync( + ISpecification specification, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetFirstAsync(specification, configuration, cancellationToken); + } + + public Task GetFirstAsync( + ISpecification specification, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetFirstAsync(specification, cancellationToken); + } + + public Task GetFirstMappedAsync( + Expression> filter, + Expression> map, + Action> configuration = default) + { + return this._asyncReadRepository.GetFirstMappedAsync(filter, map, configuration); + } + + public Task GetFirstMappedAsync( + Expression> filter, + Expression> map, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetFirstMappedAsync(filter, map, configuration, cancellationToken); + } + + public Task GetFirstMappedAsync( + Expression> filter, + Expression> map, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetFirstMappedAsync(filter, map, cancellationToken); + } + + public Task GetFirstMappedAsync( + ISpecification specification, + Expression> map, + Action> configuration = default) + { + return this._asyncReadRepository.GetFirstMappedAsync(specification, map, configuration); + } + + public Task GetFirstMappedAsync( + ISpecification specification, + Expression> map, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetFirstMappedAsync(specification, map, configuration, cancellationToken); + } + + public Task GetFirstMappedAsync( + ISpecification specification, + Expression> map, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetFirstMappedAsync(specification, map, cancellationToken); + } + + public Task> GetPagedAsync( + int limit, + Action> configuration = default) + { + return this._asyncReadRepository.GetPagedAsync(limit, configuration); + } + + public Task> GetPagedAsync( + int limit, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetPagedAsync(limit, configuration, cancellationToken); + } + + public Task> GetPagedAsync( + int limit, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetPagedAsync(limit, cancellationToken); + } + + public Task> GetPagedAsync( + ISpecification specification, + int limit, + Action> configuration = default) + { + return this._asyncReadRepository.GetPagedAsync(specification, limit, configuration); + } + + public Task> GetPagedAsync( + ISpecification specification, + int limit, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetPagedAsync(specification, limit, configuration, cancellationToken); + } + + public Task> GetPagedAsync( + ISpecification specification, + int limit, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetPagedAsync(specification, limit, cancellationToken); + } + + public Task> GetPagedAsync( + Expression> filter, + int limit, + Action> configuration = default) + { + return this._asyncReadRepository.GetPagedAsync(filter, limit, configuration); + } + + public Task> GetPagedAsync( + Expression> filter, + int limit, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetPagedAsync(filter, limit, configuration, cancellationToken); + } + + public Task> GetPagedAsync( + Expression> filter, + int limit, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetPagedAsync(filter, limit, cancellationToken); + } + + public Task> GetPagedAsync( + int pageIndex, + int pageSize, + Action> configuration = default) + { + return this._asyncReadRepository.GetPagedAsync(pageIndex, pageSize, configuration); + } + + public Task> GetPagedAsync( + int pageIndex, + int pageSize, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetPagedAsync(pageIndex, pageSize, configuration, cancellationToken); + } + + public Task> GetPagedAsync( + int pageIndex, + int pageSize, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetPagedAsync(pageIndex, pageSize, cancellationToken); + } + + public Task> GetPagedAsync( + ISpecification specification, + int pageIndex, + int pageSize, + Action> configuration = default) + { + return this._asyncReadRepository.GetPagedAsync(specification, pageIndex, pageSize, configuration); + } + + public Task> GetPagedAsync( + ISpecification specification, + int pageIndex, + int pageSize, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetPagedAsync(specification, pageIndex, pageSize, configuration, + cancellationToken); + } + + public Task> GetPagedAsync( + ISpecification specification, + int pageIndex, + int pageSize, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetPagedAsync(specification, pageIndex, pageSize, + cancellationToken); + } + + public Task> GetPagedAsync( + Expression> filter, + int pageIndex, + int pageSize, + Action> configuration = default) + { + return this._asyncReadRepository.GetPagedAsync(filter, pageIndex, pageSize, configuration); + } + + public Task> GetPagedAsync( + Expression> filter, + int pageIndex, + int pageSize, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetPagedAsync(filter, pageIndex, pageSize, configuration, cancellationToken); + } + + public Task> GetPagedAsync( + Expression> filter, + int pageIndex, + int pageSize, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetPagedAsync(filter, pageIndex, pageSize, cancellationToken); + } + + public Task GetSingleAsync( + Expression> filter, + Action> configuration = default) + { + return this._asyncReadRepository.GetSingleAsync(filter, configuration); + } + + public Task GetSingleAsync( + Expression> filter, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetSingleAsync(filter, configuration, cancellationToken); + } + + public Task GetSingleAsync( + Expression> filter, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetSingleAsync(filter, cancellationToken); + } + + public Task GetSingleAsync( + ISpecification specification, + Action> configuration = default) + { + return this._asyncReadRepository.GetSingleAsync(specification, configuration); + } + + public Task GetSingleAsync( + ISpecification specification, + Action> configuration, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetSingleAsync(specification, configuration, cancellationToken); + } + + public Task GetSingleAsync( + ISpecification specification, + CancellationToken cancellationToken) + { + return this._asyncReadRepository.GetSingleAsync(specification, cancellationToken); + } + + public Task MergeAsync(TEntity persisted, TEntity current) + { + return this._asyncWriteRepository.MergeAsync(persisted, current); + } + + public Task ModifyAsync(TEntity item) + { + return this._asyncWriteRepository.ModifyAsync(item); + } + + public Task RemoveAsync(TEntity item) + { + return this._asyncWriteRepository.RemoveAsync(item); + } + + public Task UpdateManyAsync(Expression> filter, + Expression> updateFactory, CancellationToken cancellationToken = default) + { + return this._asyncWriteRepository.UpdateManyAsync(filter, updateFactory, cancellationToken); + } + + public Task UpdateManyAsync(ISpecification specification, + Expression> updateFactory, CancellationToken cancellationToken = default) + { + return this._asyncWriteRepository.UpdateManyAsync(specification, updateFactory, cancellationToken); + } + + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + + if (_disposed) + { + return; + } + + if (disposing) + { + this._asyncReadRepository?.Dispose(); + this._asyncWriteRepository?.Dispose(); + UnitOfWork?.Dispose(); + } + + _disposed = true; } } diff --git a/src/Repository/AsyncRepository.cs b/src/Repository/AsyncRepository.cs deleted file mode 100644 index 2189f3c..0000000 --- a/src/Repository/AsyncRepository.cs +++ /dev/null @@ -1,265 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; -using eQuantic.Core.Data.EntityFramework.Repository.Read; -using eQuantic.Core.Data.EntityFramework.Repository.Write; -using eQuantic.Core.Data.Repository; -using eQuantic.Core.Data.Repository.Config; -using eQuantic.Core.Data.Repository.Read; -using eQuantic.Core.Data.Repository.Write; -using eQuantic.Linq.Specification; - -namespace eQuantic.Core.Data.EntityFramework.Repository; - -public class AsyncRepository : Repository, - IAsyncRepository - where TUnitOfWork : IQueryableUnitOfWork - where TEntity : class, IEntity, new() - where TConfig : Configuration -{ - private readonly IAsyncReadRepository _asyncReadRepository; - private readonly IAsyncWriteRepository _asyncWriteRepository; - private bool _disposed; - - public AsyncRepository(TUnitOfWork unitOfWork) : base(unitOfWork) - { - this._asyncReadRepository = new AsyncReadRepository(unitOfWork); - this._asyncWriteRepository = new AsyncWriteRepository(unitOfWork); - } - - public Task AddAsync(TEntity item) - { - return this._asyncWriteRepository.AddAsync(item); - } - - public Task> AllMatchingAsync(ISpecification specification, - Action configuration = default, CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.AllMatchingAsync(specification, configuration, cancellationToken); - } - - public Task CountAsync(CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.CountAsync(cancellationToken); - } - - public Task CountAsync(ISpecification specification, CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.CountAsync(specification, cancellationToken); - } - - public Task CountAsync(Expression> filter, CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.CountAsync(filter, cancellationToken); - } - - public Task AllAsync(ISpecification specification, Action configuration = default, - CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.AllAsync(specification, configuration, cancellationToken); - } - - public Task AllAsync(Expression> filter, Action configuration = default, - CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.AllAsync(filter, configuration, cancellationToken); - } - - public Task AnyAsync(Action configuration = default, CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.AnyAsync(configuration, cancellationToken); - } - - public Task AnyAsync(ISpecification specification, Action configuration = default, - CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.AnyAsync(specification, configuration, cancellationToken); - } - - public Task AnyAsync(Expression> filter, Action configuration = default, - CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.AnyAsync(filter, configuration, cancellationToken); - } - - public Task DeleteManyAsync(Expression> filter, - CancellationToken cancellationToken = default) - { - return this._asyncWriteRepository.DeleteManyAsync(filter, cancellationToken); - } - - public Task DeleteManyAsync(ISpecification specification, - CancellationToken cancellationToken = default) - { - return this._asyncWriteRepository.DeleteManyAsync(specification, cancellationToken); - } - - public Task> GetAllAsync(Action configuration = default, - CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.GetAllAsync(configuration, cancellationToken); - } - - public Task GetAsync(TKey id, Action configuration = default, - CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.GetAsync(id, configuration, cancellationToken); - } - - public Task> GetMappedAsync(Expression> filter, - Expression> map, Action configuration = default, - CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.GetMappedAsync(filter, map, configuration, cancellationToken); - } - - public Task> GetMappedAsync(ISpecification specification, - Expression> map, Action configuration = default, - CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.GetMappedAsync(specification, map, configuration, cancellationToken); - } - - public Task> GetFilteredAsync(Expression> filter, - Action configuration = default, CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.GetFilteredAsync(filter, configuration, cancellationToken); - } - - public Task GetFirstAsync(Expression> filter, Action configuration = default, - CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.GetFirstAsync(filter, configuration, cancellationToken); - } - - public Task GetFirstAsync(ISpecification specification, Action configuration = default, - CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.GetFirstAsync(specification, configuration, cancellationToken); - } - - public Task GetFirstMappedAsync(Expression> filter, - Expression> map, Action configuration = default, - CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.GetFirstMappedAsync(filter, map, configuration, cancellationToken); - } - - public Task GetFirstMappedAsync(ISpecification specification, - Expression> map, Action configuration = default, - CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.GetFirstMappedAsync(specification, map, configuration, cancellationToken); - } - - public Task> GetPagedAsync(int limit, Action configuration = default, - CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.GetPagedAsync(limit, configuration, cancellationToken); - } - - public Task> GetPagedAsync(ISpecification specification, int limit, - Action configuration = default, CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.GetPagedAsync(specification, limit, configuration, cancellationToken); - } - - public Task> GetPagedAsync(Expression> filter, int limit, - Action configuration = default, CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.GetPagedAsync(filter, limit, configuration, cancellationToken); - } - - public Task> GetPagedAsync(int pageIndex, int pageSize, - Action configuration = default, CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.GetPagedAsync(pageIndex, pageSize, configuration, cancellationToken); - } - - public Task> GetPagedAsync( - ISpecification specification, int pageIndex, int pageSize, - Action configuration = default, - CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.GetPagedAsync(specification, pageIndex, pageSize, configuration, - cancellationToken); - } - - public Task> GetPagedAsync( - Expression> filter, int pageIndex, int pageSize, - Action configuration = default, - CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.GetPagedAsync(filter, pageIndex, pageSize, configuration, cancellationToken); - } - - public Task GetSingleAsync(Expression> filter, Action configuration = default, - CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.GetSingleAsync(filter, configuration, cancellationToken); - } - - public Task GetSingleAsync(ISpecification specification, Action configuration = default, - CancellationToken cancellationToken = default) - { - return this._asyncReadRepository.GetSingleAsync(specification, configuration, cancellationToken); - } - - public Task MergeAsync(TEntity persisted, TEntity current) - { - return this._asyncWriteRepository.MergeAsync(persisted, current); - } - - public Task ModifyAsync(TEntity item) - { - return this._asyncWriteRepository.ModifyAsync(item); - } - - public Task RemoveAsync(TEntity item) - { - return this._asyncWriteRepository.RemoveAsync(item); - } - - public Task UpdateManyAsync(Expression> filter, - Expression> updateFactory, CancellationToken cancellationToken = default) - { - return this._asyncWriteRepository.UpdateManyAsync(filter, updateFactory, cancellationToken); - } - - public Task UpdateManyAsync(ISpecification specification, - Expression> updateFactory, CancellationToken cancellationToken = default) - { - return this._asyncWriteRepository.UpdateManyAsync(specification, updateFactory, cancellationToken); - } - - protected override void Dispose(bool disposing) - { - base.Dispose(disposing); - - if (_disposed) - { - return; - } - - if (disposing) - { - this._asyncReadRepository?.Dispose(); - this._asyncWriteRepository?.Dispose(); - UnitOfWork?.Dispose(); - } - - _disposed = true; - } -} - -public class AsyncRepository : - AsyncRepository, TEntity, TKey>, IAsyncRepository - where TUnitOfWork : IQueryableUnitOfWork - where TEntity : class, IEntity, new() -{ - public AsyncRepository(TUnitOfWork unitOfWork) : base(unitOfWork) - { - } -} diff --git a/src/Repository/DefaultUnitOfWork.cs b/src/Repository/DefaultUnitOfWork.cs index 06a119c..f626783 100644 --- a/src/Repository/DefaultUnitOfWork.cs +++ b/src/Repository/DefaultUnitOfWork.cs @@ -5,7 +5,7 @@ namespace eQuantic.Core.Data.EntityFramework.Repository; -public class DefaultUnitOfWork : UnitOfWork, IDefaultUnitOfWork +public class DefaultUnitOfWork : UnitOfWork { public DefaultUnitOfWork(IServiceProvider serviceProvider, DbContext context) : base(serviceProvider, context) { diff --git a/src/Repository/Extensions/ServiceCollectionExtensions.cs b/src/Repository/Extensions/ServiceCollectionExtensions.cs index 8ec0327..a5dd24d 100644 --- a/src/Repository/Extensions/ServiceCollectionExtensions.cs +++ b/src/Repository/Extensions/ServiceCollectionExtensions.cs @@ -11,23 +11,23 @@ namespace eQuantic.Core.Data.EntityFramework.Repository.Extensions; public static class ServiceCollectionExtensions { public static IServiceCollection AddQueryableRepositories(this IServiceCollection services) - where TQueryableUnitOfWork : class, IDefaultUnitOfWork + where TQueryableUnitOfWork : class, IQueryableUnitOfWork { return AddQueryableRepositories(services, _ => { }); } public static IServiceCollection AddQueryableRepositories( this IServiceCollection services) - where TUnitOfWorkInterface : ISqlUnitOfWork - where TUnitOfWorkImpl : class, ISqlUnitOfWork + where TUnitOfWorkInterface : IQueryableUnitOfWork + where TUnitOfWorkImpl : class, TUnitOfWorkInterface { return AddQueryableRepositories(services, _ => {}); } public static IServiceCollection AddQueryableRepositories(this IServiceCollection services, Action options) - where TUnitOfWorkInterface : ISqlUnitOfWork - where TUnitOfWorkImpl : class, ISqlUnitOfWork + where TUnitOfWorkInterface : IQueryableUnitOfWork + where TUnitOfWorkImpl : class, TUnitOfWorkInterface { var repoOptions = GetOptions(options); var lifetime = repoOptions.GetLifetime(); @@ -40,12 +40,12 @@ public static IServiceCollection AddQueryableRepositories(this IServiceCollection services, Action options) - where TQueryableUnitOfWork : class, IDefaultUnitOfWork + where TQueryableUnitOfWork : class, IQueryableUnitOfWork { var repoOptions = GetOptions(options); var lifetime = repoOptions.GetLifetime(); - AddUnitOfWork(services, lifetime); + AddUnitOfWork(services, lifetime); AddGenericRepositories(services, lifetime); return services; @@ -53,38 +53,36 @@ public static IServiceCollection AddQueryableRepositories( public static IServiceCollection AddCustomRepositories(this IServiceCollection services, Action options) - where TQueryableUnitOfWork : class, IDefaultUnitOfWork + where TQueryableUnitOfWork : class, IQueryableUnitOfWork { var repoOptions = GetOptions(options); var lifetime = repoOptions.GetLifetime(); - AddUnitOfWork(services, lifetime); + AddUnitOfWork(services, lifetime); AddRepositories(services, repoOptions); return services; } private static void AddUnitOfWork(IServiceCollection services, ServiceLifetime lifetime) - where TUnitOfWorkInterface : ISqlUnitOfWork - where TUnitOfWorkImpl : class, ISqlUnitOfWork + where TUnitOfWorkInterface : IQueryableUnitOfWork + where TUnitOfWorkImpl : class, IQueryableUnitOfWork { services.TryAdd(new ServiceDescriptor(typeof(TUnitOfWorkInterface), typeof(TUnitOfWorkImpl), lifetime)); - services.TryAdd(new ServiceDescriptor(typeof(ISqlUnitOfWork), sp => sp.GetRequiredService(), lifetime)); - services.TryAdd(new ServiceDescriptor(typeof(IQueryableUnitOfWork), sp => sp.GetRequiredService(), lifetime)); - + services.TryAdd(new ServiceDescriptor(typeof(IQueryableUnitOfWork), sp => sp.GetRequiredService(), lifetime)); + services.TryAdd(new ServiceDescriptor(typeof(ISqlUnitOfWork), sp => sp.GetRequiredService(), lifetime)); services.TryAdd(new ServiceDescriptor(typeof(IQueryableUnitOfWork), sp => sp.GetRequiredService(), lifetime)); } private static void AddGenericRepositories(IServiceCollection services, ServiceLifetime lifetime) { - services.TryAdd(new ServiceDescriptor(typeof(Repository<,,>), typeof(Repository<,,>), lifetime)); - services.TryAdd(new ServiceDescriptor(typeof(AsyncRepository<,,>), typeof(AsyncRepository<,,>), lifetime)); services.TryAdd(new ServiceDescriptor(typeof(QueryableRepository<,,>), typeof(QueryableRepository<,,>), lifetime)); services.TryAdd(new ServiceDescriptor(typeof(AsyncQueryableRepository<,,>), typeof(AsyncQueryableRepository<,,>), lifetime)); } + private static void AddRepositories(IServiceCollection services, RepositoryOptions repoOptions) { var types = repoOptions.GetAssemblies() diff --git a/src/Repository/QueryableRepository.cs b/src/Repository/QueryableRepository.cs index 5bab552..bdf398d 100644 --- a/src/Repository/QueryableRepository.cs +++ b/src/Repository/QueryableRepository.cs @@ -1,17 +1,244 @@ -using System.Diagnostics.CodeAnalysis; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq.Expressions; +using eQuantic.Core.Data.EntityFramework.Repository.Read; +using eQuantic.Core.Data.EntityFramework.Repository.Write; using eQuantic.Core.Data.Repository; using eQuantic.Core.Data.Repository.Config; +using eQuantic.Core.Data.Repository.Read; +using eQuantic.Core.Data.Repository.Write; +using eQuantic.Linq.Specification; namespace eQuantic.Core.Data.EntityFramework.Repository; [ExcludeFromCodeCoverage] public class QueryableRepository : - Repository, TEntity, TKey>, + IRepository, TEntity, TKey>, IQueryableRepository where TUnitOfWork : IQueryableUnitOfWork where TEntity : class, IEntity, new() { - public QueryableRepository(TUnitOfWork unitOfWork) : base(unitOfWork) + private readonly IQueryableReadRepository _readRepository; + private readonly IWriteRepository _writeRepository; + private bool _disposed; + + /// + /// Create a new instance of repository + /// + /// Associated Unit Of Work + public QueryableRepository(TUnitOfWork unitOfWork) + { + this.UnitOfWork = unitOfWork; + this._readRepository = new QueryableReadRepository(unitOfWork); + this._writeRepository = new WriteRepository(unitOfWork); + } + + public TUnitOfWork UnitOfWork { get; private set; } + + public void Add(TEntity item) + { + this._writeRepository.Add(item); + } + + public IEnumerable AllMatching(ISpecification specification, + Action> configuration = default) + { + return this._readRepository.AllMatching(specification, configuration); + } + + public long Count() + { + return this._readRepository.Count(); + } + + public long Count(ISpecification specification) + { + return this._readRepository.Count(specification); + } + + public long Count(Expression> filter) + { + return this._readRepository.Count(filter); + } + + public bool All(ISpecification specification, Action> configuration = default) + { + return this._readRepository.All(specification, configuration); + } + + public bool All(Expression> filter, Action> configuration = default) + { + return this._readRepository.All(filter, configuration); + } + + public bool Any(Action> configuration = default) + { + return this._readRepository.Any(configuration); + } + + public bool Any(ISpecification specification, Action> configuration = default) { + return this._readRepository.Any(specification, configuration); + } + + public bool Any(Expression> filter, Action> configuration = default) + { + return this._readRepository.Any(filter, configuration); + } + + public long DeleteMany(Expression> filter) + { + return this._writeRepository.DeleteMany(filter); + } + + public long DeleteMany(ISpecification specification) + { + return this._writeRepository.DeleteMany(specification); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + public TEntity Get(TKey id, Action> configuration = default) + { + return this._readRepository.Get(id, configuration); + } + + public IEnumerable GetAll(Action> configuration = default) + { + return this._readRepository.GetAll(configuration); + } + + public IEnumerable GetMapped(Expression> filter, + Expression> map, Action> configuration = default) + { + return this._readRepository.GetMapped(filter, map, configuration); + } + + public IEnumerable GetMapped(ISpecification specification, + Expression> map, Action> configuration = default) + { + return this._readRepository.GetMapped(specification, map, configuration); + } + + public IEnumerable GetFiltered(Expression> filter, + Action> configuration = default) + { + return this._readRepository.GetFiltered(filter, configuration); + } + + public TEntity GetFirst(Expression> filter, Action> configuration = default) + { + return this._readRepository.GetFirst(filter, configuration); + } + + public TEntity GetFirst(ISpecification specification, Action> configuration = default) + { + return this._readRepository.GetFirst(specification, configuration); + } + + public TResult GetFirstMapped(Expression> filter, Expression> map, Action> configuration = default) + { + return this._readRepository.GetFirstMapped(filter, map, configuration); + } + + public TResult GetFirstMapped(ISpecification specification, Expression> map, Action> configuration = default) + { + return this._readRepository.GetFirstMapped(specification, map, configuration); + } + + public IEnumerable GetPaged(int limit, Action> configuration = default) + { + return this._readRepository.GetPaged(limit, configuration); + } + + public IEnumerable GetPaged(ISpecification specification, int limit, + Action> configuration = default) + { + return this._readRepository.GetPaged(specification, limit, configuration); + } + + public IEnumerable GetPaged(Expression> filter, int limit, + Action> configuration = default) + { + return this._readRepository.GetPaged(filter, limit, configuration); + } + + public IEnumerable GetPaged(int pageIndex, int pageSize, Action> configuration = default) + { + return this._readRepository.GetPaged(pageIndex, pageSize, configuration); + } + + public IEnumerable GetPaged(ISpecification specification, int pageIndex, int pageSize, + Action> configuration = default) + { + return this._readRepository.GetPaged(specification, pageIndex, pageSize, configuration); + } + + public IEnumerable GetPaged(Expression> filter, int pageIndex, int pageSize, + Action> configuration = default) + { + return this._readRepository.GetPaged(filter, pageIndex, pageSize, configuration); + } + + public TEntity GetSingle(Expression> filter, Action> configuration = default) + { + return this._readRepository.GetSingle(filter, configuration); + } + + public TEntity GetSingle(ISpecification specification, Action> configuration = default) + { + return this._readRepository.GetSingle(specification, configuration); + } + + public void Merge(TEntity persisted, TEntity current) + { + this._writeRepository.Merge(persisted, current); + } + + public void Modify(TEntity item) + { + this._writeRepository.Modify(item); + } + + public void Remove(TEntity item) + { + this._writeRepository.Remove(item); + } + + public void TrackItem(TEntity item) + { + this._writeRepository.TrackItem(item); + } + + public long UpdateMany(Expression> filter, Expression> updateFactory) + { + return this._writeRepository.UpdateMany(filter, updateFactory); + } + + public long UpdateMany(ISpecification specification, Expression> updateFactory) + { + return this._writeRepository.UpdateMany(specification, updateFactory); + } + + protected virtual void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + + if (disposing) + { + this._readRepository?.Dispose(); + this._writeRepository?.Dispose(); + UnitOfWork?.Dispose(); + } + + _disposed = true; } } diff --git a/src/Repository/Read/AsyncQueryableReadRepository.cs b/src/Repository/Read/AsyncQueryableReadRepository.cs index c721d87..5182bb4 100644 --- a/src/Repository/Read/AsyncQueryableReadRepository.cs +++ b/src/Repository/Read/AsyncQueryableReadRepository.cs @@ -1,18 +1,690 @@ -using System.Diagnostics.CodeAnalysis; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; using eQuantic.Core.Data.Repository; using eQuantic.Core.Data.Repository.Config; using eQuantic.Core.Data.Repository.Read; +using eQuantic.Linq.Specification; +using Microsoft.EntityFrameworkCore; namespace eQuantic.Core.Data.EntityFramework.Repository.Read; [ExcludeFromCodeCoverage] public class AsyncQueryableReadRepository : - AsyncReadRepository, TEntity, TKey>, - IAsyncQueryableReadRepository + QueryableReadRepository, + IAsyncQueryableReadRepository, + IAsyncReadRepository, TEntity, TKey> where TUnitOfWork : class, IQueryableUnitOfWork where TEntity : class, IEntity, new() { + private Set _dbSet; + public AsyncQueryableReadRepository(TUnitOfWork unitOfWork) : base(unitOfWork) { } + + public Task> AllMatchingAsync( + ISpecification specification, + Action> configuration = default) + { + return AllMatchingAsync(specification, configuration, CancellationToken.None); + } + + public async Task> AllMatchingAsync( + ISpecification specification, + Action> configuration, + CancellationToken cancellationToken) + { + return await GetQueryable(configuration, query => + query + .Where(specification.SatisfiedBy())) + .ToListAsync(cancellationToken); + } + + public Task> AllMatchingAsync( + ISpecification specification, + CancellationToken cancellationToken) + { + return AllMatchingAsync(specification, _ => { }, cancellationToken); + } + + public Task CountAsync(CancellationToken cancellationToken = default) + { + return GetSet().LongCountAsync(cancellationToken); + } + + public Task CountAsync( + ISpecification specification, + CancellationToken cancellationToken = default) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification)); + } + + return this.CountAsync(specification.SatisfiedBy(), cancellationToken); + } + + public Task CountAsync( + Expression> filter, + CancellationToken cancellationToken = default) + { + return GetSet().LongCountAsync(filter, cancellationToken); + } + + public Task AllAsync( + ISpecification specification, + Action> configuration = default) + { + return AllAsync(specification, configuration, CancellationToken.None); + } + + public Task AllAsync( + ISpecification specification, + Action> configuration, + CancellationToken cancellationToken) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification)); + } + + return AllAsync(specification.SatisfiedBy(), configuration, cancellationToken); + } + + public Task AllAsync( + ISpecification specification, + CancellationToken cancellationToken) + { + return AllAsync(specification, _ => { }, cancellationToken); + } + + public Task AllAsync( + Expression> filter, + Action> configuration = default) + { + return AllAsync(filter, configuration, CancellationToken.None); + } + + public Task AllAsync( + Expression> filter, + Action> configuration, + CancellationToken cancellationToken) + { + return GetQueryable(configuration, _ => _).AllAsync(filter, cancellationToken); + } + + public Task AllAsync( + Expression> filter, + CancellationToken cancellationToken) + { + return AllAsync(filter, _ => { }, cancellationToken); + } + + public Task AnyAsync( + Action> configuration = default) + { + return AnyAsync(configuration, CancellationToken.None); + } + + public Task AnyAsync( + Action> configuration, + CancellationToken cancellationToken) + { + return GetQueryable(configuration, _ => _).AnyAsync(cancellationToken); + } + + public Task AnyAsync( + CancellationToken cancellationToken) + { + return AnyAsync(_ => { }, cancellationToken); + } + + public Task AnyAsync( + ISpecification specification, + Action> configuration = default) + { + return AnyAsync(specification, configuration, CancellationToken.None); + } + + public Task AnyAsync( + ISpecification specification, + Action> configuration, + CancellationToken cancellationToken) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification)); + } + + return AnyAsync(specification.SatisfiedBy(), configuration, cancellationToken); + } + + public Task AnyAsync( + ISpecification specification, + CancellationToken cancellationToken) + { + return AnyAsync(specification, _ => {}, cancellationToken); + } + + public Task AnyAsync( + Expression> filter, + Action> configuration = default) + { + return AnyAsync(filter, configuration, CancellationToken.None); + } + + public Task AnyAsync( + Expression> filter, + Action> configuration, + CancellationToken cancellationToken) + { + return GetQueryable(configuration, query => query.Where(filter)).AnyAsync(cancellationToken); + } + + public Task AnyAsync( + Expression> filter, + CancellationToken cancellationToken = default) + { + return AnyAsync(filter, _ => {}, cancellationToken); + } + + public Task> GetAllAsync( + Action> configuration = default) + { + return GetAllAsync(configuration, CancellationToken.None); + } + + public async Task> GetAllAsync( + Action> configuration, + CancellationToken cancellationToken) + { + return await GetQueryable(configuration, query => query.Where(_ => true)) + .ToListAsync(cancellationToken); + } + + public Task> GetAllAsync( + CancellationToken cancellationToken = default) + { + return GetAllAsync(_ => { }, cancellationToken); + } + + public Task GetAsync( + TKey id, + Action> configuration = default) + { + return GetAsync(id, configuration, CancellationToken.None); + } + + public Task GetAsync( + TKey id, + Action> configuration, + CancellationToken cancellationToken) + { + if (id == null) + { + throw new ArgumentNullException(nameof(id)); + } + + return GetInternalAsync(id, configuration, cancellationToken); + } + + public Task GetAsync( + TKey id, + CancellationToken cancellationToken) + { + return GetAsync(id, _ => {}, cancellationToken); + } + + public Task> GetMappedAsync( + Expression> filter, + Expression> map, + Action> configuration = default) + { + return GetMappedAsync(filter, map, configuration, CancellationToken.None); + } + + public async Task> GetMappedAsync( + Expression> filter, + Expression> map, + Action> configuration, + CancellationToken cancellationToken) + { + return await GetQueryable(configuration, query => query.Where(filter)) + .Select(map) + .ToListAsync(cancellationToken); + } + + public Task> GetMappedAsync( + Expression> filter, + Expression> map, + CancellationToken cancellationToken) + { + return GetMappedAsync(filter, map, _ => { }, cancellationToken); + } + + public Task> GetMappedAsync( + ISpecification specification, + Expression> map, + Action> configuration = default) + { + return GetMappedAsync(specification, map, configuration, CancellationToken.None); + } + + public Task> GetMappedAsync( + ISpecification specification, + Expression> map, + Action> configuration, + CancellationToken cancellationToken) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification)); + } + + return GetMappedAsync(specification.SatisfiedBy(), map, configuration, cancellationToken); + } + + public Task> GetMappedAsync( + ISpecification specification, + Expression> map, + CancellationToken cancellationToken = default) + { + return GetMappedAsync(specification, map, _ => {}, cancellationToken); + } + + public Task> GetFilteredAsync( + Expression> filter, + Action> configuration = default) + { + return GetFilteredAsync(filter, configuration, CancellationToken.None); + } + + public async Task> GetFilteredAsync( + Expression> filter, + Action> configuration, + CancellationToken cancellationToken) + { + return await GetQueryable(configuration, query => query.Where(filter)) + .ToListAsync(cancellationToken); + } + + public Task> GetFilteredAsync( + Expression> filter, + CancellationToken cancellationToken) + { + return GetFilteredAsync(filter, _ => { }, cancellationToken); + } + + public Task GetFirstAsync( + Expression> filter, + Action> configuration = default) + { + return GetFirstAsync(filter, configuration, CancellationToken.None); + } + + public async Task GetFirstAsync( + Expression> filter, + Action> configuration, + CancellationToken cancellationToken) + { + return await GetQueryable(configuration, query => query.Where(filter)) + .FirstOrDefaultAsync(cancellationToken); + } + + public Task GetFirstAsync( + Expression> filter, + CancellationToken cancellationToken) + { + return GetFirstAsync(filter, _ => { }, cancellationToken); + } + + public Task GetFirstAsync( + ISpecification specification, + Action> configuration = default) + { + return GetFirstAsync(specification, configuration, CancellationToken.None); + } + + public Task GetFirstAsync( + ISpecification specification, + Action> configuration, + CancellationToken cancellationToken) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification)); + } + + return GetFirstAsync(specification.SatisfiedBy(), configuration, cancellationToken); + } + + public Task GetFirstAsync( + ISpecification specification, + CancellationToken cancellationToken) + { + return GetFirstAsync(specification, _ => {}, cancellationToken); + } + + public Task GetFirstMappedAsync( + Expression> filter, + Expression> map, + Action> configuration = default) + { + return GetFirstMappedAsync(filter, map, configuration, CancellationToken.None); + } + + public Task GetFirstMappedAsync( + Expression> filter, + Expression> map, + Action> configuration, + CancellationToken cancellationToken) + { + if (filter == null) + { + throw new ArgumentNullException(nameof(filter), "Filter expression cannot be null"); + } + + return GetQueryable(configuration, query => query.Where(filter)) + .Select(map) + .FirstOrDefaultAsync(cancellationToken); + } + + public Task GetFirstMappedAsync( + Expression> filter, + Expression> map, + CancellationToken cancellationToken) + { + return GetFirstMappedAsync(filter, map, _ => { }, cancellationToken); + } + + public Task GetFirstMappedAsync( + ISpecification specification, + Expression> map, + Action> configuration = default) + { + return GetFirstMappedAsync(specification, map, configuration, CancellationToken.None); + } + + public Task GetFirstMappedAsync( + ISpecification specification, + Expression> map, + Action> configuration, + CancellationToken cancellationToken) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification)); + } + + return GetFirstMappedAsync(specification.SatisfiedBy(), map, configuration, cancellationToken); + } + + public Task GetFirstMappedAsync( + ISpecification specification, + Expression> map, + CancellationToken cancellationToken = default) + { + return GetFirstMappedAsync(specification, map, _ => {}, cancellationToken); + } + + public Task> GetPagedAsync( + int limit, + Action> configuration = default) + { + return GetPagedAsync(limit, configuration, CancellationToken.None); + } + + public Task> GetPagedAsync( + int limit, + Action> configuration, + CancellationToken cancellationToken) + { + return GetPagedAsync((Expression>)null, 1, limit, configuration, cancellationToken); + } + + public Task> GetPagedAsync( + int limit, + CancellationToken cancellationToken = default) + { + return GetPagedAsync(limit, _ => {}, cancellationToken); + } + + public Task> GetPagedAsync( + ISpecification specification, + int limit, + Action> configuration = default) + { + return GetPagedAsync(specification, limit, configuration, CancellationToken.None); + } + + public Task> GetPagedAsync( + ISpecification specification, + int limit, + Action> configuration, + CancellationToken cancellationToken) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification)); + } + + return GetPagedAsync(specification.SatisfiedBy(), 1, limit, configuration, cancellationToken); + } + + public Task> GetPagedAsync( + ISpecification specification, + int limit, + CancellationToken cancellationToken) + { + return GetPagedAsync(specification, limit, _ => {}, cancellationToken); + } + + public Task> GetPagedAsync( + Expression> filter, + int limit, + Action> configuration = default) + { + return GetPagedAsync(filter, limit, configuration, CancellationToken.None); + } + + public Task> GetPagedAsync( + Expression> filter, + int limit, + Action> configuration, + CancellationToken cancellationToken) + { + return GetPagedAsync(filter, 1, limit, configuration, cancellationToken); + } + + public Task> GetPagedAsync( + Expression> filter, + int limit, + CancellationToken cancellationToken) + { + return GetPagedAsync(filter, limit, _ => {}, cancellationToken); + } + + public Task> GetPagedAsync( + int pageIndex, + int pageSize, + Action> configuration = default) + { + return GetPagedAsync(pageIndex, pageSize, configuration, + CancellationToken.None); + } + + public Task> GetPagedAsync( + int pageIndex, + int pageSize, + Action> configuration, + CancellationToken cancellationToken) + { + return GetPagedAsync((Expression>)null, pageIndex, pageSize, configuration, + cancellationToken); + } + + public Task> GetPagedAsync( + int pageIndex, + int pageSize, + CancellationToken cancellationToken) + { + return GetPagedAsync(pageIndex, pageSize, _ => {}, + cancellationToken); + } + + public Task> GetPagedAsync( + ISpecification specification, + int pageIndex, + int pageSize, + Action> configuration = default) + { + return GetPagedAsync(specification, pageIndex, pageSize, configuration, CancellationToken.None); + } + + public Task> GetPagedAsync( + ISpecification specification, + int pageIndex, + int pageSize, + Action> configuration, + CancellationToken cancellationToken) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification)); + } + + return GetPagedAsync(specification.SatisfiedBy(), pageIndex, pageSize, configuration, cancellationToken); + } + + public Task> GetPagedAsync( + ISpecification specification, + int pageIndex, + int pageSize, + CancellationToken cancellationToken) + { + return GetPagedAsync(specification, pageIndex, pageSize, _ => {}, cancellationToken); + } + + public Task> GetPagedAsync( + Expression> filter, + int pageIndex, + int pageSize, + Action> configuration = default) + { + return GetPagedAsync(filter, pageIndex, pageSize, configuration, CancellationToken.None); + } + + public async Task> GetPagedAsync( + Expression> filter, + int pageIndex, + int pageSize, + Action> configuration, + CancellationToken cancellationToken) + { + var query = GetQueryable(configuration, internalQuery => + { + if (filter != null) + { + internalQuery = internalQuery.Where(filter); + } + + return internalQuery; + }); + + + if (pageSize > 0) + { + return await query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync(cancellationToken); + } + + return await query.ToListAsync(cancellationToken); + } + + public Task> GetPagedAsync( + Expression> filter, + int pageIndex, + int pageSize, + CancellationToken cancellationToken) + { + return GetPagedAsync(filter, pageIndex, pageSize, _ => {}, cancellationToken); + } + + public Task GetSingleAsync( + Expression> filter, + Action> configuration = default) + { + return GetSingleAsync(filter, configuration, CancellationToken.None); + } + + public async Task GetSingleAsync( + Expression> filter, + Action> configuration, + CancellationToken cancellationToken) + { + return await GetQueryable(configuration, query => query.Where(filter)) + .SingleOrDefaultAsync(cancellationToken); + } + + public Task GetSingleAsync( + Expression> filter, + CancellationToken cancellationToken) + { + return GetSingleAsync(filter, _ => { }, cancellationToken); + } + + public Task GetSingleAsync( + ISpecification specification, + Action> configuration = default) + { + return GetSingleAsync(specification, configuration, CancellationToken.None); + } + + public Task GetSingleAsync( + ISpecification specification, + Action> configuration, + CancellationToken cancellationToken) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification)); + } + + return GetSingleAsync(specification.SatisfiedBy(), configuration, cancellationToken); + } + + public Task GetSingleAsync( + ISpecification specification, + CancellationToken cancellationToken) + { + return GetSingleAsync(specification, _ => {}, cancellationToken); + } + + private async Task GetInternalAsync(TKey id, Action> configuration = default, + CancellationToken cancellationToken = default) + { + if (configuration == null) + { + return await GetSet().FindAsync(id, cancellationToken); + } + + var idExpression = GetSet().GetExpression(id); + return await GetQueryable(configuration, query => query.Where(idExpression)) + .SingleOrDefaultAsync(cancellationToken); + } + + private IQueryable GetQueryable(Action> configuration, + Func, IQueryable> internalQueryAction) + { + return GetSet().GetQueryable(configuration, internalQueryAction); + } + + private Set GetSet() + { + return _dbSet ??= (Set)UnitOfWork.CreateSet(); + } } diff --git a/src/Repository/Read/AsyncReadRepository.cs b/src/Repository/Read/AsyncReadRepository.cs deleted file mode 100644 index fcb20ef..0000000 --- a/src/Repository/Read/AsyncReadRepository.cs +++ /dev/null @@ -1,289 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; -using eQuantic.Core.Data.Repository; -using eQuantic.Core.Data.Repository.Config; -using eQuantic.Core.Data.Repository.Read; -using eQuantic.Linq.Specification; -using Microsoft.EntityFrameworkCore; - -namespace eQuantic.Core.Data.EntityFramework.Repository.Read; - -public class AsyncReadRepository : - ReadRepository, IAsyncReadRepository - where TUnitOfWork : IQueryableUnitOfWork - where TEntity : class, IEntity, new() - where TConfig : Configuration -{ - private Set _dbSet; - - public AsyncReadRepository(TUnitOfWork unitOfWork) : base(unitOfWork) - { - } - - public async Task> AllMatchingAsync(ISpecification specification, - Action configuration = default, CancellationToken cancellationToken = default) - { - return await GetQueryable(configuration, query => - query - .Where(specification.SatisfiedBy())) - .ToListAsync(cancellationToken); - } - - public Task CountAsync(CancellationToken cancellationToken = default) - { - return GetSet().LongCountAsync(cancellationToken); - } - - public Task CountAsync(ISpecification specification, CancellationToken cancellationToken = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification)); - } - - return this.CountAsync(specification.SatisfiedBy(), cancellationToken); - } - - public Task CountAsync(Expression> filter, CancellationToken cancellationToken = default) - { - return GetSet().LongCountAsync(filter, cancellationToken); - } - - public Task AllAsync(ISpecification specification, Action configuration = default, - CancellationToken cancellationToken = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification)); - } - - return this.AllAsync(specification.SatisfiedBy(), configuration, cancellationToken); - } - - public Task AllAsync(Expression> filter, Action configuration = default, - CancellationToken cancellationToken = default) - { - return GetQueryable(configuration, _ => _).AllAsync(filter, cancellationToken); - } - - public Task AnyAsync(Action configuration = default, CancellationToken cancellationToken = default) - { - return GetQueryable(configuration, _ => _).AnyAsync(cancellationToken); - } - - public Task AnyAsync(ISpecification specification, Action configuration = default, - CancellationToken cancellationToken = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification)); - } - - return this.AnyAsync(specification.SatisfiedBy(), configuration, cancellationToken); - } - - public Task AnyAsync(Expression> filter, Action configuration = default, - CancellationToken cancellationToken = default) - { - return GetQueryable(configuration, query => query.Where(filter)).AnyAsync(cancellationToken); - } - - public async Task> GetAllAsync(Action configuration = default, - CancellationToken cancellationToken = default) - { - return await GetQueryable(configuration, query => query.Where(_ => true)) - .ToListAsync(cancellationToken); - } - - public Task GetAsync(TKey id, Action configuration = default, - CancellationToken cancellationToken = default) - { - if (id == null) - { - throw new ArgumentNullException(nameof(id)); - } - - return GetInternalAsync(id, configuration, cancellationToken); - } - - public async Task> GetMappedAsync(Expression> filter, - Expression> map, Action configuration = default, - CancellationToken cancellationToken = default) - { - return await GetQueryable(configuration, query => query.Where(filter)) - .Select(map) - .ToListAsync(cancellationToken); - } - - public Task> GetMappedAsync(ISpecification specification, - Expression> map, Action configuration = default, - CancellationToken cancellationToken = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification)); - } - - return this.GetMappedAsync(specification.SatisfiedBy(), map, configuration, cancellationToken); - } - - private async Task GetInternalAsync(TKey id, Action configuration = default, - CancellationToken cancellationToken = default) - { - if (configuration == null) - { - return await GetSet().FindAsync(id, cancellationToken); - } - - var idExpression = GetSet().GetExpression(id); - return await GetQueryable(configuration, query => query.Where(idExpression)) - .SingleOrDefaultAsync(cancellationToken); - } - - public async Task> GetFilteredAsync(Expression> filter, - Action configuration = default, CancellationToken cancellationToken = default) - { - return await GetQueryable(configuration, query => query.Where(filter)) - .ToListAsync(cancellationToken); - } - - public async Task GetFirstAsync(Expression> filter, - Action configuration = default, CancellationToken cancellationToken = default) - { - return await GetQueryable(configuration, query => query.Where(filter)) - .FirstOrDefaultAsync(cancellationToken); - } - - public Task GetFirstAsync(ISpecification specification, Action configuration = default, - CancellationToken cancellationToken = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification)); - } - - return GetFirstAsync(specification.SatisfiedBy(), configuration, cancellationToken); - } - - public Task GetFirstMappedAsync(Expression> filter, - Expression> map, Action configuration = default, - CancellationToken cancellationToken = default) - { - if (filter == null) - { - throw new ArgumentNullException(nameof(filter), "Filter expression cannot be null"); - } - - return GetQueryable(configuration, query => query.Where(filter)) - .Select(map) - .FirstOrDefaultAsync(cancellationToken); - } - - public Task GetFirstMappedAsync(ISpecification specification, - Expression> map, Action configuration = default, - CancellationToken cancellationToken = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification)); - } - - return this.GetFirstMappedAsync(specification.SatisfiedBy(), map, configuration, cancellationToken); - } - - public Task> GetPagedAsync(int limit, Action configuration = default, - CancellationToken cancellationToken = default) - { - return GetPagedAsync((Expression>)null, 1, limit, configuration, cancellationToken); - } - - public Task> GetPagedAsync(ISpecification specification, int limit, - Action configuration = default, CancellationToken cancellationToken = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification)); - } - - return GetPagedAsync(specification.SatisfiedBy(), 1, limit, configuration, cancellationToken); - } - - public Task> GetPagedAsync(Expression> filter, int limit, - Action configuration = default, CancellationToken cancellationToken = default) - { - return GetPagedAsync(filter, 1, limit, configuration, cancellationToken); - } - - public Task> GetPagedAsync(int pageIndex, int pageSize, - Action configuration = default, CancellationToken cancellationToken = default) - { - return GetPagedAsync((Expression>)null, pageIndex, pageSize, configuration, - cancellationToken); - } - - public Task> GetPagedAsync(ISpecification specification, int pageIndex, int pageSize, - Action configuration = default, CancellationToken cancellationToken = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification)); - } - - return GetPagedAsync(specification.SatisfiedBy(), pageIndex, pageSize, configuration, cancellationToken); - } - - public async Task> GetPagedAsync(Expression> filter, int pageIndex, - int pageSize, Action configuration = default, CancellationToken cancellationToken = default) - { - var query = GetQueryable(configuration, internalQuery => - { - if (filter != null) - { - internalQuery = internalQuery.Where(filter); - } - - return internalQuery; - }); - - - if (pageSize > 0) - { - return await query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync(cancellationToken); - } - - return await query.ToListAsync(cancellationToken); - } - - public async Task GetSingleAsync(Expression> filter, - Action configuration = default, CancellationToken cancellationToken = default) - { - return await GetQueryable(configuration, query => query.Where(filter)) - .SingleOrDefaultAsync(cancellationToken); - } - - public Task GetSingleAsync(ISpecification specification, Action configuration = default, - CancellationToken cancellationToken = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification)); - } - - return GetSingleAsync(specification.SatisfiedBy(), configuration, cancellationToken); - } - - private IQueryable GetQueryable(Action configuration, - Func, IQueryable> internalQueryAction) - { - return GetSet().GetQueryable(configuration, internalQueryAction); - } - - private Set GetSet() - { - return _dbSet ??= (Set)UnitOfWork.CreateSet(); - } -} diff --git a/src/Repository/Read/QueryableReadRepository.cs b/src/Repository/Read/QueryableReadRepository.cs index 9b1e09e..6752b25 100644 --- a/src/Repository/Read/QueryableReadRepository.cs +++ b/src/Repository/Read/QueryableReadRepository.cs @@ -1,18 +1,320 @@ -using System.Diagnostics.CodeAnalysis; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Linq.Expressions; using eQuantic.Core.Data.Repository; using eQuantic.Core.Data.Repository.Config; using eQuantic.Core.Data.Repository.Read; +using eQuantic.Linq.Specification; namespace eQuantic.Core.Data.EntityFramework.Repository.Read; [ExcludeFromCodeCoverage] public class QueryableReadRepository : - ReadRepository, TEntity, TKey>, - IQueryableReadRepository + IQueryableReadRepository, + IReadRepository, TEntity, TKey> where TUnitOfWork : IQueryableUnitOfWork where TEntity : class, IEntity, new() { - public QueryableReadRepository(TUnitOfWork unitOfWork) : base(unitOfWork) + private Set _dbSet; + private bool _disposed; + private const string SpecificationCannotBeNull = "Specification cannot be null"; + private const string FilterExpressionCannotBeNull = "Filter expression cannot be null"; + /// + /// Creates a new instance of the read repository + /// + /// Associated Unit Of Work + public QueryableReadRepository(TUnitOfWork unitOfWork) { + UnitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork)); + } + + /// + /// + /// + public TUnitOfWork UnitOfWork { get; private set; } + + public IEnumerable AllMatching(ISpecification specification, + Action> configuration = default) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); + } + + return GetQueryable(configuration, query => query.Where(specification.SatisfiedBy())); + } + + public long Count() + { + return GetSet().LongCount(); + } + + public long Count(ISpecification specification) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); + } + + return this.Count(specification.SatisfiedBy()); + } + + public long Count(Expression> filter) + { + if (filter == null) + { + throw new ArgumentNullException(nameof(filter), FilterExpressionCannotBeNull); + } + + return GetSet().LongCount(filter); + } + + public bool All(ISpecification specification, Action> configuration = default) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); + } + + return this.All(specification.SatisfiedBy()); + } + + public bool All(Expression> filter, Action> configuration = default) + { + if (filter == null) + { + throw new ArgumentNullException(nameof(filter), FilterExpressionCannotBeNull); + } + + return GetQueryable(configuration, _ => _).All(filter); + } + + public bool Any(Action> configuration = default) + { + return GetQueryable(configuration, _ => _).Any(); + } + + public bool Any(ISpecification specification, Action> configuration = default) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); + } + + return this.Any(specification.SatisfiedBy()); + } + + public bool Any(Expression> filter, Action> configuration = default) + { + if (filter == null) + { + throw new ArgumentNullException(nameof(filter), FilterExpressionCannotBeNull); + } + + return GetQueryable(configuration, query => query.Where(filter)).Any(); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + public TEntity Get(TKey id, Action> configuration = default) + { + if (id == null) + { + throw new ArgumentNullException(nameof(id)); + } + + if (configuration == null) + { + return GetSet().Find(id); + } + + var idExpression = GetSet().GetExpression(id); + return GetQueryable(configuration, query => query.Where(idExpression)) + .SingleOrDefault(); + } + + public IEnumerable GetAll(Action> configuration = default) + { + return GetQueryable(configuration, query => query); + } + + public IEnumerable GetMapped(Expression> filter, + Expression> map, Action> configuration = default) + { + return GetQueryable(configuration, query => query.Where(filter)).Select(map); + } + + public IEnumerable GetMapped(ISpecification specification, + Expression> map, Action> configuration = default) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); + } + + return this.GetMapped(specification.SatisfiedBy(), map, configuration); + } + + public IEnumerable GetFiltered(Expression> filter, + Action> configuration = default) + { + if (filter == null) + { + throw new ArgumentNullException(nameof(filter), FilterExpressionCannotBeNull); + } + + return GetQueryable(configuration, query => query.Where(filter)); + } + + public TEntity GetFirst(Expression> filter, Action> configuration = default) + { + if (filter == null) + { + throw new ArgumentNullException(nameof(filter), FilterExpressionCannotBeNull); + } + + return GetQueryable(configuration, query => query.Where(filter)).FirstOrDefault(); + } + + public TEntity GetFirst(ISpecification specification, Action> configuration = default) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification),SpecificationCannotBeNull); + } + + return GetQueryable(configuration, query => query.Where(specification.SatisfiedBy())).FirstOrDefault(); + } + + public TResult GetFirstMapped(Expression> filter, + Expression> map, Action> configuration = default) + { + if (filter == null) + { + throw new ArgumentNullException(nameof(filter), FilterExpressionCannotBeNull); + } + return GetQueryable(configuration, query => query.Where(filter)) + .Select(map) + .FirstOrDefault(); + } + + public TResult GetFirstMapped(ISpecification specification, + Expression> map, Action> configuration = default) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); + } + + return this.GetFirstMapped(specification.SatisfiedBy(), map, configuration); + } + + public IEnumerable GetPaged(int limit, Action> configuration = default) + { + return GetPaged((Expression>)null, 1, limit, configuration); + } + + public IEnumerable GetPaged(ISpecification specification, int limit, + Action> configuration = default) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); + } + + return GetPaged(specification.SatisfiedBy(), 1, limit, configuration); + } + + public IEnumerable GetPaged(Expression> filter, int limit, + Action> configuration = default) + { + return GetPaged(filter, 1, limit, configuration); + } + + public IEnumerable GetPaged(int pageIndex, int pageSize, Action> configuration = default) + { + return GetPaged((Expression>)null, pageIndex, pageSize, configuration); + } + + public IEnumerable GetPaged(ISpecification specification, int pageIndex, int pageSize, + Action> configuration = default) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); + } + + return GetPaged(specification.SatisfiedBy(), pageIndex, pageSize, configuration); + } + + public IEnumerable GetPaged(Expression> filter, int pageIndex, int pageSize, + Action> configuration = default) + { + var query = GetQueryable(configuration, internalQuery => + { + if (filter != null) + { + internalQuery = internalQuery.Where(filter); + } + + return internalQuery; + }); + return pageSize > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize) : query; + } + + public TEntity GetSingle(Expression> filter, Action> configuration = default) + { + if (filter == null) + { + throw new ArgumentNullException(nameof(filter), FilterExpressionCannotBeNull); + } + + return GetQueryable(configuration, query => query.Where(filter)) + .SingleOrDefault(); + } + + public TEntity GetSingle(ISpecification specification, Action> configuration = default) + { + if (specification == null) + { + throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); + } + + return GetQueryable(configuration, query => + query + .Where(specification.SatisfiedBy())) + .SingleOrDefault(); + } + + protected virtual void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + + if (disposing) + { + UnitOfWork?.Dispose(); + } + + _disposed = true; + } + + private IQueryable GetQueryable(Action> configuration, + Func, IQueryable> internalQueryAction) + { + return GetSet().GetQueryable(configuration, internalQueryAction); + } + + private Set GetSet() + { + return _dbSet ??= (Set)UnitOfWork.CreateSet(); } } diff --git a/src/Repository/Read/ReadRepository.cs b/src/Repository/Read/ReadRepository.cs deleted file mode 100644 index 04151c0..0000000 --- a/src/Repository/Read/ReadRepository.cs +++ /dev/null @@ -1,317 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using eQuantic.Core.Data.Repository; -using eQuantic.Core.Data.Repository.Config; -using eQuantic.Core.Data.Repository.Read; -using eQuantic.Linq.Specification; - -namespace eQuantic.Core.Data.EntityFramework.Repository.Read; - -public class ReadRepository : IReadRepository - where TUnitOfWork : IQueryableUnitOfWork - where TEntity : class, IEntity, new() - where TConfig : Configuration -{ - private Set _dbSet; - private bool _disposed; - private const string SpecificationCannotBeNull = "Specification cannot be null"; - private const string FilterExpressionCannotBeNull = "Filter expression cannot be null"; - /// - /// Creates a new instance of the read repository - /// - /// Associated Unit Of Work - public ReadRepository(TUnitOfWork unitOfWork) - { - UnitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork)); - } - - /// - /// - /// - public TUnitOfWork UnitOfWork { get; private set; } - - public IEnumerable AllMatching(ISpecification specification, - Action configuration = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); - } - - return GetQueryable(configuration, query => query.Where(specification.SatisfiedBy())); - } - - public long Count() - { - return GetSet().LongCount(); - } - - public long Count(ISpecification specification) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); - } - - return this.Count(specification.SatisfiedBy()); - } - - public long Count(Expression> filter) - { - if (filter == null) - { - throw new ArgumentNullException(nameof(filter), FilterExpressionCannotBeNull); - } - - return GetSet().LongCount(filter); - } - - public bool All(ISpecification specification, Action configuration = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); - } - - return this.All(specification.SatisfiedBy()); - } - - public bool All(Expression> filter, Action configuration = default) - { - if (filter == null) - { - throw new ArgumentNullException(nameof(filter), FilterExpressionCannotBeNull); - } - - return GetQueryable(configuration, _ => _).All(filter); - } - - public bool Any(Action configuration = default) - { - return GetQueryable(configuration, _ => _).Any(); - } - - public bool Any(ISpecification specification, Action configuration = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); - } - - return this.Any(specification.SatisfiedBy()); - } - - public bool Any(Expression> filter, Action configuration = default) - { - if (filter == null) - { - throw new ArgumentNullException(nameof(filter), FilterExpressionCannotBeNull); - } - - return GetQueryable(configuration, query => query.Where(filter)).Any(); - } - - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - public TEntity Get(TKey id, Action configuration = default) - { - if (id == null) - { - throw new ArgumentNullException(nameof(id)); - } - - if (configuration == null) - { - return GetSet().Find(id); - } - - var idExpression = GetSet().GetExpression(id); - return GetQueryable(configuration, query => query.Where(idExpression)) - .SingleOrDefault(); - } - - public IEnumerable GetAll(Action configuration = default) - { - return GetQueryable(configuration, query => query); - } - - public IEnumerable GetMapped(Expression> filter, - Expression> map, Action configuration = default) - { - return GetQueryable(configuration, query => query.Where(filter)).Select(map); - } - - public IEnumerable GetMapped(ISpecification specification, - Expression> map, Action configuration = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); - } - - return this.GetMapped(specification.SatisfiedBy(), map, configuration); - } - - public IEnumerable GetFiltered(Expression> filter, - Action configuration = default) - { - if (filter == null) - { - throw new ArgumentNullException(nameof(filter), FilterExpressionCannotBeNull); - } - - return GetQueryable(configuration, query => query.Where(filter)); - } - - public TEntity GetFirst(Expression> filter, Action configuration = default) - { - if (filter == null) - { - throw new ArgumentNullException(nameof(filter), FilterExpressionCannotBeNull); - } - - return GetQueryable(configuration, query => query.Where(filter)).FirstOrDefault(); - } - - public TEntity GetFirst(ISpecification specification, Action configuration = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification),SpecificationCannotBeNull); - } - - return GetQueryable(configuration, query => query.Where(specification.SatisfiedBy())).FirstOrDefault(); - } - - public TResult GetFirstMapped(Expression> filter, - Expression> map, Action configuration = default) - { - if (filter == null) - { - throw new ArgumentNullException(nameof(filter), FilterExpressionCannotBeNull); - } - return GetQueryable(configuration, query => query.Where(filter)) - .Select(map) - .FirstOrDefault(); - } - - public TResult GetFirstMapped(ISpecification specification, - Expression> map, Action configuration = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); - } - - return this.GetFirstMapped(specification.SatisfiedBy(), map, configuration); - } - - public IEnumerable GetPaged(int limit, Action configuration = default) - { - return GetPaged((Expression>)null, 1, limit, configuration); - } - - public IEnumerable GetPaged(ISpecification specification, int limit, - Action configuration = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); - } - - return GetPaged(specification.SatisfiedBy(), 1, limit, configuration); - } - - public IEnumerable GetPaged(Expression> filter, int limit, - Action configuration = default) - { - return GetPaged(filter, 1, limit, configuration); - } - - public IEnumerable GetPaged(int pageIndex, int pageSize, Action configuration = default) - { - return GetPaged((Expression>)null, pageIndex, pageSize, configuration); - } - - public IEnumerable GetPaged(ISpecification specification, int pageIndex, int pageSize, - Action configuration = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); - } - - return GetPaged(specification.SatisfiedBy(), pageIndex, pageSize, configuration); - } - - public IEnumerable GetPaged(Expression> filter, int pageIndex, int pageSize, - Action configuration = default) - { - var query = GetQueryable(configuration, internalQuery => - { - if (filter != null) - { - internalQuery = internalQuery.Where(filter); - } - - return internalQuery; - }); - return pageSize > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize) : query; - } - - public TEntity GetSingle(Expression> filter, Action configuration = default) - { - if (filter == null) - { - throw new ArgumentNullException(nameof(filter), FilterExpressionCannotBeNull); - } - - return GetQueryable(configuration, query => query.Where(filter)) - .SingleOrDefault(); - } - - public TEntity GetSingle(ISpecification specification, Action configuration = default) - { - if (specification == null) - { - throw new ArgumentNullException(nameof(specification), SpecificationCannotBeNull); - } - - return GetQueryable(configuration, query => - query - .Where(specification.SatisfiedBy())) - .SingleOrDefault(); - } - - protected virtual void Dispose(bool disposing) - { - if (_disposed) - { - return; - } - - if (disposing) - { - UnitOfWork?.Dispose(); - } - - _disposed = true; - } - - private IQueryable GetQueryable(Action configuration, - Func, IQueryable> internalQueryAction) - { - return GetSet().GetQueryable(configuration, internalQueryAction); - } - - private Set GetSet() - { - return _dbSet ??= (Set)UnitOfWork.CreateSet(); - } -} diff --git a/src/Repository/Repository.cs b/src/Repository/Repository.cs deleted file mode 100644 index f6325e6..0000000 --- a/src/Repository/Repository.cs +++ /dev/null @@ -1,251 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq.Expressions; -using eQuantic.Core.Data.EntityFramework.Repository.Read; -using eQuantic.Core.Data.EntityFramework.Repository.Write; -using eQuantic.Core.Data.Repository; -using eQuantic.Core.Data.Repository.Config; -using eQuantic.Core.Data.Repository.Read; -using eQuantic.Core.Data.Repository.Write; -using eQuantic.Linq.Specification; - -namespace eQuantic.Core.Data.EntityFramework.Repository; - -public class Repository : IRepository - where TUnitOfWork : IQueryableUnitOfWork - where TEntity : class, IEntity, new() - where TConfig : Configuration -{ - private readonly IReadRepository _readRepository; - private readonly IWriteRepository _writeRepository; - private bool _disposed; - - /// - /// Create a new instance of repository - /// - /// Associated Unit Of Work - public Repository(TUnitOfWork unitOfWork) - { - this.UnitOfWork = unitOfWork; - this._readRepository = new ReadRepository(unitOfWork); - this._writeRepository = new WriteRepository(unitOfWork); - } - - public TUnitOfWork UnitOfWork { get; private set; } - - public void Add(TEntity item) - { - this._writeRepository.Add(item); - } - - public IEnumerable AllMatching(ISpecification specification, - Action configuration = default) - { - return this._readRepository.AllMatching(specification, configuration); - } - - public long Count() - { - return this._readRepository.Count(); - } - - public long Count(ISpecification specification) - { - return this._readRepository.Count(specification); - } - - public long Count(Expression> filter) - { - return this._readRepository.Count(filter); - } - - public bool All(ISpecification specification, Action configuration = default) - { - return this._readRepository.All(specification, configuration); - } - - public bool All(Expression> filter, Action configuration = default) - { - return this._readRepository.All(filter, configuration); - } - - public bool Any(Action configuration = default) - { - return this._readRepository.Any(configuration); - } - - public bool Any(ISpecification specification, Action configuration = default) - { - return this._readRepository.Any(specification, configuration); - } - - public bool Any(Expression> filter, Action configuration = default) - { - return this._readRepository.Any(filter, configuration); - } - - public long DeleteMany(Expression> filter) - { - return this._writeRepository.DeleteMany(filter); - } - - public long DeleteMany(ISpecification specification) - { - return this._writeRepository.DeleteMany(specification); - } - - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - public TEntity Get(TKey id, Action configuration = default) - { - return this._readRepository.Get(id, configuration); - } - - public IEnumerable GetAll(Action configuration = default) - { - return this._readRepository.GetAll(configuration); - } - - public IEnumerable GetMapped(Expression> filter, - Expression> map, Action configuration = default) - { - return this._readRepository.GetMapped(filter, map, configuration); - } - - public IEnumerable GetMapped(ISpecification specification, - Expression> map, Action configuration = default) - { - return this._readRepository.GetMapped(specification, map, configuration); - } - - public IEnumerable GetFiltered(Expression> filter, - Action configuration = default) - { - return this._readRepository.GetFiltered(filter, configuration); - } - - public TEntity GetFirst(Expression> filter, Action configuration = default) - { - return this._readRepository.GetFirst(filter, configuration); - } - - public TEntity GetFirst(ISpecification specification, Action configuration = default) - { - return this._readRepository.GetFirst(specification, configuration); - } - - public TResult GetFirstMapped(Expression> filter, Expression> map, Action configuration = default) - { - return this._readRepository.GetFirstMapped(filter, map, configuration); - } - - public TResult GetFirstMapped(ISpecification specification, Expression> map, Action configuration = default) - { - return this._readRepository.GetFirstMapped(specification, map, configuration); - } - - public IEnumerable GetPaged(int limit, Action configuration = default) - { - return this._readRepository.GetPaged(limit, configuration); - } - - public IEnumerable GetPaged(ISpecification specification, int limit, - Action configuration = default) - { - return this._readRepository.GetPaged(specification, limit, configuration); - } - - public IEnumerable GetPaged(Expression> filter, int limit, - Action configuration = default) - { - return this._readRepository.GetPaged(filter, limit, configuration); - } - - public IEnumerable GetPaged(int pageIndex, int pageSize, Action configuration = default) - { - return this._readRepository.GetPaged(pageIndex, pageSize, configuration); - } - - public IEnumerable GetPaged(ISpecification specification, int pageIndex, int pageSize, - Action configuration = default) - { - return this._readRepository.GetPaged(specification, pageIndex, pageSize, configuration); - } - - public IEnumerable GetPaged(Expression> filter, int pageIndex, int pageSize, - Action configuration = default) - { - return this._readRepository.GetPaged(filter, pageIndex, pageSize, configuration); - } - - public TEntity GetSingle(Expression> filter, Action configuration = default) - { - return this._readRepository.GetSingle(filter, configuration); - } - - public TEntity GetSingle(ISpecification specification, Action configuration = default) - { - return this._readRepository.GetSingle(specification, configuration); - } - - public void Merge(TEntity persisted, TEntity current) - { - this._writeRepository.Merge(persisted, current); - } - - public void Modify(TEntity item) - { - this._writeRepository.Modify(item); - } - - public void Remove(TEntity item) - { - this._writeRepository.Remove(item); - } - - public void TrackItem(TEntity item) - { - this._writeRepository.TrackItem(item); - } - - public long UpdateMany(Expression> filter, Expression> updateFactory) - { - return this._writeRepository.UpdateMany(filter, updateFactory); - } - - public long UpdateMany(ISpecification specification, Expression> updateFactory) - { - return this._writeRepository.UpdateMany(specification, updateFactory); - } - - protected virtual void Dispose(bool disposing) - { - if (_disposed) - { - return; - } - - if (disposing) - { - this._readRepository?.Dispose(); - this._writeRepository?.Dispose(); - UnitOfWork?.Dispose(); - } - - _disposed = true; - } -} - -public class Repository : Repository, TEntity, TKey>, - IRepository - where TUnitOfWork : IQueryableUnitOfWork - where TEntity : class, IEntity, new() -{ - public Repository(TUnitOfWork unitOfWork) : base(unitOfWork) - { - } -} diff --git a/src/Repository/UnitOfWork.cs b/src/Repository/UnitOfWork.cs index 25a0e5f..ff2957b 100644 --- a/src/Repository/UnitOfWork.cs +++ b/src/Repository/UnitOfWork.cs @@ -24,50 +24,15 @@ protected UnitOfWork(DbContext context) : base(context) internal DbContext GetDbContext() => Context; } -public abstract class UnitOfWork : UnitOfWork, ISqlUnitOfWork - where TDbContext : DbContext - where TUnitOfWork : ISqlUnitOfWork -{ - private readonly IServiceProvider _serviceProvider; - - protected UnitOfWork(IServiceProvider serviceProvider, TDbContext context) : base(context) - { - _serviceProvider = serviceProvider; - } - - public virtual IRepository GetRepository() - where TEntity : class, IEntity, new() - { - return _serviceProvider.GetRequiredService>(); - } - - public IAsyncRepository GetAsyncRepository() - where TEntity : class, IEntity, new() - { - return _serviceProvider.GetRequiredService>(); - } - - public IQueryableRepository GetQueryableRepository() - where TEntity : class, IEntity, new() - { - return _serviceProvider.GetRequiredService>(); - } - - public IAsyncQueryableRepository GetAsyncQueryableRepository() - where TEntity : class, IEntity, new() - { - return _serviceProvider.GetRequiredService>(); - } -} - public abstract class UnitOfWork : UnitOfWork, ISqlUnitOfWork where TDbContext : DbContext { private readonly TDbContext _context; - + private readonly IServiceProvider _serviceProvider; - protected UnitOfWork(TDbContext context) : base(context) + protected UnitOfWork(IServiceProvider serviceProvider, TDbContext context) : base(context) { + _serviceProvider = serviceProvider; _context = context; } @@ -269,6 +234,34 @@ public virtual SaveOptions GetSaveOptions() return new SaveOptions(); } + public virtual IRepository GetRepository() + where TEntity : class, IEntity, new() where TUnitOfWork : IUnitOfWork + { + var repo = _serviceProvider.GetRequiredService>(); + return repo; + } + + public IAsyncRepository GetAsyncRepository() + where TEntity : class, IEntity, new() + where TUnitOfWork : IUnitOfWork + { + return _serviceProvider.GetRequiredService>(); + } + + public IQueryableRepository GetQueryableRepository() + where TEntity : class, IEntity, new() + where TUnitOfWork : IQueryableUnitOfWork + { + return _serviceProvider.GetRequiredService>(); + } + + public IAsyncQueryableRepository GetAsyncQueryableRepository() + where TEntity : class, IEntity, new() + where TUnitOfWork : IQueryableUnitOfWork + { + return _serviceProvider.GetRequiredService>(); + } + private Data.Repository.ISet InternalCreateSet() where TEntity : class, IEntity, new() => new Set(_context); } diff --git a/src/eQuantic.Core.Data.EntityFramework.csproj b/src/eQuantic.Core.Data.EntityFramework.csproj index 06509ec..78e81a7 100644 --- a/src/eQuantic.Core.Data.EntityFramework.csproj +++ b/src/eQuantic.Core.Data.EntityFramework.csproj @@ -4,7 +4,7 @@ Core Data library for Entity Framework eQuantic.Core.Data.EntityFramework - 4.1.3.0 + 4.2.0.0 eQuantic Systems netstandard2.1;net6.0;net7.0;net8.0 eQuantic.Core.Data.EntityFramework @@ -23,15 +23,15 @@ LICENSE README.md Copyright © 2016 - 4.1.3.0 - 4.1.3.0 + 4.2.0.0 + 4.2.0.0 Icon.png latest - + @@ -46,7 +46,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + @@ -61,7 +61,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + @@ -76,7 +76,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + @@ -91,7 +91,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - +