added cancellation tokens to 'Create' methods

This commit is contained in:
Sean Garrett
2023-06-18 18:08:50 +01:00
parent cae2b01dd9
commit 39e93dc968
4 changed files with 253 additions and 137 deletions
@@ -1,9 +1,9 @@
using MongoDbGenericRepository.DataAccess.Create; using System;
using MongoDbGenericRepository.Models;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MongoDbGenericRepository.DataAccess.Create;
using MongoDbGenericRepository.Models;
namespace MongoDbGenericRepository namespace MongoDbGenericRepository
{ {
@@ -23,7 +23,10 @@ namespace MongoDbGenericRepository
{ {
get get
{ {
if (_mongoDbCreator != null) { return _mongoDbCreator; } if (_mongoDbCreator != null)
{
return _mongoDbCreator;
}
lock (_initLock) lock (_initLock)
{ {
@@ -35,115 +38,127 @@ namespace MongoDbGenericRepository
return _mongoDbCreator; return _mongoDbCreator;
} }
set { _mongoDbCreator = value; } set => _mongoDbCreator = value;
} }
/// <summary> /// <inheritdoc />
/// Asynchronously adds a document to the collection. public virtual async Task AddOneAsync<TDocument, TKey>(TDocument document)
/// Populates the Id and AddedAtUtc fields if necessary. where TDocument : IDocument<TKey>
/// </summary> where TKey : IEquatable<TKey>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> {
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam> await AddOneAsync<TDocument, TKey>(document, CancellationToken.None);
/// <param name="document">The document you want to add.</param> }
/// <param name="cancellationToken">An optional cancellation Token.</param>
public virtual async Task AddOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default) /// <inheritdoc />
public virtual async Task AddOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
await MongoDbCreator.AddOneAsync<TDocument, TKey>(document, cancellationToken); await MongoDbCreator.AddOneAsync<TDocument, TKey>(document, cancellationToken);
} }
/// <summary> /// <inheritdoc />
/// Asynchronously adds a document to the collection. public virtual async Task AddOneAsync<TDocument>(TDocument document)
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="document">The document you want to add.</param>
/// <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, cancellationToken); await AddOneAsync<TDocument, Guid>(document, CancellationToken.None);
} }
/// <summary> /// <inheritdoc />
/// Adds a document to the collection. public virtual async Task AddOneAsync<TDocument>(TDocument document, CancellationToken cancellationToken)
/// Populates the Id and AddedAtUtc fields if necessary. where TDocument : IDocument<Guid>
/// </summary> {
/// <typeparam name="TDocument">The type representing a Document.</typeparam> await AddOneAsync<TDocument, Guid>(document, cancellationToken);
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam> }
/// <param name="document">The document you want to add.</param>
/// <inheritdoc />
public virtual void AddOne<TDocument, TKey>(TDocument document) public virtual void AddOne<TDocument, TKey>(TDocument document)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
MongoDbCreator.AddOne<TDocument, TKey>(document); AddOne<TDocument, TKey>(document, CancellationToken.None);
} }
/// <summary> /// <inheritdoc />
/// Adds a document to the collection. public virtual void AddOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken)
/// Populates the Id and AddedAtUtc fields if necessary. where TDocument : IDocument<TKey>
/// </summary> where TKey : IEquatable<TKey>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="document">The document you want to add.</param>
public virtual void AddOne<TDocument>(TDocument document) where TDocument : IDocument<Guid>
{ {
MongoDbCreator.AddOne<TDocument, Guid>(document); MongoDbCreator.AddOne<TDocument, TKey>(document, cancellationToken);
} }
/// <summary> /// <inheritdoc />
/// Asynchronously adds a list of documents to the collection. public virtual void AddOne<TDocument>(TDocument document)
/// Populates the Id and AddedAtUtc fields if necessary. where TDocument : IDocument<Guid>
/// </summary> {
/// <typeparam name="TDocument">The type representing a Document.</typeparam> AddOne<TDocument, Guid>(document, CancellationToken.None);
/// <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="cancellationToken">An optional cancellation Token.</param> /// <inheritdoc />
public virtual async Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken = default) public virtual void AddOne<TDocument>(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument<Guid>
{
AddOne<TDocument, Guid>(document, cancellationToken);
}
/// <inheritdoc />
public virtual async Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
await AddManyAsync<TDocument, TKey>(documents, CancellationToken.None);
}
/// <inheritdoc />
public virtual async Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
await MongoDbCreator.AddManyAsync<TDocument, TKey>(documents, cancellationToken); await MongoDbCreator.AddManyAsync<TDocument, TKey>(documents, cancellationToken);
} }
/// <summary> /// <inheritdoc />
/// Asynchronously adds a list of documents to the collection. public virtual async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents)
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="documents">The documents you want to add.</param>
/// <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, cancellationToken); await AddManyAsync<TDocument, Guid>(documents, CancellationToken.None);
} }
/// <summary> /// <inheritdoc />
/// Adds a list of documents to the collection. public virtual async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
/// Populates the Id and AddedAtUtc fields if necessary. where TDocument : IDocument<Guid>
/// </summary> {
/// <typeparam name="TDocument">The type representing a Document.</typeparam> await AddManyAsync<TDocument, Guid>(documents, cancellationToken);
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam> }
/// <param name="documents">The documents you want to add.</param>
/// <inheritdoc />
public virtual void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents) public virtual void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
MongoDbCreator.AddMany<TDocument, TKey>(documents); AddMany<TDocument, TKey>(documents, CancellationToken.None);
} }
/// <summary> /// <inheritdoc />
/// Adds a list of documents to the collection. public virtual void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
/// Populates the Id and AddedAtUtc fields if necessary. where TDocument : IDocument<TKey>
/// </summary> where TKey : IEquatable<TKey>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> {
/// <param name="documents">The documents you want to add.</param> MongoDbCreator.AddMany<TDocument, TKey>(documents, cancellationToken);
}
/// <inheritdoc />
public virtual void AddMany<TDocument>(IEnumerable<TDocument> documents) public virtual void AddMany<TDocument>(IEnumerable<TDocument> documents)
where TDocument : IDocument<Guid> where TDocument : IDocument<Guid>
{ {
MongoDbCreator.AddMany<TDocument, Guid>(documents); AddMany<TDocument, Guid>(documents, CancellationToken.None);
}
} }
/// <inheritdoc />
public virtual void AddMany<TDocument>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<Guid>
{
AddMany<TDocument, Guid>(documents, cancellationToken);
}
}
} }
@@ -12,6 +12,17 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
public interface IBaseMongoRepository_Create : IBaseMongoRepository_Create<Guid> public interface IBaseMongoRepository_Create : IBaseMongoRepository_Create<Guid>
{ {
/// <summary>
/// Asynchronously adds a document to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </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="document">The document you want to add.</param>
Task AddOneAsync<TDocument, TKey>(TDocument document)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary> /// <summary>
/// Asynchronously adds a document to the collection. /// Asynchronously adds a document to the collection.
/// Populates the Id and AddedAtUtc fields if necessary. /// Populates the Id and AddedAtUtc fields if necessary.
@@ -20,7 +31,7 @@ namespace MongoDbGenericRepository
/// <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>
/// <param name="cancellationToken">An optional cancellation Token.</param> /// <param name="cancellationToken">An optional cancellation Token.</param>
Task AddOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default) Task AddOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
@@ -35,6 +46,29 @@ namespace MongoDbGenericRepository
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
/// <summary>
/// Adds a document to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </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="document">The document you want to add.</param>
/// <param name="cancellationToken">The cancellation token.</param>
void AddOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Asynchronously adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </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="documents">The documents you want to add.</param>
Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary> /// <summary>
/// Asynchronously adds a list of documents to the collection. /// Asynchronously adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary. /// Populates the Id and AddedAtUtc fields if necessary.
@@ -43,7 +77,7 @@ namespace MongoDbGenericRepository
/// <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>
/// <param name="cancellationToken">An optional cancellation Token.</param> /// <param name="cancellationToken">An optional cancellation Token.</param>
Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken = default) Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
@@ -57,5 +91,17 @@ namespace MongoDbGenericRepository
void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents) void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
/// <summary>
/// Adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </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="documents">The documents you want to add.</param>
/// <param name="cancellationToken">The cancellation token</param>
void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
} }
} }
@@ -1,9 +1,9 @@
using MongoDbGenericRepository.DataAccess.Create; using System;
using MongoDbGenericRepository.Models;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MongoDbGenericRepository.DataAccess.Create;
using MongoDbGenericRepository.Models;
namespace MongoDbGenericRepository namespace MongoDbGenericRepository
{ {
@@ -33,56 +33,67 @@ namespace MongoDbGenericRepository
} }
} }
} }
return _mongoDbCreator; return _mongoDbCreator;
} }
set { _mongoDbCreator = value; } set => _mongoDbCreator = value;
} }
/// <summary> /// <inheritdoc />
/// Asynchronously adds a document to the collection. public virtual async Task AddOneAsync<TDocument>(TDocument document)
/// Populates the Id and AddedAtUtc fields if necessary. where TDocument : IDocument<TKey>
/// </summary> {
/// <typeparam name="TDocument">The type representing a Document.</typeparam> await AddOneAsync(document, CancellationToken.None);
/// <param name="document">The document you want to add.</param> }
/// <param name="cancellationToken">An optional cancellation Token.</param>
public virtual async Task AddOneAsync<TDocument>(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument<TKey> /// <inheritdoc />
public virtual async Task AddOneAsync<TDocument>(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
{ {
await MongoDbCreator.AddOneAsync<TDocument, TKey>(document, cancellationToken); await MongoDbCreator.AddOneAsync<TDocument, TKey>(document, cancellationToken);
} }
/// <summary> /// <inheritdoc />
/// Adds a document to the collection. public virtual void AddOne<TDocument>(TDocument document)
/// Populates the Id and AddedAtUtc fields if necessary. where TDocument : IDocument<TKey>
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="document">The document you want to add.</param>
public virtual void AddOne<TDocument>(TDocument document) where TDocument : IDocument<TKey>
{ {
MongoDbCreator.AddOne<TDocument, TKey>(document); AddOne(document, CancellationToken.None);
} }
/// <summary> /// <inheritdoc />
/// Asynchronously adds a list of documents to the collection. public virtual void AddOne<TDocument>(TDocument document, CancellationToken cancellationToken)
/// Populates the Id and AddedAtUtc fields if necessary. where TDocument : IDocument<TKey>
/// </summary> {
/// <typeparam name="TDocument">The type representing a Document.</typeparam> MongoDbCreator.AddOne<TDocument, TKey>(document, cancellationToken);
/// <param name="documents">The documents you want to add.</param> }
/// <param name="cancellationToken">An optional cancellation Token.</param>
public virtual async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents, CancellationToken cancellationToken = default) where TDocument : IDocument<TKey> /// <inheritdoc />
public virtual async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey>
{
await AddManyAsync(documents, CancellationToken.None);
}
/// <inheritdoc />
public virtual async Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
{ {
await MongoDbCreator.AddManyAsync<TDocument, TKey>(documents, cancellationToken); await MongoDbCreator.AddManyAsync<TDocument, TKey>(documents, cancellationToken);
} }
/// <summary> /// <inheritdoc />
/// Adds a list of documents to the collection. public virtual void AddMany<TDocument>(IEnumerable<TDocument> documents)
/// Populates the Id and AddedAtUtc fields if necessary. where TDocument : IDocument<TKey>
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="documents">The documents you want to add.</param>
public virtual void AddMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument<TKey>
{ {
MongoDbCreator.AddMany<TDocument, TKey>(documents); AddMany(documents, CancellationToken.None);
}
/// <inheritdoc />
public virtual void AddMany<TDocument>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
{
MongoDbCreator.AddMany<TDocument, TKey>(documents, cancellationToken);
} }
} }
} }
@@ -10,7 +10,8 @@ namespace MongoDbGenericRepository
/// The interface exposing data insertion functionality for Key typed repositories. /// The interface exposing data insertion functionality for Key typed repositories.
/// </summary> /// </summary>
/// <typeparam name="TKey"></typeparam> /// <typeparam name="TKey"></typeparam>
public interface IBaseMongoRepository_Create<TKey> where TKey : IEquatable<TKey> public interface IBaseMongoRepository_Create<TKey>
where TKey : IEquatable<TKey>
{ {
/// <summary> /// <summary>
/// Asynchronously adds a document to the collection. /// Asynchronously adds a document to the collection.
@@ -18,8 +19,18 @@ 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>
/// <param name="cancellationToken">An optional cancellation Token.</param> Task AddOneAsync<TDocument>(TDocument document)
Task AddOneAsync<TDocument>(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument<TKey>; where TDocument : IDocument<TKey>;
/// <summary>
/// Asynchronously adds a document to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="document">The document you want to add.</param>
/// <param name="cancellationToken">The cancellation Token.</param>
Task AddOneAsync<TDocument>(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>;
/// <summary> /// <summary>
/// Adds a document to the collection. /// Adds a document to the collection.
@@ -27,7 +38,27 @@ 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>
void AddOne<TDocument>(TDocument document) where TDocument : IDocument<TKey>; void AddOne<TDocument>(TDocument document)
where TDocument : IDocument<TKey>;
/// <summary>
/// Adds a document to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="document">The document you want to add.</param>
/// <param name="cancellationToken">The cancellation token.</param>
void AddOne<TDocument>(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>;
/// <summary>
/// Asynchronously adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="documents">The documents you want to add.</param>
Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey>;
/// <summary> /// <summary>
/// Asynchronously adds a list of documents to the collection. /// Asynchronously adds a list of documents to the collection.
@@ -36,7 +67,8 @@ namespace MongoDbGenericRepository
/// <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>
/// <param name="cancellationToken">An optional cancellation Token.</param> /// <param name="cancellationToken">An optional cancellation Token.</param>
Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents, CancellationToken cancellationToken = default) where TDocument : IDocument<TKey>; Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>;
/// <summary> /// <summary>
/// Adds a list of documents to the collection. /// Adds a list of documents to the collection.
@@ -44,6 +76,18 @@ 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>
void AddMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument<TKey>; void AddMany<TDocument>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey>;
/// <summary>
/// Adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="documents">The documents you want to add.</param>
/// <param name="cancellationToken">The cancellation token.</param>
void AddMany<TDocument>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>;
} }
} }