Merge pull request #19 from alexandre-spieser/alex/refactorForKeyTypeRepo

Alex/refactor for key type repo
This commit is contained in:
Alexandre SPIESER
2019-04-14 22:48:59 +01:00
committed by GitHub
12 changed files with 446 additions and 64 deletions
@@ -1,10 +1,13 @@
using CoreIntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDB.Bson;
using MongoDbGenericRepository.Attributes; using MongoDbGenericRepository.Attributes;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using System; using System;
namespace CoreIntegrationTests namespace CoreIntegrationTests
{ {
#region Guid Type
[CollectionName("TestingCNameAttrPartTKey")] [CollectionName("TestingCNameAttrPartTKey")]
public class CoreTKeyPartitionedCollectionNameDoc : TestDoc<Guid>, IPartitionedDocument public class CoreTKeyPartitionedCollectionNameDoc : TestDoc<Guid>, IPartitionedDocument
{ {
@@ -28,4 +31,35 @@ namespace CoreIntegrationTests
return "CoreCRUDTKeyPartitionedCollectionNameAttributeTests"; return "CoreCRUDTKeyPartitionedCollectionNameAttributeTests";
} }
} }
#endregion Guid Type
#region ObjectId Type
[CollectionName("TestingCNameAttrPartObjectId")]
public class CoreObjectIdPartitionedCollectionNameDoc : TestDoc<ObjectId>, IPartitionedDocument
{
public CoreObjectIdPartitionedCollectionNameDoc()
{
PartitionKey = "CoreTestPartitionKeyObjectId";
}
public string PartitionKey { get; set; }
}
public class CRUDObjectIdPartitionedCollectionNameAttributeTests : MongoDbTKeyDocumentTestBase<CoreObjectIdPartitionedCollectionNameDoc, ObjectId>
{
public CRUDObjectIdPartitionedCollectionNameAttributeTests(MongoDbTestFixture<CoreObjectIdPartitionedCollectionNameDoc, ObjectId> fixture) : base(fixture)
{
}
public override string GetClassName()
{
return "CoreCRUDTKeyPartitionedCollectionNameAttributeTests";
}
}
#endregion ObjectId Type
} }
-1
View File
@@ -11,7 +11,6 @@ namespace CoreIntegrationTests
{ {
public CRUDTests(MongoDbTestFixture<CoreTestDocument, Guid> fixture) : base(fixture) public CRUDTests(MongoDbTestFixture<CoreTestDocument, Guid> fixture) : base(fixture)
{ {
} }
public override string GetClassName() public override string GetClassName()
@@ -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";
@@ -1127,6 +1128,174 @@ namespace CoreIntegrationTests.Infrastructure
#endregion Math #endregion Math
#region Group By
[Fact]
public void GroupByTProjection()
{
// Arrange
var documents = CreateTestDocuments(5);
var content = GetContent();
for (var i = 0; i < documents.Count - 2; i++)
{
documents[i].GroupingKey = 1;
documents[i].SomeContent = $"{content}-{i}";
}
for (var i = 3; i < documents.Count; i++)
{
documents[i].GroupingKey = 2;
documents[i].SomeContent = $"{content}-{i}";
}
SUT.AddMany<T, TKey>(documents);
// Act
var result = SUT.GroupBy<T, int, ProjectedGroup, TKey>(
e => e.GroupingKey,
g => new ProjectedGroup
{
Key = g.Key,
Content = g.Select(doc => doc.SomeContent).ToList()
},
PartitionKey);
// Assert
var key1Group = result.First(e => e.Key == 1);
Assert.NotNull(key1Group);
Assert.Equal(3, key1Group.Content.Count);
var key2Group = result.First(e => e.Key == 2);
Assert.NotNull(key2Group);
Assert.Equal(2, key2Group.Content.Count);
}
[Fact]
public void FilteredGroupByTProjection()
{
// Arrange
var documents = CreateTestDocuments(5);
var content = GetContent();
for (var i = 0; i < documents.Count - 2; i++)
{
documents[i].GroupingKey = 4;
documents[i].SomeContent = $"{content}-{i}";
}
for (var i = 3; i < documents.Count; i++)
{
documents[i].GroupingKey = 5;
documents[i].SomeContent = $"{content}-{i}";
}
var guid1 = Guid.NewGuid().ToString("n");
var guid2 = Guid.NewGuid().ToString("n");
for (var i = 0; i < documents.Count - 1; i++)
{
documents[i].Children = new List<Child> {
new Child(guid1, guid2)
};
}
SUT.AddMany<T, TKey>(documents);
// Act
var result = SUT.GroupBy<T, int, ProjectedGroup, TKey>(
e => e.Children.Any(c => c.Type == guid1),
e => e.GroupingKey,
g => new ProjectedGroup
{
Key = g.Key,
Content = g.Select(doc => doc.SomeContent).ToList()
},
PartitionKey);
// Assert
var key1Group = result.First(e => e.Key == 4);
Assert.NotNull(key1Group);
Assert.Equal(3, key1Group.Content.Count);
var key2Group = result.First(e => e.Key == 5);
Assert.NotNull(key2Group);
Assert.Single(key2Group.Content);
}
#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)]
@@ -5,32 +5,29 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace CoreIntegrationTests.Infrastructure namespace CoreIntegrationTests.Infrastructure
{ {
public class MongoDbTestFixture<T, TKey> : IDisposable public class MongoDbTestFixture<T, TKey> : IDisposable
where T : IDocument<TKey>, new() where T : IDocument<TKey>, new()
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
public IMongoDbContext Context; public IMongoDbContext Context;
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()
{ {
var docIds = DocsToDelete.ToList().Select(e => e.Id);
if (docIds.Any()) if (DocsToDelete.Any())
{ {
TestRepository.Instance.DeleteMany<T, TKey>(e => docIds.Contains(e.Id)); TestRepository.Instance.DeleteMany<T, TKey>(DocsToDelete.ToList());
} }
} }
@@ -46,8 +43,9 @@ namespace CoreIntegrationTests.Infrastructure
var docs = new List<T>(); var docs = new List<T>();
for (var i = 0; i < numberOfDocumentsToCreate; i++) for (var i = 0; i < numberOfDocumentsToCreate; i++)
{ {
docs.Add(new T()); var doc = new T();
DocsToDelete.Add(docs.Last()); docs.Add(doc);
DocsToDelete.Add(doc);
} }
return docs; return docs;
} }
@@ -82,6 +82,7 @@ namespace CoreIntegrationTests.Infrastructure
Children = new List<Child>(); Children = new List<Child>();
} }
public int GroupingKey { get; set; }
public string SomeContent { get; set; } public string SomeContent { get; set; }
public string SomeContent4 { get; set; } public string SomeContent4 { get; set; }
public string SomeContent5 { get; set; } public string SomeContent5 { get; set; }
@@ -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
{ {
@@ -3,7 +3,6 @@ using MongoDbGenericRepository.Models;
using MongoDbGenericRepository.Utils; using MongoDbGenericRepository.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -40,39 +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>