Added GetPagedListAsync methods to the repository

pull/4617/head
liangshiwei 5 years ago
parent bc7141a819
commit 05f5816dcf

@ -35,6 +35,8 @@ namespace Volo.Abp.Domain.Repositories
public abstract Task<long> GetCountAsync(CancellationToken cancellationToken = default);
public abstract Task<List<TEntity>> GetPagedListAsync(int skipCount, int maxResultCount, string sorting, bool includeDetails = false, CancellationToken cancellationToken = default);
protected virtual CancellationToken GetCancellationToken(CancellationToken preferredValue = default)
{
return CancellationTokenProvider.FallbackToProvider(preferredValue);

@ -21,6 +21,15 @@ namespace Volo.Abp.Domain.Repositories
/// Gets total count of all entities.
/// </summary>
Task<long> GetCountAsync(CancellationToken cancellationToken = default);
Task<List<TEntity>> GetPagedListAsync(
int skipCount,
int maxResultCount,
string sorting,
bool includeDetails = false,
CancellationToken cancellationToken = default);
}
public interface IReadOnlyBasicRepository<TEntity, TKey> : IReadOnlyBasicRepository<TEntity>

@ -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<List<TEntity>> 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<TEntity> GetQueryable()
{
return DbSet.AsQueryable();

@ -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<TMemoryDbContext> 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<TMemoryDbContext> 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<IEntity> 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<Guid> 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<Func<TEntity, bool>> predicate,
bool autoSave = false,
Expression<Func<TEntity, bool>> 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<TEntity> 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<List<TEntity>> 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<TMemoryDbContext, TEntity, TKey> : MemoryDbRepository<TMemoryDbContext, TEntity>, IMemoryDbRepository<TEntity, TKey>
@ -296,4 +310,4 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
await DeleteAsync(x => x.Id.Equals(id), autoSave, cancellationToken);
}
}
}
}

@ -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<List<TEntity>> GetPagedListAsync(
int skipCount,
int maxResultCount,
string sorting,
bool includeDetails = false,
CancellationToken cancellationToken = default)
{
return await GetMongoQueryable()
.OrderBy(sorting)
.As<IMongoQueryable<TEntity>>()
.PageBy<TEntity, IMongoQueryable<TEntity>>(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken));
}
public override async Task DeleteAsync(
Expression<Func<TEntity, bool>> predicate,
bool autoSave = false,

@ -280,6 +280,12 @@ namespace Volo.Abp.Domain.Repositories
{
throw new NotImplementedException();
}
public override Task<List<TEntity>> GetPagedListAsync(int skipCount, int maxResultCount, string sorting, bool includeDetails = false,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}
public class MyTestDefaultRepository<TEntity, TKey> : MyTestDefaultRepository<TEntity>, IRepository<TEntity, TKey>

@ -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()
{

Loading…
Cancel
Save