pushed cancellation token changes

This commit is contained in:
Alexandre SPIESER
2021-03-15 08:03:50 +00:00
parent eebdc89575
commit 76a1aab671
5 changed files with 66 additions and 21 deletions
@@ -2,10 +2,15 @@
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MongoDbGenericRepository namespace MongoDbGenericRepository
{ {
/// <summary>
/// The IBaseMongoRepository_Create interface to expose document creation functionality
/// with document having an Id of type Guid.
/// </summary>
public interface IBaseMongoRepository_Create : IBaseMongoRepository_Create<Guid> public interface IBaseMongoRepository_Create : IBaseMongoRepository_Create<Guid>
{ {
/// <summary> /// <summary>
@@ -15,7 +20,8 @@ namespace MongoDbGenericRepository
/// <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="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="document">The document you want to add.</param> /// <param name="document">The document you want to add.</param>
Task AddOneAsync<TDocument, TKey>(TDocument document) /// <param name="cancellationToken">An optional cancellation Token.</param>
Task AddOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
@@ -37,7 +43,8 @@ namespace MongoDbGenericRepository
/// <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="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="documents">The documents you want to add.</param> /// <param name="documents">The documents you want to add.</param>
Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents) /// <param name="cancellationToken">An optional cancellation Token.</param>
Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
@@ -61,6 +68,10 @@ namespace MongoDbGenericRepository
{ {
private readonly object _initLock = new object(); private readonly object _initLock = new object();
private MongoDbCreator _mongoDbCreator; private MongoDbCreator _mongoDbCreator;
/// <summary>
/// The MongoDbCreator field.
/// </summary>
protected virtual MongoDbCreator MongoDbCreator protected virtual MongoDbCreator MongoDbCreator
{ {
get get
@@ -87,11 +98,12 @@ namespace MongoDbGenericRepository
/// <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="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="document">The document you want to add.</param> /// <param name="document">The document you want to add.</param>
public virtual async Task AddOneAsync<TDocument, TKey>(TDocument document) /// <param name="cancellationToken">An optional cancellation Token.</param>
public virtual async Task AddOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
await MongoDbCreator.AddOneAsync<TDocument, TKey>(document); await MongoDbCreator.AddOneAsync<TDocument, TKey>(document, cancellationToken);
} }
/// <summary> /// <summary>
@@ -100,10 +112,11 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="document">The document you want to add.</param> /// <param name="document">The document you want to add.</param>
public virtual async Task AddOneAsync<TDocument>(TDocument document) /// <param name="cancellationToken">An optional cancellation Token.</param>
public virtual async Task AddOneAsync<TDocument>(TDocument document, CancellationToken cancellationToken = default)
where TDocument : IDocument<Guid> where TDocument : IDocument<Guid>
{ {
await MongoDbCreator.AddOneAsync<TDocument, Guid>(document); await MongoDbCreator.AddOneAsync<TDocument, Guid>(document, cancellationToken);
} }
/// <summary> /// <summary>
@@ -138,11 +151,12 @@ namespace MongoDbGenericRepository
/// <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="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="documents">The documents you want to add.</param> /// <param name="documents">The documents you want to add.</param>
public virtual async Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents) /// <param name="cancellationToken">An optional cancellation Token.</param>
public virtual async Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
await MongoDbCreator.AddManyAsync<TDocument, TKey>(documents); await MongoDbCreator.AddManyAsync<TDocument, TKey>(documents, cancellationToken);
} }
/// <summary> /// <summary>
@@ -151,10 +165,11 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="documents">The documents you want to add.</param> /// <param name="documents">The documents you want to add.</param>
public virtual async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) /// <param name="cancellationToken">An optional cancellation Token.</param>
public virtual async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents, CancellationToken cancellationToken = default)
where TDocument : IDocument<Guid> where TDocument : IDocument<Guid>
{ {
await MongoDbCreator.AddManyAsync<TDocument, Guid>(documents); await MongoDbCreator.AddManyAsync<TDocument, Guid>(documents, cancellationToken);
} }
/// <summary> /// <summary>
@@ -28,6 +28,14 @@ namespace MongoDbGenericRepository.DataAccess.Base
#region Utility Methods #region Utility Methods
/// <summary>
/// Gets a IMongoQueryable for a potentially partitioned document type and a filter.
/// </summary>
/// <typeparam name="TDocument">The document type.</typeparam>
/// <typeparam name="TKey">The type of the primary key.</typeparam>
/// <param name="filter">The filter definition.</param>
/// <param name="partitionKey">The collection partition key.</param>
/// <returns></returns>
public virtual IMongoQueryable<TDocument> GetQuery<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) public virtual IMongoQueryable<TDocument> GetQuery<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
@@ -6,12 +6,20 @@ using MongoDbGenericRepository.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MongoDbGenericRepository.DataAccess.Create namespace MongoDbGenericRepository.DataAccess.Create
{ {
/// <summary>
/// A class to insert MongoDb document.
/// </summary>
public class MongoDbCreator : DataAccessBase public class MongoDbCreator : DataAccessBase
{ {
/// <summary>
/// The construct of the MongoDbCreator class.
/// </summary>
/// <param name="mongoDbContext">A <see cref="IMongoDbContext"/> instance.</param>
public MongoDbCreator(IMongoDbContext mongoDbContext) : base(mongoDbContext) public MongoDbCreator(IMongoDbContext mongoDbContext) : base(mongoDbContext)
{ {
} }
@@ -25,12 +33,13 @@ namespace MongoDbGenericRepository.DataAccess.Create
/// <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="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="document">The document you want to add.</param> /// <param name="document">The document you want to add.</param>
public virtual async Task AddOneAsync<TDocument, TKey>(TDocument document) /// <param name="cancellationToken">An optional cancellation Token.</param>
public virtual async Task AddOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
FormatDocument<TDocument, TKey>(document); FormatDocument<TDocument, TKey>(document);
await HandlePartitioned<TDocument, TKey>(document).InsertOneAsync(document); await HandlePartitioned<TDocument, TKey>(document).InsertOneAsync(document, null, cancellationToken);
} }
/// <summary> /// <summary>
@@ -55,7 +64,8 @@ namespace MongoDbGenericRepository.DataAccess.Create
/// <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="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="documents">The documents you want to add.</param> /// <param name="documents">The documents you want to add.</param>
public virtual async Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents) /// <param name="cancellationToken">An optional cancellation Token.</param>
public virtual async Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -72,12 +82,12 @@ namespace MongoDbGenericRepository.DataAccess.Create
{ {
foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey)) foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
{ {
await HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).InsertManyAsync(group.ToList()); await HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).InsertManyAsync(group.ToList(), null, cancellationToken);
} }
} }
else else
{ {
await GetCollection<TDocument, TKey>().InsertManyAsync(documents.ToList()); await GetCollection<TDocument, TKey>().InsertManyAsync(documents.ToList(), null, cancellationToken);
} }
} }
@@ -11,8 +11,15 @@ using System.Threading.Tasks;
namespace MongoDbGenericRepository.DataAccess.Read namespace MongoDbGenericRepository.DataAccess.Read
{ {
/// <summary>
/// A class to read MongoDb document.
/// </summary>
public partial class MongoDbReader : DataAccessBase public partial class MongoDbReader : DataAccessBase
{ {
/// <summary>
/// The construct of the MongoDbReader class.
/// </summary>
/// <param name="mongoDbContext">A <see cref="IMongoDbContext"/> instance.</param>
public MongoDbReader(IMongoDbContext mongoDbContext) : base(mongoDbContext) public MongoDbReader(IMongoDbContext mongoDbContext) : base(mongoDbContext)
{ {
} }
@@ -193,7 +200,7 @@ namespace MongoDbGenericRepository.DataAccess.Read
/// <typeparam name="TKey">The type of the primary key.</typeparam> /// <typeparam name="TKey">The type of the primary key.</typeparam>
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="maxValueSelector">A property selector to order by descending.</param> /// <param name="maxValueSelector">A property selector to order by descending.</param>
/// <param name="partitionKey">An optional partitionKey.</param /// <param name="partitionKey">An optional partitionKey.</param>
/// <param name="cancellationToken">An optional cancellation Token.</param> /// <param name="cancellationToken">An optional cancellation Token.</param>
public async virtual Task<TDocument> GetByMaxAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) public async virtual Task<TDocument> GetByMaxAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, object>> maxValueSelector, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
@@ -2,6 +2,7 @@
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MongoDbGenericRepository namespace MongoDbGenericRepository
@@ -18,7 +19,8 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="document">The document you want to add.</param> /// <param name="document">The document you want to add.</param>
Task AddOneAsync<TDocument>(TDocument document) where TDocument : IDocument<TKey>; /// <param name="cancellationToken">An optional cancellation Token.</param>
Task AddOneAsync<TDocument>(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument<TKey>;
/// <summary> /// <summary>
/// Adds a document to the collection. /// Adds a document to the collection.
@@ -34,7 +36,8 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="documents">The documents you want to add.</param> /// <param name="documents">The documents you want to add.</param>
Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument<TKey>; /// <param name="cancellationToken">An optional cancellation Token.</param>
Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents, CancellationToken cancellationToken = default) where TDocument : IDocument<TKey>;
/// <summary> /// <summary>
/// Adds a list of documents to the collection. /// Adds a list of documents to the collection.
@@ -81,7 +84,8 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="document">The document you want to add.</param> /// <param name="document">The document you want to add.</param>
public virtual async Task AddOneAsync<TDocument>(TDocument document) where TDocument : IDocument<TKey> /// <param name="cancellationToken">An optional cancellation Token.</param>
public virtual async Task AddOneAsync<TDocument>(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument<TKey>
{ {
await MongoDbCreator.AddOneAsync<TDocument, TKey>(document); await MongoDbCreator.AddOneAsync<TDocument, TKey>(document);
} }
@@ -103,9 +107,10 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="documents">The documents you want to add.</param> /// <param name="documents">The documents you want to add.</param>
public virtual async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument<TKey> /// <param name="cancellationToken">An optional cancellation Token.</param>
public virtual async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents, CancellationToken cancellationToken = default) where TDocument : IDocument<TKey>
{ {
await MongoDbCreator.AddManyAsync<TDocument, TKey>(documents); await MongoDbCreator.AddManyAsync<TDocument, TKey>(documents, cancellationToken);
} }
/// <summary> /// <summary>