diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs index f37e3d51e6..a5b880b421 100644 --- a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs +++ b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs @@ -1,6 +1,7 @@ using System; -using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Volo.Abp.Domain.Entities; @@ -40,14 +41,10 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore return DbSet.AsQueryable(); } - public override Task> GetListAsync() + public override async Task GetAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken)) { - return GetQueryable().ToListAsync(); - } + var entity = await FindAsync(id, cancellationToken); - public override async Task GetAsync(TPrimaryKey id) - { - var entity = await FindAsync(id); if (entity == null) { throw new EntityNotFoundException(typeof(TEntity), id); @@ -56,16 +53,14 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore return entity; } - //TODO: Find by multiple primary key - public override TEntity Find(TPrimaryKey id) { return DbSet.Find(id); } - public override Task FindAsync(TPrimaryKey id) + public override Task FindAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken)) { - return DbSet.FindAsync(id); + return DbSet.FindAsync(new object[] { id }, cancellationToken); } public override TEntity Insert(TEntity entity) @@ -80,10 +75,10 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore return insertedEntity.Id; } - public override async Task InsertAndGetIdAsync(TEntity entity) + public override async Task InsertAndGetIdAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)) { - var insertedEntity = await InsertAsync(entity); - await DbContext.SaveChangesAsync(true); + var insertedEntity = await InsertAsync(entity, cancellationToken); + await DbContext.SaveChangesAsync(cancellationToken); return insertedEntity.Id; } @@ -109,9 +104,13 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore DbSet.Remove(entity); } - public override Task CountAsync() + public override async Task DeleteAsync(Expression> predicate, CancellationToken cancellationToken = default(CancellationToken)) { - return DbSet.CountAsync(); + var entities = await GetQueryable().Where(predicate).ToListAsync(cancellationToken); + foreach (var entity in entities) + { + DbSet.Remove(entity); + } } } } diff --git a/src/Volo.Abp/Volo/Abp/Domain/Repositories/IQueryableRepository.cs b/src/Volo.Abp/Volo/Abp/Domain/Repositories/IQueryableRepository.cs index b54c8eda3c..588717899e 100644 --- a/src/Volo.Abp/Volo/Abp/Domain/Repositories/IQueryableRepository.cs +++ b/src/Volo.Abp/Volo/Abp/Domain/Repositories/IQueryableRepository.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using JetBrains.Annotations; using Volo.Abp.Domain.Entities; @@ -16,6 +17,8 @@ namespace Volo.Abp.Domain.Repositories public interface IQueryableRepository : IRepository, IQueryable where TEntity : class, IEntity { + //TODO: Is Delete needed? + /// /// Deletes many entities by function. /// Notice that: All entities fits to given predicate are retrieved and deleted. @@ -31,7 +34,8 @@ namespace Volo.Abp.Domain.Repositories /// This may cause major performance problems if there are too many entities with /// given predicate. /// + /// A to observe while waiting for the task to complete. /// A condition to filter entities - Task DeleteAsync([NotNull] Expression> predicate); + Task DeleteAsync([NotNull] Expression> predicate, CancellationToken cancellationToken = default(CancellationToken)); } } \ No newline at end of file diff --git a/src/Volo.Abp/Volo/Abp/Domain/Repositories/IRepository.cs b/src/Volo.Abp/Volo/Abp/Domain/Repositories/IRepository.cs index e9219c78dc..3012dd5272 100644 --- a/src/Volo.Abp/Volo/Abp/Domain/Repositories/IRepository.cs +++ b/src/Volo.Abp/Volo/Abp/Domain/Repositories/IRepository.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using JetBrains.Annotations; using Volo.Abp.Domain.Entities; @@ -23,20 +23,6 @@ namespace Volo.Abp.Domain.Repositories public interface IRepository : IRepository where TEntity : class, IEntity { - /// - /// Used to get all entities. - /// - /// List of all entities - [NotNull] - List GetList(); - - /// - /// Used to get all entities. - /// - /// List of all entities - [NotNull] - Task> GetListAsync(); - /// /// Gets an entity with given primary key. /// Throws if can not find an entity with given id. @@ -51,9 +37,10 @@ namespace Volo.Abp.Domain.Repositories /// Throws if can not find an entity with given id. /// /// Primary key of the entity to get + /// A to observe while waiting for the task to complete. /// Entity [NotNull] - Task GetAsync(TPrimaryKey id); + Task GetAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken)); /// /// Gets an entity with given primary key or null if not found. @@ -67,8 +54,9 @@ namespace Volo.Abp.Domain.Repositories /// Gets an entity with given primary key or null if not found. /// /// Primary key of the entity to get + /// A to observe while waiting for the task to complete. /// Entity or null - Task FindAsync(TPrimaryKey id); + Task FindAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken)); /// /// Inserts a new entity. @@ -80,9 +68,10 @@ namespace Volo.Abp.Domain.Repositories /// /// Inserts a new entity. /// + /// A to observe while waiting for the task to complete. /// Inserted entity [NotNull] - Task InsertAsync([NotNull] TEntity entity); + Task InsertAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default(CancellationToken)); /// /// Inserts a new entity and gets it's Id. @@ -98,9 +87,10 @@ namespace Volo.Abp.Domain.Repositories /// It may require to save current unit of work /// to be able to retrieve id. /// + /// A to observe while waiting for the task to complete. /// Entity /// Id of the entity - Task InsertAndGetIdAsync([NotNull] TEntity entity); + Task InsertAndGetIdAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default(CancellationToken)); /// /// Updates an existing entity. @@ -112,9 +102,10 @@ namespace Volo.Abp.Domain.Repositories /// /// Updates an existing entity. /// + /// A to observe while waiting for the task to complete. /// Entity [NotNull] - Task UpdateAsync([NotNull] TEntity entity); + Task UpdateAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default(CancellationToken)); /// /// Deletes an entity. @@ -125,8 +116,9 @@ namespace Volo.Abp.Domain.Repositories /// /// Deletes an entity. /// + /// A to observe while waiting for the task to complete. /// Entity to be deleted - Task DeleteAsync([NotNull] TEntity entity); + Task DeleteAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default(CancellationToken)); /// /// Deletes an entity by primary key. @@ -137,19 +129,8 @@ namespace Volo.Abp.Domain.Repositories /// /// Deletes an entity by primary key. /// + /// A to observe while waiting for the task to complete. /// Primary key of the entity - Task DeleteAsync(TPrimaryKey id); - - /// - /// Gets count of all entities in this repository. - /// - /// Count of entities - int Count(); - - /// - /// Gets count of all entities in this repository. - /// - /// Count of entities - Task CountAsync(); + Task DeleteAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken)); } } diff --git a/src/Volo.Abp/Volo/Abp/Domain/Repositories/QueryableRepositoryBase.cs b/src/Volo.Abp/Volo/Abp/Domain/Repositories/QueryableRepositoryBase.cs index dd2e1f6416..1833e87418 100644 --- a/src/Volo.Abp/Volo/Abp/Domain/Repositories/QueryableRepositoryBase.cs +++ b/src/Volo.Abp/Volo/Abp/Domain/Repositories/QueryableRepositoryBase.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Entities; @@ -35,11 +36,6 @@ namespace Volo.Abp.Domain.Repositories protected abstract IQueryable GetQueryable(); - public override List GetList() - { - return GetQueryable().ToList(); - } - public override TEntity Find(TPrimaryKey id) { return GetQueryable().FirstOrDefault(CreateEqualityExpressionForId(id)); @@ -53,15 +49,10 @@ namespace Volo.Abp.Domain.Repositories } } - public virtual Task DeleteAsync(Expression> predicate) + public virtual Task DeleteAsync(Expression> predicate, CancellationToken cancellationToken = default(CancellationToken)) { Delete(predicate); return Task.CompletedTask; } - - public override int Count() - { - return GetQueryable().Count(); - } } } \ No newline at end of file diff --git a/src/Volo.Abp/Volo/Abp/Domain/Repositories/RepositoryBase.cs b/src/Volo.Abp/Volo/Abp/Domain/Repositories/RepositoryBase.cs index 08ce710839..8883edcd11 100644 --- a/src/Volo.Abp/Volo/Abp/Domain/Repositories/RepositoryBase.cs +++ b/src/Volo.Abp/Volo/Abp/Domain/Repositories/RepositoryBase.cs @@ -1,6 +1,6 @@ using System; -using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Entities; @@ -15,16 +15,10 @@ namespace Volo.Abp.Domain.Repositories public abstract class RepositoryBase : IRepository where TEntity : class, IEntity { - public abstract List GetList(); - - public virtual Task> GetListAsync() - { - return Task.FromResult(GetList()); - } - public virtual TEntity Get(TPrimaryKey id) { var entity = Find(id); + if (entity == null) { throw new EntityNotFoundException(typeof(TEntity), id); @@ -33,21 +27,21 @@ namespace Volo.Abp.Domain.Repositories return entity; } - public virtual Task GetAsync(TPrimaryKey id) + public virtual Task GetAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken)) { return Task.FromResult(Get(id)); } public abstract TEntity Find(TPrimaryKey id); - public virtual Task FindAsync(TPrimaryKey id) + public virtual Task FindAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken)) { return Task.FromResult(Find(id)); } public abstract TEntity Insert(TEntity entity); - public virtual Task InsertAsync(TEntity entity) + public virtual Task InsertAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)) { return Task.FromResult(Insert(entity)); } @@ -57,21 +51,21 @@ namespace Volo.Abp.Domain.Repositories return Insert(entity).Id; } - public virtual Task InsertAndGetIdAsync(TEntity entity) + public virtual Task InsertAndGetIdAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)) { return Task.FromResult(InsertAndGetId(entity)); } public abstract TEntity Update(TEntity entity); - public virtual Task UpdateAsync(TEntity entity) + public virtual Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)) { return Task.FromResult(Update(entity)); } public abstract void Delete(TEntity entity); - public virtual Task DeleteAsync(TEntity entity) + public virtual Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)) { Delete(entity); return Task.CompletedTask; @@ -88,19 +82,12 @@ namespace Volo.Abp.Domain.Repositories Delete(entity); } - public virtual Task DeleteAsync(TPrimaryKey id) + public virtual Task DeleteAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken)) { Delete(id); return Task.CompletedTask; } - public abstract int Count(); - - public virtual Task CountAsync() - { - return Task.FromResult(Count()); - } - protected static Expression> CreateEqualityExpressionForId(TPrimaryKey id) { var lambdaParam = Expression.Parameter(typeof(TEntity));