tests now full clear the mongo collections / added GetSortedPaginatedAsync + tests

This commit is contained in:
Alexandre SPIESER
2019-04-14 22:47:06 +01:00
parent d2df667b3f
commit baaf2b5ee9
9 changed files with 322 additions and 54 deletions
@@ -71,7 +71,7 @@ namespace CoreIntegrationTests.Infrastructure
public void AddOne() public void AddOne()
{ {
// Arrange // Arrange
var document = new T(); var document = CreateTestDocument();
// Act // Act
SUT.AddOne<T>(document); SUT.AddOne<T>(document);
// Assert // Assert
@@ -84,7 +84,7 @@ namespace CoreIntegrationTests.Infrastructure
public async Task AddOneAsync() public async Task AddOneAsync()
{ {
// Arrange // Arrange
var document = new T(); var document = CreateTestDocument();
// Act // Act
await SUT.AddOneAsync<T>(document); await SUT.AddOneAsync<T>(document);
// Assert // Assert
@@ -97,7 +97,7 @@ namespace CoreIntegrationTests.Infrastructure
public void AddMany() public void AddMany()
{ {
// Arrange // Arrange
var documents = new List<T> { new T(), new T() }; var documents = CreateTestDocuments(2);
// Act // Act
SUT.AddMany<T>(documents); SUT.AddMany<T>(documents);
// Assert // Assert
@@ -115,7 +115,7 @@ namespace CoreIntegrationTests.Infrastructure
if (!string.IsNullOrEmpty(PartitionKey)) if (!string.IsNullOrEmpty(PartitionKey))
{ {
// Arrange // Arrange
var documents = new List<T> { new T(), new T(), new T(), new T() }; var documents = CreateTestDocuments(4);
if (documents.Any(e => e is IPartitionedDocument)) if (documents.Any(e => e is IPartitionedDocument))
{ {
var secondPartitionKey = $"{PartitionKey}-2"; var secondPartitionKey = $"{PartitionKey}-2";
@@ -138,7 +138,7 @@ namespace CoreIntegrationTests.Infrastructure
public async Task AddManyAsync() public async Task AddManyAsync()
{ {
// Arrange // Arrange
var documents = new List<T> { new T(), new T() }; var documents = CreateTestDocuments(2);
// Act // Act
await SUT.AddManyAsync<T>(documents); await SUT.AddManyAsync<T>(documents);
// Assert // Assert
@@ -156,7 +156,7 @@ namespace CoreIntegrationTests.Infrastructure
if (!string.IsNullOrEmpty(PartitionKey)) if (!string.IsNullOrEmpty(PartitionKey))
{ {
// Arrange // Arrange
var documents = new List<T> { new T(), new T(), new T(), new T() }; var documents = CreateTestDocuments(4);
if (documents.Any(e => e is IPartitionedDocument)) if (documents.Any(e => e is IPartitionedDocument))
{ {
var secondPartitionKey = $"{PartitionKey}-2"; var secondPartitionKey = $"{PartitionKey}-2";
@@ -1,4 +1,5 @@
using MongoDbGenericRepository.Models; using MongoDB.Driver;
using MongoDbGenericRepository.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
@@ -72,7 +73,7 @@ namespace CoreIntegrationTests.Infrastructure
public void AddOne() public void AddOne()
{ {
// Arrange // Arrange
var document = new T(); var document = CreateTestDocument();
// Act // Act
SUT.AddOne<T, TKey>(document); SUT.AddOne<T, TKey>(document);
// Assert // Assert
@@ -85,7 +86,7 @@ namespace CoreIntegrationTests.Infrastructure
public async Task AddOneAsync() public async Task AddOneAsync()
{ {
// Arrange // Arrange
var document = new T(); var document = CreateTestDocument();
// Act // Act
await SUT.AddOneAsync<T, TKey>(document); await SUT.AddOneAsync<T, TKey>(document);
// Assert // Assert
@@ -98,7 +99,7 @@ namespace CoreIntegrationTests.Infrastructure
public void AddMany() public void AddMany()
{ {
// Arrange // Arrange
var documents = new List<T> { new T(), new T() }; var documents = CreateTestDocuments(2);
// Act // Act
SUT.AddMany<T, TKey>(documents); SUT.AddMany<T, TKey>(documents);
// Assert // Assert
@@ -116,7 +117,7 @@ namespace CoreIntegrationTests.Infrastructure
if (!string.IsNullOrEmpty(PartitionKey)) if (!string.IsNullOrEmpty(PartitionKey))
{ {
// Arrange // Arrange
var documents = new List<T> { new T(), new T(), new T(), new T() }; var documents = CreateTestDocuments(4);
if (documents.Any(e => e is IPartitionedDocument)) if (documents.Any(e => e is IPartitionedDocument))
{ {
var secondPartitionKey = $"{PartitionKey}-2"; var secondPartitionKey = $"{PartitionKey}-2";
@@ -139,7 +140,7 @@ namespace CoreIntegrationTests.Infrastructure
public async Task AddManyAsync() public async Task AddManyAsync()
{ {
// Arrange // Arrange
var documents = new List<T> { new T(), new T() }; var documents = CreateTestDocuments(2);
// Act // Act
await SUT.AddManyAsync<T, TKey>(documents); await SUT.AddManyAsync<T, TKey>(documents);
// Assert // Assert
@@ -157,7 +158,7 @@ namespace CoreIntegrationTests.Infrastructure
if (!string.IsNullOrEmpty(PartitionKey)) if (!string.IsNullOrEmpty(PartitionKey))
{ {
// Arrange // Arrange
var documents = new List<T> { new T(), new T(), new T(), new T() }; var documents = CreateTestDocuments(4);
if (documents.Any(e => e is IPartitionedDocument)) if (documents.Any(e => e is IPartitionedDocument))
{ {
var secondPartitionKey = $"{PartitionKey}-2"; var secondPartitionKey = $"{PartitionKey}-2";
@@ -1149,7 +1150,8 @@ namespace CoreIntegrationTests.Infrastructure
// Act // Act
var result = SUT.GroupBy<T, int, ProjectedGroup, TKey>( var result = SUT.GroupBy<T, int, ProjectedGroup, TKey>(
e => e.GroupingKey, g => new ProjectedGroup e => e.GroupingKey,
g => new ProjectedGroup
{ {
Key = g.Key, Key = g.Key,
Content = g.Select(doc => doc.SomeContent).ToList() Content = g.Select(doc => doc.SomeContent).ToList()
@@ -1195,11 +1197,13 @@ namespace CoreIntegrationTests.Infrastructure
// Act // Act
var result = SUT.GroupBy<T, int, ProjectedGroup, TKey>( var result = SUT.GroupBy<T, int, ProjectedGroup, TKey>(
e => e.Children.Any(c => c.Type == guid1), e => e.Children.Any(c => c.Type == guid1),
e => e.GroupingKey, g => new ProjectedGroup e => e.GroupingKey,
g => new ProjectedGroup
{ {
Key = g.Key, Key = g.Key,
Content = g.Select(doc => doc.SomeContent).ToList() Content = g.Select(doc => doc.SomeContent).ToList()
}, PartitionKey); },
PartitionKey);
// Assert // Assert
var key1Group = result.First(e => e.Key == 4); var key1Group = result.First(e => e.Key == 4);
@@ -1212,6 +1216,86 @@ namespace CoreIntegrationTests.Infrastructure
#endregion Group By #endregion Group By
#region Pagination
public static Random Random = new Random();
[Fact]
public async Task GetSortedPaginatedAsync()
{
// Arrange
var content = $"{Guid.NewGuid()}";
var documents = CreateTestDocuments(10);
for (var i = 0; i < 5; i++)
{
documents[i].GroupingKey = 8;
documents[i].Nested.SomeAmount = Random.Next(1, 500000);
documents[i].SomeContent = content;
}
for (var i = 5; i < documents.Count; i++)
{
documents[i].GroupingKey = 9;
documents[i].SomeContent = content;
}
SUT.AddMany<T, TKey>(documents);
documents = documents.OrderByDescending(e => e.Nested.SomeAmount).ToList();
var notExpected = documents.First();
var expectedFirstResult = documents[1];
// Act
var result = await SUT.GetSortedPaginatedAsync<T, TKey>(
e => e.GroupingKey == 8 && e.SomeContent == content,
e => e.Nested.SomeAmount,
false,
1,5,
PartitionKey);
// Assert
Assert.Equal(4, result.Count);
Assert.True(!result.Contains(notExpected));
Assert.Equal(expectedFirstResult.Id, result[0].Id);
}
[Fact]
public async Task GetSortedPaginatedAsyncWithSortOptions()
{
// Arrange
var content = $"{Guid.NewGuid()}";
var documents = CreateTestDocuments(10);
for (var i = 0; i < 5; i++)
{
documents[i].GroupingKey = 8;
documents[i].Nested.SomeAmount = Random.Next(1, 500000);
documents[i].SomeContent = content;
}
for (var i = 5; i < documents.Count; i++)
{
documents[i].GroupingKey = 9;
documents[i].SomeContent = content;
}
SUT.AddMany<T, TKey>(documents);
documents = documents.OrderByDescending(e => e.Nested.SomeAmount).ToList();
var notExpected = documents.First();
var expectedFirstResult = documents[1];
var sorting = Builders<T>.Sort.Descending(e => e.Nested.SomeAmount);
// Act
var result = await SUT.GetSortedPaginatedAsync<T, TKey>(
e => e.GroupingKey == 8 && e.SomeContent == content,
sorting,
1, 5,
PartitionKey);
// Assert
Assert.Equal(4, result.Count);
Assert.True(!result.Contains(notExpected));
Assert.Equal(expectedFirstResult.Id, result[0].Id);
}
#endregion Pagination
#region Test Utils #region Test Utils
[MethodImpl(MethodImplOptions.NoInlining)] [MethodImpl(MethodImplOptions.NoInlining)]
@@ -16,15 +16,15 @@ namespace CoreIntegrationTests.Infrastructure
public MongoDbTestFixture() public MongoDbTestFixture()
{ {
DocsToDelete = new ConcurrentBag<T>();
} }
public string PartitionKey { get; set; } public string PartitionKey { get; set; }
public ConcurrentBag<T> DocsToDelete { get; set; } public static ConcurrentBag<T> DocsToDelete { get; set; } = new ConcurrentBag<T>();
public virtual void Dispose() public virtual void Dispose()
{ {
if (DocsToDelete.Any()) if (DocsToDelete.Any())
{ {
TestRepository.Instance.DeleteMany<T, TKey>(DocsToDelete.ToList()); TestRepository.Instance.DeleteMany<T, TKey>(DocsToDelete.ToList());
@@ -406,5 +406,49 @@ namespace MongoDbGenericRepository
where TProjection : class, new(); where TProjection : class, new();
#endregion Group By #endregion Group By
#region Pagination
/// <summary>
/// Asynchronously returns a paginated list of the documents matching the filter condition.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="sortSelector">The property selector.</param>
/// <param name="ascending">Order of the sorting.</param>
/// <param name="skipNumber">The number of documents you want to skip. Default value is 0.</param>
/// <param name="takeNumber">The number of documents you want to take. Default value is 50.</param>
/// <param name="partitionKey">An optional partition key.</param>
Task<List<TDocument>> GetSortedPaginatedAsync<TDocument, TKey>(
Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, object>> sortSelector,
bool ascending = true,
int skipNumber = 0,
int takeNumber = 50,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Asynchronously returns a paginated list of the documents matching the filter condition.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="sortDefinition">The sort definition.</param>
/// <param name="skipNumber">The number of documents you want to skip. Default value is 0.</param>
/// <param name="takeNumber">The number of documents you want to take. Default value is 50.</param>
/// <param name="partitionKey">An optional partition key.</param>
Task<List<TDocument>> GetSortedPaginatedAsync<TDocument, TKey>(
Expression<Func<TDocument, bool>> filter,
SortDefinition<TDocument> sortDefinition,
int skipNumber = 0,
int takeNumber = 50,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
#endregion Pagination
} }
} }
@@ -1,8 +1,4 @@
using MongoDbGenericRepository.Models; using System;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace MongoDbGenericRepository namespace MongoDbGenericRepository
{ {
@@ -39,37 +39,6 @@ namespace MongoDbGenericRepository
{ {
} }
/// <summary>
/// Asynchronously returns a paginated list of the documents matching the filter condition.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="sortSelector">The property selector.</param>
/// <param name="ascending">Order of the sorting.</param>
/// <param name="skipNumber">The number of documents you want to skip. Default value is 0.</param>
/// <param name="takeNumber">The number of documents you want to take. Default value is 50.</param>
/// <param name="partitionKey">An optional partition key.</param>
public virtual async Task<List<TDocument>> GetSortedPaginatedAsync<TDocument>(
Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, object>> sortSelector,
bool ascending = true,
int skipNumber = 0,
int takeNumber = 50,
string partitionKey = null)
where TDocument : IDocument
{
var sorting = ascending
? Builders<TDocument>.Sort.Ascending(sortSelector)
: Builders<TDocument>.Sort.Descending(sortSelector);
return await HandlePartitioned<TDocument>(partitionKey)
.Find(filter)
.Sort(sorting)
.Skip(skipNumber)
.Limit(takeNumber)
.ToListAsync();
}
/// <summary> /// <summary>
/// Asynchronously returns a paginated list of the documents matching the filter condition. /// Asynchronously returns a paginated list of the documents matching the filter condition.
/// </summary> /// </summary>
@@ -328,6 +328,7 @@ namespace MongoDbGenericRepository.DataAccess.Read
/// Sums the values of a selected field for a given filtered collection of documents. /// Sums the values of a selected field for a given filtered collection of documents.
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param> /// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param> /// <param name="partitionKey">The partition key of your document, if any.</param>
@@ -344,6 +345,7 @@ namespace MongoDbGenericRepository.DataAccess.Read
/// Sums the values of a selected field for a given filtered collection of documents. /// Sums the values of a selected field for a given filtered collection of documents.
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param> /// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param> /// <param name="partitionKey">The partition key of your document, if any.</param>
@@ -360,6 +362,7 @@ namespace MongoDbGenericRepository.DataAccess.Read
/// Sums the values of a selected field for a given filtered collection of documents. /// Sums the values of a selected field for a given filtered collection of documents.
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param> /// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param> /// <param name="partitionKey">The partition key of your document, if any.</param>
@@ -376,6 +379,7 @@ namespace MongoDbGenericRepository.DataAccess.Read
/// Sums the values of a selected field for a given filtered collection of documents. /// Sums the values of a selected field for a given filtered collection of documents.
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="selector">The field you want to sum.</param> /// <param name="selector">The field you want to sum.</param>
/// <param name="partitionKey">The partition key of your document, if any.</param> /// <param name="partitionKey">The partition key of your document, if any.</param>
@@ -398,6 +402,7 @@ namespace MongoDbGenericRepository.DataAccess.Read
/// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria.
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <typeparam name="TGroupKey">The type of the grouping criteria.</typeparam> /// <typeparam name="TGroupKey">The type of the grouping criteria.</typeparam>
/// <typeparam name="TProjection">The type of the projected group.</typeparam> /// <typeparam name="TProjection">The type of the projected group.</typeparam>
/// <param name="groupingCriteria">The grouping criteria.</param> /// <param name="groupingCriteria">The grouping criteria.</param>
@@ -423,6 +428,7 @@ namespace MongoDbGenericRepository.DataAccess.Read
/// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria.
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <typeparam name="TGroupKey">The type of the grouping criteria.</typeparam> /// <typeparam name="TGroupKey">The type of the grouping criteria.</typeparam>
/// <typeparam name="TProjection">The type of the projected group.</typeparam> /// <typeparam name="TProjection">The type of the projected group.</typeparam>
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
@@ -444,5 +450,65 @@ namespace MongoDbGenericRepository.DataAccess.Read
.Group(selector, projection) .Group(selector, projection)
.ToList(); .ToList();
} }
/// <summary>
/// Asynchronously returns a paginated list of the documents matching the filter condition.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="sortSelector">The property selector.</param>
/// <param name="ascending">Order of the sorting.</param>
/// <param name="skipNumber">The number of documents you want to skip. Default value is 0.</param>
/// <param name="takeNumber">The number of documents you want to take. Default value is 50.</param>
/// <param name="partitionKey">An optional partition key.</param>
public virtual async Task<List<TDocument>> GetSortedPaginatedAsync<TDocument, TKey>(
Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, object>> sortSelector,
bool ascending = true,
int skipNumber = 0,
int takeNumber = 50,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var sorting = ascending
? Builders<TDocument>.Sort.Ascending(sortSelector)
: Builders<TDocument>.Sort.Descending(sortSelector);
return await HandlePartitioned<TDocument, TKey>(partitionKey)
.Find(filter)
.Sort(sorting)
.Skip(skipNumber)
.Limit(takeNumber)
.ToListAsync();
}
/// <summary>
/// Asynchronously returns a paginated list of the documents matching the filter condition.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="sortDefinition">The sort definition.</param>
/// <param name="skipNumber">The number of documents you want to skip. Default value is 0.</param>
/// <param name="takeNumber">The number of documents you want to take. Default value is 50.</param>
/// <param name="partitionKey">An optional partition key.</param>
public virtual async Task<List<TDocument>> GetSortedPaginatedAsync<TDocument, TKey>(
Expression<Func<TDocument, bool>> filter,
SortDefinition<TDocument> sortDefinition,
int skipNumber = 0,
int takeNumber = 50,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return await HandlePartitioned<TDocument, TKey>(partitionKey)
.Find(filter)
.Sort(sortDefinition)
.Skip(skipNumber)
.Limit(takeNumber)
.ToListAsync();
}
} }
} }
@@ -485,5 +485,47 @@ namespace MongoDbGenericRepository
{ {
return MongoDbReader.GroupBy<TDocument, TGroupKey, TProjection, TKey>(filter, groupingCriteria, groupProjection, partitionKey); return MongoDbReader.GroupBy<TDocument, TGroupKey, TProjection, TKey>(filter, groupingCriteria, groupProjection, partitionKey);
} }
/// <summary>
/// Asynchronously returns a paginated list of the documents matching the filter condition.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="sortSelector">The property selector.</param>
/// <param name="ascending">Order of the sorting.</param>
/// <param name="skipNumber">The number of documents you want to skip. Default value is 0.</param>
/// <param name="takeNumber">The number of documents you want to take. Default value is 50.</param>
/// <param name="partitionKey">An optional partition key.</param>
public virtual async Task<List<TDocument>> GetSortedPaginatedAsync<TDocument>(
Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, object>> sortSelector,
bool ascending = true,
int skipNumber = 0,
int takeNumber = 50,
string partitionKey = null)
where TDocument : IDocument<TKey>
{
return await MongoDbReader.GetSortedPaginatedAsync<TDocument, TKey>(filter, sortSelector, ascending, skipNumber, takeNumber, partitionKey);
}
/// <summary>
/// Asynchronously returns a paginated list of the documents matching the filter condition.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="sortDefinition">The sort definition.</param>
/// <param name="skipNumber">The number of documents you want to skip. Default value is 0.</param>
/// <param name="takeNumber">The number of documents you want to take. Default value is 50.</param>
/// <param name="partitionKey">An optional partition key.</param>
public virtual async Task<List<TDocument>> GetSortedPaginatedAsync<TDocument>(
Expression<Func<TDocument, bool>> filter,
SortDefinition<TDocument> sortDefinition,
int skipNumber = 0,
int takeNumber = 50,
string partitionKey = null)
where TDocument : IDocument<TKey>
{
return await MongoDbReader.GetSortedPaginatedAsync<TDocument, TKey>(filter, sortDefinition, skipNumber, takeNumber, partitionKey);
}
} }
} }
@@ -510,6 +510,73 @@ namespace MongoDbGenericRepository
#endregion Group By TKey #endregion Group By TKey
#region Pagination
/// <summary>
/// Asynchronously returns a paginated list of the documents matching the filter condition.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="sortSelector">The property selector.</param>
/// <param name="ascending">Order of the sorting.</param>
/// <param name="skipNumber">The number of documents you want to skip. Default value is 0.</param>
/// <param name="takeNumber">The number of documents you want to take. Default value is 50.</param>
/// <param name="partitionKey">An optional partition key.</param>
public virtual async Task<List<TDocument>> GetSortedPaginatedAsync<TDocument, TKey>(
Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, object>> sortSelector,
bool ascending = true,
int skipNumber = 0,
int takeNumber = 50,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var sorting = ascending
? Builders<TDocument>.Sort.Ascending(sortSelector)
: Builders<TDocument>.Sort.Descending(sortSelector);
return await HandlePartitioned<TDocument, TKey>(partitionKey)
.Find(filter)
.Sort(sorting)
.Skip(skipNumber)
.Limit(takeNumber)
.ToListAsync();
}
/// <summary>
/// Asynchronously returns a paginated list of the documents matching the filter condition.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="sortDefinition">The sort definition.</param>
/// <param name="skipNumber">The number of documents you want to skip. Default value is 0.</param>
/// <param name="takeNumber">The number of documents you want to take. Default value is 50.</param>
/// <param name="partitionKey">An optional partition key.</param>
public virtual async Task<List<TDocument>> GetSortedPaginatedAsync<TDocument, TKey>(
Expression<Func<TDocument, bool>> filter,
SortDefinition<TDocument> sortDefinition,
int skipNumber = 0,
int takeNumber = 50,
string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return await HandlePartitioned<TDocument, TKey>(partitionKey)
.Find(filter)
.Sort(sortDefinition)
.Skip(skipNumber)
.Limit(takeNumber)
.ToListAsync();
}
#endregion Pagination
/// <summary> /// <summary>
/// Gets a collections for a potentially partitioned document type. /// Gets a collections for a potentially partitioned document type.
/// </summary> /// </summary>