From 76a1aab671e4e2be346aefe4062f3b7879302ea2 Mon Sep 17 00:00:00 2001 From: Alexandre SPIESER Date: Mon, 15 Mar 2021 08:03:50 +0000 Subject: [PATCH] pushed cancellation token changes --- .../BaseMongoRepository.Create.cs | 35 +++++++++++++------ .../DataAccess/Base/DataAccessBase.cs | 8 +++++ .../DataAccess/Create/MongoDbCreator.cs | 20 ++++++++--- .../DataAccess/Read/MongoDbReader.Main.cs | 9 ++++- .../BaseMongoRepository.TKey.Create.cs | 15 +++++--- 5 files changed, 66 insertions(+), 21 deletions(-) diff --git a/MongoDbGenericRepository/BaseMongoRepository.Create.cs b/MongoDbGenericRepository/BaseMongoRepository.Create.cs index d2b319e..70d20e5 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Create.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Create.cs @@ -2,10 +2,15 @@ using MongoDbGenericRepository.Models; using System; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository { + /// + /// The IBaseMongoRepository_Create interface to expose document creation functionality + /// with document having an Id of type Guid. + /// public interface IBaseMongoRepository_Create : IBaseMongoRepository_Create { /// @@ -15,7 +20,8 @@ namespace MongoDbGenericRepository /// The type representing a Document. /// The type of the primary key for a Document. /// The document you want to add. - Task AddOneAsync(TDocument document) + /// An optional cancellation Token. + Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -37,7 +43,8 @@ namespace MongoDbGenericRepository /// The type representing a Document. /// The type of the primary key for a Document. /// The documents you want to add. - Task AddManyAsync(IEnumerable documents) + /// An optional cancellation Token. + Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -61,6 +68,10 @@ namespace MongoDbGenericRepository { private readonly object _initLock = new object(); private MongoDbCreator _mongoDbCreator; + + /// + /// The MongoDbCreator field. + /// protected virtual MongoDbCreator MongoDbCreator { get @@ -87,11 +98,12 @@ namespace MongoDbGenericRepository /// The type representing a Document. /// The type of the primary key for a Document. /// The document you want to add. - public virtual async Task AddOneAsync(TDocument document) + /// An optional cancellation Token. + public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - await MongoDbCreator.AddOneAsync(document); + await MongoDbCreator.AddOneAsync(document, cancellationToken); } /// @@ -100,10 +112,11 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The document you want to add. - public virtual async Task AddOneAsync(TDocument document) + /// An optional cancellation Token. + public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument { - await MongoDbCreator.AddOneAsync(document); + await MongoDbCreator.AddOneAsync(document, cancellationToken); } /// @@ -138,11 +151,12 @@ namespace MongoDbGenericRepository /// The type representing a Document. /// The type of the primary key for a Document. /// The documents you want to add. - public virtual async Task AddManyAsync(IEnumerable documents) + /// An optional cancellation Token. + public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - await MongoDbCreator.AddManyAsync(documents); + await MongoDbCreator.AddManyAsync(documents, cancellationToken); } /// @@ -151,10 +165,11 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The documents you want to add. - public virtual async Task AddManyAsync(IEnumerable documents) + /// An optional cancellation Token. + public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument { - await MongoDbCreator.AddManyAsync(documents); + await MongoDbCreator.AddManyAsync(documents, cancellationToken); } /// diff --git a/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs b/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs index c0d324f..eded8d5 100644 --- a/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs +++ b/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs @@ -28,6 +28,14 @@ namespace MongoDbGenericRepository.DataAccess.Base #region Utility Methods + /// + /// Gets a IMongoQueryable for a potentially partitioned document type and a filter. + /// + /// The document type. + /// The type of the primary key. + /// The filter definition. + /// The collection partition key. + /// public virtual IMongoQueryable GetQuery(Expression> filter, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable diff --git a/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs b/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs index bc490c7..7847030 100644 --- a/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs +++ b/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs @@ -6,12 +6,20 @@ using MongoDbGenericRepository.Utils; using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Create { + /// + /// A class to insert MongoDb document. + /// public class MongoDbCreator : DataAccessBase { + /// + /// The construct of the MongoDbCreator class. + /// + /// A instance. public MongoDbCreator(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } @@ -25,12 +33,13 @@ namespace MongoDbGenericRepository.DataAccess.Create /// The type representing a Document. /// The type of the primary key for a Document. /// The document you want to add. - public virtual async Task AddOneAsync(TDocument document) + /// An optional cancellation Token. + public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { FormatDocument(document); - await HandlePartitioned(document).InsertOneAsync(document); + await HandlePartitioned(document).InsertOneAsync(document, null, cancellationToken); } /// @@ -55,7 +64,8 @@ namespace MongoDbGenericRepository.DataAccess.Create /// The type representing a Document. /// The type of the primary key for a Document. /// The documents you want to add. - public virtual async Task AddManyAsync(IEnumerable documents) + /// An optional cancellation Token. + public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { @@ -72,12 +82,12 @@ namespace MongoDbGenericRepository.DataAccess.Create { foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey)) { - await HandlePartitioned(group.FirstOrDefault()).InsertManyAsync(group.ToList()); + await HandlePartitioned(group.FirstOrDefault()).InsertManyAsync(group.ToList(), null, cancellationToken); } } else { - await GetCollection().InsertManyAsync(documents.ToList()); + await GetCollection().InsertManyAsync(documents.ToList(), null, cancellationToken); } } diff --git a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs index ab6e54c..832b4ae 100644 --- a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs +++ b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs @@ -11,8 +11,15 @@ using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Read { + /// + /// A class to read MongoDb document. + /// public partial class MongoDbReader : DataAccessBase { + /// + /// The construct of the MongoDbReader class. + /// + /// A instance. public MongoDbReader(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } @@ -193,7 +200,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// The type of the primary key. /// A LINQ expression filter. /// A property selector to order by descending. - /// An optional partitionKey.An optional partitionKey. /// An optional cancellation Token. public async virtual Task GetByMaxAsync(Expression> filter, Expression> maxValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs index c445c2f..1a1b439 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs @@ -2,6 +2,7 @@ using MongoDbGenericRepository.Models; using System; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository @@ -18,7 +19,8 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The document you want to add. - Task AddOneAsync(TDocument document) where TDocument : IDocument; + /// An optional cancellation Token. + Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument; /// /// Adds a document to the collection. @@ -34,7 +36,8 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The documents you want to add. - Task AddManyAsync(IEnumerable documents) where TDocument : IDocument; + /// An optional cancellation Token. + Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument; /// /// Adds a list of documents to the collection. @@ -81,7 +84,8 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The document you want to add. - public virtual async Task AddOneAsync(TDocument document) where TDocument : IDocument + /// An optional cancellation Token. + public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument { await MongoDbCreator.AddOneAsync(document); } @@ -103,9 +107,10 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The documents you want to add. - public virtual async Task AddManyAsync(IEnumerable documents) where TDocument : IDocument + /// An optional cancellation Token. + public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument { - await MongoDbCreator.AddManyAsync(documents); + await MongoDbCreator.AddManyAsync(documents, cancellationToken); } ///