From 05f5816dcf02e41b9f91fc31c43b740c4eb0caf1 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Fri, 3 Jul 2020 23:42:37 +0800 Subject: [PATCH] Added GetPagedListAsync methods to the repository --- .../Repositories/BasicRepositoryBase.cs | 2 + .../Repositories/IReadOnlyBasicRepository.cs | 9 ++++ .../EntityFrameworkCore/EfCoreRepository.cs | 16 +++++++ .../MemoryDb/MemoryDbRepository.cs | 48 ++++++++++++------- .../Repositories/MongoDB/MongoDbRepository.cs | 15 ++++++ .../RepositoryRegistration_Tests.cs | 6 +++ .../TestApp/Testing/Repository_Basic_Tests.cs | 14 ++++++ 7 files changed, 93 insertions(+), 17 deletions(-) diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/BasicRepositoryBase.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/BasicRepositoryBase.cs index d13d52ed97..ae1b03475c 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/BasicRepositoryBase.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/BasicRepositoryBase.cs @@ -35,6 +35,8 @@ namespace Volo.Abp.Domain.Repositories public abstract Task GetCountAsync(CancellationToken cancellationToken = default); + public abstract Task> GetPagedListAsync(int skipCount, int maxResultCount, string sorting, bool includeDetails = false, CancellationToken cancellationToken = default); + protected virtual CancellationToken GetCancellationToken(CancellationToken preferredValue = default) { return CancellationTokenProvider.FallbackToProvider(preferredValue); diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IReadOnlyBasicRepository.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IReadOnlyBasicRepository.cs index 828e305ff8..b0d270a97e 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IReadOnlyBasicRepository.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IReadOnlyBasicRepository.cs @@ -21,6 +21,15 @@ namespace Volo.Abp.Domain.Repositories /// Gets total count of all entities. /// Task GetCountAsync(CancellationToken cancellationToken = default); + + Task> GetPagedListAsync( + int skipCount, + int maxResultCount, + string sorting, + bool includeDetails = false, + CancellationToken cancellationToken = default); + + } public interface IReadOnlyBasicRepository : IReadOnlyBasicRepository diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs index a48e5d7da5..9515c72b6e 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs +++ b/framework/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.Dynamic.Core; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; @@ -88,6 +89,21 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore return await DbSet.LongCountAsync(GetCancellationToken(cancellationToken)); } + public override async Task> GetPagedListAsync( + int skipCount, + int maxResultCount, + string sorting, + bool includeDetails = false, + CancellationToken cancellationToken = default) + { + var queryable = includeDetails ? WithDetails() : DbSet; + + return await queryable + .OrderBy(sorting) + .PageBy(skipCount, maxResultCount) + .ToListAsync(GetCancellationToken(cancellationToken)); + } + protected override IQueryable GetQueryable() { return DbSet.AsQueryable(); diff --git a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs index 1dc38bf5ea..fdf66b08a6 100644 --- a/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs +++ b/framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Dynamic.Core; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; @@ -25,21 +26,21 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb public virtual IMemoryDatabase Database => DatabaseProvider.GetDatabase(); protected IMemoryDatabaseProvider DatabaseProvider { get; } - + public ILocalEventBus LocalEventBus { get; set; } public IDistributedEventBus DistributedEventBus { get; set; } public IEntityChangeEventHelper EntityChangeEventHelper { get; set; } - + public IAuditPropertySetter AuditPropertySetter { get; set; } - + public IGuidGenerator GuidGenerator { get; set; } public MemoryDbRepository(IMemoryDatabaseProvider databaseProvider) { DatabaseProvider = databaseProvider; - + LocalEventBus = NullLocalEventBus.Instance; DistributedEventBus = NullDistributedEventBus.Instance; EntityChangeEventHelper = NullEntityChangeEventHelper.Instance; @@ -49,7 +50,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb { return ApplyDataFilters(Collection.AsQueryable()); } - + protected virtual async Task TriggerDomainEventsAsync(object entity) { var generatesDomainEventsEntity = entity as IGeneratesDomainEvents; @@ -80,7 +81,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb generatesDomainEventsEntity.ClearDistributedEvents(); } } - + protected virtual bool IsHardDeleted(TEntity entity) { if (!(UnitOfWorkManager?.Current?.Items.GetOrDefault(UnitOfWorkItemNames.HardDeletedEntities) is HashSet hardDeletedEntities)) @@ -90,7 +91,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb return hardDeletedEntities.Contains(entity); } - + protected virtual void CheckAndSetId(TEntity entity) { if (entity is IEntity entityWithGuidId) @@ -117,12 +118,12 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb { AuditPropertySetter.SetCreationProperties(entity); } - + protected virtual void SetModificationAuditProperties(TEntity entity) { AuditPropertySetter.SetModificationProperties(entity); } - + protected virtual void SetDeletionAuditProperties(TEntity entity) { AuditPropertySetter.SetDeletionProperties(entity); @@ -139,7 +140,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb await EntityChangeEventHelper.TriggerEntityUpdatedEventOnUowCompletedAsync(entity); await EntityChangeEventHelper.TriggerEntityUpdatingEventAsync(entity); } - + protected virtual async Task TriggerEntityDeleteEventsAsync(TEntity entity) { await EntityChangeEventHelper.TriggerEntityDeletedEventOnUowCompletedAsync(entity); @@ -153,7 +154,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb await TriggerEntityCreateEvents(entity); await TriggerDomainEventsAsync(entity); } - + protected virtual async Task ApplyAbpConceptsForDeletedEntityAsync(TEntity entity) { SetDeletionAuditProperties(entity); @@ -170,8 +171,8 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb } public override async Task DeleteAsync( - Expression> predicate, - bool autoSave = false, + Expression> predicate, + bool autoSave = false, CancellationToken cancellationToken = default) { var entities = GetQueryable().Where(predicate).ToList(); @@ -189,10 +190,10 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb await ApplyAbpConceptsForAddedEntityAsync(entity); Collection.Add(entity); - + return entity; } - + public override async Task UpdateAsync( TEntity entity, bool autoSave = false, @@ -216,7 +217,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb return entity; } - + public override async Task DeleteAsync( TEntity entity, bool autoSave = false, @@ -244,6 +245,19 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb { return Task.FromResult(GetQueryable().LongCount()); } + + public override Task> GetPagedListAsync( + int skipCount, + int maxResultCount, + string sorting, + bool includeDetails = false, + CancellationToken cancellationToken = default) + { + return Task.FromResult(GetQueryable() + .OrderBy(sorting) + .PageBy(skipCount, maxResultCount) + .ToList()); + } } public class MemoryDbRepository : MemoryDbRepository, IMemoryDbRepository @@ -296,4 +310,4 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb await DeleteAsync(x => x.Id.Equals(id), autoSave, cancellationToken); } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs index 6abcc70f0d..132801b203 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs @@ -3,6 +3,7 @@ using MongoDB.Driver.Linq; using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Dynamic.Core; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; @@ -147,6 +148,20 @@ namespace Volo.Abp.Domain.Repositories.MongoDB return await GetMongoQueryable().LongCountAsync(GetCancellationToken(cancellationToken)); } + public override async Task> GetPagedListAsync( + int skipCount, + int maxResultCount, + string sorting, + bool includeDetails = false, + CancellationToken cancellationToken = default) + { + return await GetMongoQueryable() + .OrderBy(sorting) + .As>() + .PageBy>(skipCount, maxResultCount) + .ToListAsync(GetCancellationToken(cancellationToken)); + } + public override async Task DeleteAsync( Expression> predicate, bool autoSave = false, diff --git a/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs index 067f4b6a01..57b8054784 100644 --- a/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs +++ b/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs @@ -280,6 +280,12 @@ namespace Volo.Abp.Domain.Repositories { throw new NotImplementedException(); } + + public override Task> GetPagedListAsync(int skipCount, int maxResultCount, string sorting, bool includeDetails = false, + CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } } public class MyTestDefaultRepository : MyTestDefaultRepository, IRepository diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/Repository_Basic_Tests.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/Repository_Basic_Tests.cs index d82d6206a1..6e72bd1f3c 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/Repository_Basic_Tests.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/Repository_Basic_Tests.cs @@ -35,6 +35,20 @@ namespace Volo.Abp.TestApp.Testing persons.Count.ShouldBeGreaterThan(0); } + [Fact] + public async Task GetPagedListAsync() + { + var persons = await PersonRepository.GetPagedListAsync(0, 10, "name"); + persons.Count.ShouldBeGreaterThan(0); + } + + [Fact] + public async Task GetPagedListAsync_Should_Return_Empty() + { + var persons = await PersonRepository.GetPagedListAsync(1, 10, "name"); + persons.Count.ShouldBe(0); + } + [Fact] public async Task GetAsync_With_Predicate() {