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;
@ -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>

@ -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