From 39e93dc968eea4605258fdb35b2747fc19752406 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sun, 18 Jun 2023 18:08:50 +0100 Subject: [PATCH] added cancellation tokens to 'Create' methods --- .../BaseMongoRepository.Create.cs | 171 ++++++++++-------- .../IBaseMongoRepository.Create.cs | 52 +++++- .../BaseMongoRepository.TKey.Create.cs | 93 +++++----- .../IBaseMongoRepository.TKey.Create.cs | 74 ++++++-- 4 files changed, 253 insertions(+), 137 deletions(-) diff --git a/MongoDbGenericRepository/BaseMongoRepository.Create.cs b/MongoDbGenericRepository/BaseMongoRepository.Create.cs index 53e37fa..5b2446f 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Create.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Create.cs @@ -1,15 +1,15 @@ -using MongoDbGenericRepository.DataAccess.Create; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. - /// Its constructor must be given a connection string and a database name. + /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. + /// Its constructor must be given a connection string and a database name. /// public abstract partial class BaseMongoRepository : IBaseMongoRepository_Create { @@ -17,13 +17,16 @@ namespace MongoDbGenericRepository private IMongoDbCreator _mongoDbCreator; /// - /// The MongoDbCreator field. + /// The MongoDbCreator field. /// protected virtual IMongoDbCreator MongoDbCreator { get { - if (_mongoDbCreator != null) { return _mongoDbCreator; } + if (_mongoDbCreator != null) + { + return _mongoDbCreator; + } lock (_initLock) { @@ -35,115 +38,127 @@ namespace MongoDbGenericRepository return _mongoDbCreator; } - set { _mongoDbCreator = value; } + set => _mongoDbCreator = value; } - /// - /// Asynchronously adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to add. - /// An optional cancellation Token. - public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) + /// + public virtual async Task AddOneAsync(TDocument document) + where TDocument : IDocument + where TKey : IEquatable + { + await AddOneAsync(document, CancellationToken.None); + } + + /// + public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { await MongoDbCreator.AddOneAsync(document, cancellationToken); } - /// - /// Asynchronously adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The document you want to add. - /// An optional cancellation Token. - public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) + /// + public virtual async Task AddOneAsync(TDocument document) where TDocument : IDocument { - await MongoDbCreator.AddOneAsync(document, cancellationToken); + await AddOneAsync(document, CancellationToken.None); } - /// - /// Adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// 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, CancellationToken cancellationToken) + where TDocument : IDocument + { + await AddOneAsync(document, cancellationToken); + } + + /// public virtual void AddOne(TDocument document) where TDocument : IDocument where TKey : IEquatable { - MongoDbCreator.AddOne(document); + AddOne(document, CancellationToken.None); } - /// - /// Adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The document you want to add. - public virtual void AddOne(TDocument document) where TDocument : IDocument + /// + public virtual void AddOne(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable { - MongoDbCreator.AddOne(document); + MongoDbCreator.AddOne(document, cancellationToken); } - /// - /// Asynchronously adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The documents you want to add. - /// An optional cancellation Token. - public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) + /// + public virtual void AddOne(TDocument document) + where TDocument : IDocument + { + AddOne(document, CancellationToken.None); + } + + /// + public virtual void AddOne(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + { + AddOne(document, cancellationToken); + } + + /// + public virtual async Task AddManyAsync(IEnumerable documents) + where TDocument : IDocument + where TKey : IEquatable + { + await AddManyAsync(documents, CancellationToken.None); + } + + /// + public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { await MongoDbCreator.AddManyAsync(documents, cancellationToken); } - /// - /// Asynchronously adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The documents you want to add. - /// An optional cancellation Token. - public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) + /// + public virtual async Task AddManyAsync(IEnumerable documents) where TDocument : IDocument { - await MongoDbCreator.AddManyAsync(documents, cancellationToken); + await AddManyAsync(documents, CancellationToken.None); } - /// - /// Adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// 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, CancellationToken cancellationToken) + where TDocument : IDocument + { + await AddManyAsync(documents, cancellationToken); + } + + /// public virtual void AddMany(IEnumerable documents) where TDocument : IDocument where TKey : IEquatable { - MongoDbCreator.AddMany(documents); + AddMany(documents, CancellationToken.None); } - /// - /// Adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The documents you want to add. - public virtual void AddMany(IEnumerable documents) + /// + public virtual void AddMany(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + MongoDbCreator.AddMany(documents, cancellationToken); + } + + /// + public virtual void AddMany(IEnumerable documents) where TDocument : IDocument { - MongoDbCreator.AddMany(documents); + AddMany(documents, CancellationToken.None); + } + + /// + public virtual void AddMany(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + { + AddMany(documents, cancellationToken); } } - -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Create.cs b/MongoDbGenericRepository/IBaseMongoRepository.Create.cs index 8dc50e3..1b7243d 100644 --- a/MongoDbGenericRepository/IBaseMongoRepository.Create.cs +++ b/MongoDbGenericRepository/IBaseMongoRepository.Create.cs @@ -7,11 +7,22 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The IBaseMongoRepository_Create interface to expose document creation functionality + /// The IBaseMongoRepository_Create interface to expose document creation functionality /// with document having an Id of type Guid. /// public interface IBaseMongoRepository_Create : IBaseMongoRepository_Create { + /// + /// Asynchronously adds a document to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to add. + Task AddOneAsync(TDocument document) + where TDocument : IDocument + where TKey : IEquatable; + /// /// Asynchronously adds a document to the collection. /// Populates the Id and AddedAtUtc fields if necessary. @@ -20,7 +31,7 @@ namespace MongoDbGenericRepository /// The type of the primary key for a Document. /// The document you want to add. /// An optional cancellation Token. - Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) + Task AddOneAsync(TDocument document, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -35,6 +46,29 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable; + /// + /// Adds a document to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to add. + /// The cancellation token. + void AddOne(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously adds a list of documents to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The documents you want to add. + Task AddManyAsync(IEnumerable documents) + where TDocument : IDocument + where TKey : IEquatable; + /// /// Asynchronously adds a list of documents to the collection. /// Populates the Id and AddedAtUtc fields if necessary. @@ -43,7 +77,7 @@ namespace MongoDbGenericRepository /// The type of the primary key for a Document. /// The documents you want to add. /// An optional cancellation Token. - Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) + Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -57,5 +91,17 @@ namespace MongoDbGenericRepository void AddMany(IEnumerable documents) where TDocument : IDocument where TKey : IEquatable; + + /// + /// Adds a list of documents to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The documents you want to add. + /// The cancellation token + void AddMany(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; } } \ No newline at end of file diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs index abd4833..c51dc35 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs @@ -1,23 +1,23 @@ -using MongoDbGenericRepository.DataAccess.Create; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using MongoDbGenericRepository.DataAccess.Create; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. - /// Its constructor must be given a connection string and a database name. + /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. + /// Its constructor must be given a connection string and a database name. /// - public abstract partial class BaseMongoRepository : IBaseMongoRepository_Create + public abstract partial class BaseMongoRepository : IBaseMongoRepository_Create where TKey : IEquatable { private IMongoDbCreator _mongoDbCreator; /// - /// The MongoDb accessor to insert data. + /// The MongoDb accessor to insert data. /// protected virtual IMongoDbCreator MongoDbCreator { @@ -33,56 +33,67 @@ namespace MongoDbGenericRepository } } } + return _mongoDbCreator; } - set { _mongoDbCreator = value; } + set => _mongoDbCreator = value; } - /// - /// Asynchronously adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The document you want to add. - /// An optional cancellation Token. - public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument + /// + public virtual async Task AddOneAsync(TDocument document) + where TDocument : IDocument + { + await AddOneAsync(document, CancellationToken.None); + } + + /// + public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument { await MongoDbCreator.AddOneAsync(document, cancellationToken); } - /// - /// Adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The document you want to add. - public virtual void AddOne(TDocument document) where TDocument : IDocument + /// + public virtual void AddOne(TDocument document) + where TDocument : IDocument { - MongoDbCreator.AddOne(document); + AddOne(document, CancellationToken.None); } - /// - /// Asynchronously adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The documents you want to add. - /// An optional cancellation Token. - public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument + /// + public virtual void AddOne(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + { + MongoDbCreator.AddOne(document, cancellationToken); + } + + /// + public virtual async Task AddManyAsync(IEnumerable documents) + where TDocument : IDocument + { + await AddManyAsync(documents, CancellationToken.None); + } + + /// + public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument { await MongoDbCreator.AddManyAsync(documents, cancellationToken); } - /// - /// Adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. - /// - /// The type representing a Document. - /// The documents you want to add. - public virtual void AddMany(IEnumerable documents) where TDocument : IDocument + /// + public virtual void AddMany(IEnumerable documents) + where TDocument : IDocument { - MongoDbCreator.AddMany(documents); + AddMany(documents, CancellationToken.None); + } + + /// + public virtual void AddMany(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + { + MongoDbCreator.AddMany(documents, cancellationToken); } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Create.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Create.cs index 8994fde..bce8e62 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Create.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Create.cs @@ -7,43 +7,87 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The interface exposing data insertion functionality for Key typed repositories. + /// The interface exposing data insertion functionality for Key typed repositories. /// /// - public interface IBaseMongoRepository_Create where TKey : IEquatable + public interface IBaseMongoRepository_Create + where TKey : IEquatable { /// - /// Asynchronously adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. + /// Asynchronously adds a document to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. /// /// The type representing a Document. /// The document you want to add. - /// An optional cancellation Token. - Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument; + Task AddOneAsync(TDocument document) + where TDocument : IDocument; /// - /// Adds a document to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. + /// Asynchronously adds a document to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. /// /// The type representing a Document. /// The document you want to add. - void AddOne(TDocument document) where TDocument : IDocument; + /// The cancellation Token. + Task AddOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument; /// - /// Asynchronously adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. + /// Adds a document to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The document you want to add. + void AddOne(TDocument document) + where TDocument : IDocument; + + /// + /// Adds a document to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The document you want to add. + /// The cancellation token. + void AddOne(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Asynchronously adds a list of documents to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The documents you want to add. + Task AddManyAsync(IEnumerable documents) + where TDocument : IDocument; + + /// + /// Asynchronously adds a list of documents to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. /// /// The type representing a Document. /// The documents you want to add. /// An optional cancellation Token. - Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument; + Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument; /// - /// Adds a list of documents to the collection. - /// Populates the Id and AddedAtUtc fields if necessary. + /// Adds a list of documents to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. /// /// The type representing a Document. /// The documents you want to add. - void AddMany(IEnumerable documents) where TDocument : IDocument; + void AddMany(IEnumerable documents) + where TDocument : IDocument; + + + /// + /// Adds a list of documents to the collection. + /// Populates the Id and AddedAtUtc fields if necessary. + /// + /// The type representing a Document. + /// The documents you want to add. + /// The cancellation token. + void AddMany(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument; } } \ No newline at end of file