From 0cf6a7e5d38137f6f893e39c3f484b31929fb241 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Thu, 15 Jun 2023 21:35:51 +0100 Subject: [PATCH] added overloads for delete --- CoreUnitTests/CoreUnitTests.csproj | 4 +- CoreUnitTests/Infrastructure/TestDocument.cs | 6 + .../TestKeyedMongoRepository.cs | 6 + .../DeleteTests/DeleteOneAsyncTests.cs | 80 ++++++ .../BaseMongoRepository.Delete.cs | 266 +++++++++++------- .../DataAccess/Delete/IMongoDbEraser.cs | 13 +- .../DataAccess/Delete/MongoDbEraser.cs | 70 ++--- .../IBaseMongoRepository.Delete.cs | 101 ++++++- .../BaseMongoRepository.TKey.Delete.cs | 148 +++++----- .../IBaseMongoRepository.TKey.Delete.cs | 88 +++++- 10 files changed, 545 insertions(+), 237 deletions(-) create mode 100644 CoreUnitTests/Infrastructure/TestDocument.cs create mode 100644 CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs create mode 100644 CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs diff --git a/CoreUnitTests/CoreUnitTests.csproj b/CoreUnitTests/CoreUnitTests.csproj index 35cd8dc..ea5f539 100644 --- a/CoreUnitTests/CoreUnitTests.csproj +++ b/CoreUnitTests/CoreUnitTests.csproj @@ -8,8 +8,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/CoreUnitTests/Infrastructure/TestDocument.cs b/CoreUnitTests/Infrastructure/TestDocument.cs new file mode 100644 index 0000000..ca3f13f --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestDocument.cs @@ -0,0 +1,6 @@ +namespace CoreUnitTests.Infrastructure; + +public class TestDocument +{ + +} \ No newline at end of file diff --git a/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs b/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs new file mode 100644 index 0000000..569667f --- /dev/null +++ b/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs @@ -0,0 +1,6 @@ +namespace CoreUnitTests.Infrastructure; + +public class TestKeyedMongoRepository : KeyedMongoRepository +{ + +} \ No newline at end of file diff --git a/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs new file mode 100644 index 0000000..629bc99 --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/DeleteTests/DeleteOneAsyncTests.cs @@ -0,0 +1,80 @@ +using System.Threading; +using System.Threading.Tasks; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.DeleteTests; + +public class DeleteOneAsyncTests +{ + [Fact] + public async Task DeleteOneAsync_WithDocument_ShouldDeleteOne() + { + // Arrange + var repository = GetNewRepository(); + var document = new TestDocument {Name = "DeleteOneAsync_WithDocument_ShouldDeleteOne"}; + await repository.InsertOneAsync(document); + + // Act + var result = await repository.DeleteOneAsync(document); + + // Assert + result.Should().Be(1); + } + + [Fact] + public async Task DeleteOneAsync_WithDocumentAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var repository = GetNewRepository(); + var document = new TestDocument {Name = "DeleteOneAsync_WithDocumentAndCancellationToken_ShouldDeleteOne"}; + await repository.InsertOneAsync(document); + + // Act + var result = await repository.DeleteOneAsync(document, CancellationToken.None); + + // Assert + result.Should().Be(1); + } + + [Fact] + public async Task DeleteOneAsync_WithDocumentAndCancellationToken_ShouldDeleteOneWithCancellationToken() + { + // Arrange + var repository = GetNewRepository(); + var document = new TestDocument {Name = "DeleteOneAsync_WithDocumentAndCancellationToken_ShouldDeleteOneWithCancellationToken"}; + await repository.InsertOneAsync(document); + + // Act + var result = await repository.DeleteOneAsync(document, new CancellationToken(true)); + + // Assert + result.Should().Be(0); + } + + [Fact] + public async Task DeleteOneAsync_WithFilter_ShouldDeleteOne() + { + // Arrange + var repository = GetNewRepository(); + var document = new TestDocument {Name = "DeleteOneAsync_WithFilter_ShouldDeleteOne"}; + await repository.InsertOneAsync(document); + + // Act + var result = await repository.DeleteOneAsync(x => x.Name == document.Name); + + // Assert + result.Should().Be(1); + } + + [Fact] + public async Task DeleteOneAsync_WithFilterAndCancellationToken_ShouldDeleteOne() + { + // Arrange + var repository = GetNewRepository(); + var document = new TestDocument {Name = "DeleteOneAsync_WithFilterAndCancellationToken_ShouldDeleteOne"}; + await repository.InsertOneAsync(document); + + // Act + var result = await repository.DeleteOneAsync(x => x + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/BaseMongoRepository.Delete.cs b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs index 5974289..a7e3125 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Delete.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Delete.cs @@ -3,6 +3,7 @@ using MongoDbGenericRepository.Models; using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository @@ -35,94 +36,115 @@ namespace MongoDbGenericRepository #region Delete - /// - /// Asynchronously deletes a document. - /// - /// The type representing a Document. - /// The document you want to delete. - /// The number of documents deleted. - public virtual async Task DeleteOneAsync(TDocument document) where TDocument : IDocument + /// + public virtual async Task DeleteOneAsync(TDocument document) + where TDocument : IDocument { - return await MongoDbEraser.DeleteOneAsync(document); + return await DeleteOneAsync(document, CancellationToken.None); } - /// - /// Deletes a document. - /// - /// The type representing a Document. - /// The document you want to delete. - /// The number of documents deleted. - public virtual long DeleteOne(TDocument document) where TDocument : IDocument + /// + public virtual async Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbEraser.DeleteOneAsync(document, cancellationToken); + } + + /// + public virtual long DeleteOne(TDocument document) + where TDocument : IDocument { return MongoDbEraser.DeleteOne(document); } - /// - /// Deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual long DeleteOne(Expression> filter, string partitionKey = null) where TDocument : IDocument + /// + + public virtual long DeleteOne(Expression> filter, string partitionKey = null) + where TDocument : IDocument { return MongoDbEraser.DeleteOne(filter, partitionKey); } - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument + /// + public virtual async Task DeleteOneAsync(Expression> filter) + where TDocument : IDocument { - return await MongoDbEraser.DeleteOneAsync(filter, partitionKey); + return await DeleteOneAsync(filter, null, CancellationToken.None); } - /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument + /// + public virtual async Task DeleteOneAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument { - return await MongoDbEraser.DeleteManyAsync(filter, partitionKey); + return await DeleteOneAsync(filter, null, cancellationToken); } - /// - /// Asynchronously deletes a list of documents. - /// - /// The type representing a Document. - /// The list of documents to delete. - /// The number of documents deleted. - public virtual async Task DeleteManyAsync(IEnumerable documents) where TDocument : IDocument + /// + public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return await DeleteOneAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbEraser.DeleteOneAsync(filter, partitionKey, cancellationToken); + } + + /// + public async Task DeleteManyAsync(Expression> filter) + where TDocument : IDocument + { + return await DeleteManyAsync(filter, null, CancellationToken.None); + } + + /// + public async Task DeleteManyAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await DeleteManyAsync(filter, null, cancellationToken); + } + + /// + public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return await DeleteManyAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public async Task DeleteManyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbEraser.DeleteManyAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task DeleteManyAsync(IEnumerable documents) + where TDocument : IDocument { return await DeleteManyAsync(documents); } - /// - /// Deletes a list of documents. - /// - /// The type representing a Document. - /// The list of documents to delete. - /// The number of documents deleted. - public virtual long DeleteMany(IEnumerable documents) where TDocument : IDocument + /// + public virtual async Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await DeleteManyAsync(documents, cancellationToken); + } + + /// + public virtual long DeleteMany(IEnumerable documents) + where TDocument : IDocument { return DeleteMany(documents); } - /// - /// Deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual long DeleteMany(Expression> filter, string partitionKey = null) where TDocument : IDocument + /// + public virtual long DeleteMany(Expression> filter, string partitionKey = null) + where TDocument : IDocument { return MongoDbEraser.DeleteMany(filter, partitionKey); } @@ -131,13 +153,7 @@ namespace MongoDbGenericRepository #region Delete TKey - /// - /// Deletes a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to delete. - /// The number of documents deleted. + /// public virtual long DeleteOne(TDocument document) where TDocument : IDocument where TKey : IEquatable @@ -145,18 +161,20 @@ namespace MongoDbGenericRepository return MongoDbEraser.DeleteOne(document); } - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to delete. - /// The number of documents deleted. + /// public virtual async Task DeleteOneAsync(TDocument document) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbEraser.DeleteOneAsync(document); + return await DeleteOneAsync(document, CancellationToken.None); + } + + /// + public virtual async Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbEraser.DeleteOneAsync(document, cancellationToken); } /// @@ -174,48 +192,84 @@ namespace MongoDbGenericRepository return MongoDbEraser.DeleteOne(filter, partitionKey); } - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey = null) + /// + public virtual async Task DeleteOneAsync(Expression> filter) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbEraser.DeleteOneAsync(filter, partitionKey); + return await DeleteOneAsync(filter, null, CancellationToken.None); } - /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey = null) + /// + public virtual async Task DeleteOneAsync(Expression> filter, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbEraser.DeleteManyAsync(filter, partitionKey); + return await DeleteOneAsync(filter, null, cancellationToken); } - /// - /// Asynchronously deletes a list of documents. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The list of documents to delete. - /// The number of documents deleted. + /// + public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await DeleteOneAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbEraser.DeleteOneAsync(filter, partitionKey, cancellationToken); + } + + /// + public virtual async Task DeleteManyAsync(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable + { + return await DeleteManyAsync(filter, null, CancellationToken.None); + } + + /// + public virtual async Task DeleteManyAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await DeleteManyAsync(filter, null, cancellationToken); + } + + /// + public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await DeleteManyAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbEraser.DeleteManyAsync(filter, partitionKey, cancellationToken); + } + + /// public virtual async Task DeleteManyAsync(IEnumerable documents) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbEraser.DeleteManyAsync(documents); + return await DeleteManyAsync(documents, CancellationToken.None); + } + + /// + public virtual async Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbEraser.DeleteManyAsync(documents, cancellationToken); } /// diff --git a/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs b/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs index 61a3d9d..12ec088 100644 --- a/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs +++ b/MongoDbGenericRepository/DataAccess/Delete/IMongoDbEraser.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using MongoDbGenericRepository.DataAccess.Base; using MongoDbGenericRepository.Models; @@ -41,8 +42,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// The type representing a Document. /// The type of the primary key for a Document. /// The document you want to delete. + /// The cancellation token /// The number of documents deleted. - Task DeleteOneAsync(TDocument document) + Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -53,8 +55,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. + /// The cancellation token /// The number of documents deleted. - Task DeleteOneAsync(Expression> filter, string partitionKey = null) + Task DeleteOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -65,8 +68,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. + /// The cancellation token /// The number of documents deleted. - Task DeleteManyAsync(Expression> filter, string partitionKey = null) + Task DeleteManyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -76,8 +80,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete /// The type representing a Document. /// The type of the primary key for a Document. /// The list of documents to delete. + /// The cancellation token. /// The number of documents deleted. - Task DeleteManyAsync(IEnumerable documents) + Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; diff --git a/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs b/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs index 5cf6486..d3d0574 100644 --- a/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs +++ b/MongoDbGenericRepository/DataAccess/Delete/MongoDbEraser.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Delete @@ -22,13 +23,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete #region Delete TKey - /// - /// Deletes a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to delete. - /// The number of documents deleted. + /// public virtual long DeleteOne(TDocument document) where TDocument : IDocument where TKey : IEquatable @@ -37,29 +32,16 @@ namespace MongoDbGenericRepository.DataAccess.Delete return HandlePartitioned(document).DeleteOne(filter).DeletedCount; } - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to delete. - /// The number of documents deleted. - public virtual async Task DeleteOneAsync(TDocument document) + /// + public virtual async Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", document.Id); - return (await HandlePartitioned(document).DeleteOneAsync(filter)).DeletedCount; + return (await HandlePartitioned(document).DeleteOneAsync(filter, cancellationToken)).DeletedCount; } - /// - /// Deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. + /// public virtual long DeleteOne(Expression> filter, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable @@ -67,44 +49,24 @@ namespace MongoDbGenericRepository.DataAccess.Delete return HandlePartitioned(partitionKey).DeleteOne(filter).DeletedCount; } - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey = null) + /// + public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return (await HandlePartitioned(partitionKey).DeleteOneAsync(filter)).DeletedCount; + return (await HandlePartitioned(partitionKey).DeleteOneAsync(filter, cancellationToken)).DeletedCount; } - /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey = null) + /// + public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { return (await HandlePartitioned(partitionKey).DeleteManyAsync(filter)).DeletedCount; } - /// - /// Asynchronously deletes a list of documents. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The list of documents to delete. - /// The number of documents deleted. - public virtual async Task DeleteManyAsync(IEnumerable documents) + /// + public virtual async Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -122,13 +84,15 @@ namespace MongoDbGenericRepository.DataAccess.Delete foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument)e).PartitionKey)) { var groupIdsToDelete = group.Select(e => e.Id).ToArray(); - deleteCount += (await HandlePartitioned(group.FirstOrDefault()).DeleteManyAsync(x => groupIdsToDelete.Contains(x.Id))).DeletedCount; + deleteCount += (await HandlePartitioned(group.FirstOrDefault()) + .DeleteManyAsync(x => groupIdsToDelete.Contains(x.Id), cancellationToken: cancellationToken)) + .DeletedCount; } return deleteCount; } var idsToDelete = documentList.Select(e => e.Id).ToArray(); - return (await HandlePartitioned(documentList.FirstOrDefault()).DeleteManyAsync(x => idsToDelete.Contains(x.Id))).DeletedCount; + return (await HandlePartitioned(documentList.FirstOrDefault()).DeleteManyAsync(x => idsToDelete.Contains(x.Id), cancellationToken: cancellationToken)).DeletedCount; } /// diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs b/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs index eba3e72..fcfb229 100644 --- a/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs +++ b/MongoDbGenericRepository/IBaseMongoRepository.Delete.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using MongoDbGenericRepository.Models; @@ -33,6 +34,18 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable; + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to delete. + /// The Cancellation token + /// The number of documents deleted. + Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + /// /// Deletes a document matching the condition of the LINQ expression filter. /// @@ -45,6 +58,29 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable; + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The number of documents deleted. + Task DeleteOneAsync(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteOneAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + /// /// Asynchronously deletes a document matching the condition of the LINQ expression filter. /// @@ -53,7 +89,43 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. - Task DeleteOneAsync(Expression> filter, string partitionKey = null) + Task DeleteOneAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The number of documents deleted. + Task DeleteManyAsync(Expression> filter) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteManyAsync(Expression> filter, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -65,7 +137,20 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. - Task DeleteManyAsync(Expression> filter, string partitionKey = null) + Task DeleteManyAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteManyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -80,6 +165,18 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable; + /// + /// Asynchronously deletes a list of documents. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The list of documents to delete. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + /// /// Deletes a list of documents. /// diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs index 8d48668..d3047b9 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Delete.cs @@ -3,11 +3,12 @@ using MongoDbGenericRepository.Models; using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository { - public abstract partial class BaseMongoRepository: IBaseMongoRepository_Delete + public abstract partial class BaseMongoRepository : IBaseMongoRepository_Delete where TKey : IEquatable { private IMongoDbEraser _mongoDbEraser; @@ -33,100 +34,113 @@ namespace MongoDbGenericRepository set { _mongoDbEraser = value; } } - /// - /// Deletes a document. - /// - /// The type representing a Document. - /// The document you want to delete. - /// The number of documents deleted. + /// public virtual long DeleteOne(TDocument document) where TDocument : IDocument { return MongoDbEraser.DeleteOne(document); } - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// The document you want to delete. - /// The number of documents deleted. + /// public virtual async Task DeleteOneAsync(TDocument document) where TDocument : IDocument { - return await MongoDbEraser.DeleteOneAsync(document); + return await DeleteOneAsync(document, CancellationToken.None); } - /// - /// Deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. + /// + public virtual async Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbEraser.DeleteOneAsync(document, cancellationToken); + } + + /// + public virtual async Task DeleteOneAsync(Expression> filter) + where TDocument : IDocument + { + return await DeleteOneAsync(filter, null, CancellationToken.None); + } + + /// + public virtual async Task DeleteOneAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await DeleteOneAsync(filter, null, cancellationToken); + } + + /// + public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return await DeleteOneAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbEraser.DeleteOneAsync(filter, partitionKey, cancellationToken); + } + + /// public virtual long DeleteOne(Expression> filter, string partitionKey = null) where TDocument : IDocument { return MongoDbEraser.DeleteOne(filter, partitionKey); } - /// - /// Asynchronously deletes a document matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual async Task DeleteOneAsync(Expression> filter, string partitionKey = null) - where TDocument : IDocument - { - return await MongoDbEraser.DeleteOneAsync(filter, partitionKey); - } - - /// - /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. - public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey = null) - where TDocument : IDocument - { - return await MongoDbEraser.DeleteManyAsync(filter, partitionKey); - } - - /// - /// Asynchronously deletes a list of documents. - /// - /// The type representing a Document. - /// The list of documents to delete. - /// The number of documents deleted. + /// public virtual async Task DeleteManyAsync(IEnumerable documents) where TDocument : IDocument { - return await MongoDbEraser.DeleteManyAsync(documents); + return await DeleteManyAsync(documents, CancellationToken.None); } - /// - /// Deletes a list of documents. - /// - /// The type representing a Document. - /// The list of documents to delete. - /// The number of documents deleted. + /// + public virtual async Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbEraser.DeleteManyAsync(documents, cancellationToken); + } + + + /// + public async Task DeleteManyAsync(Expression> filter) + where TDocument : IDocument + { + return await DeleteManyAsync(filter, null, CancellationToken.None); + } + + /// + public async Task DeleteManyAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await DeleteManyAsync(filter, null, cancellationToken); + } + + /// + public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey) + where TDocument : IDocument + { + return await DeleteManyAsync(filter, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task DeleteManyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbEraser.DeleteManyAsync(filter, partitionKey, cancellationToken); + } + + /// public virtual long DeleteMany(IEnumerable documents) where TDocument : IDocument { return MongoDbEraser.DeleteMany(documents); } - /// - /// Deletes the documents matching the condition of the LINQ expression filter. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// An optional partition key. - /// The number of documents deleted. + /// public virtual long DeleteMany(Expression> filter, string partitionKey = null) where TDocument : IDocument { diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs index fabb293..42217fc 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Delete.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using MongoDbGenericRepository.Models; @@ -10,7 +11,8 @@ namespace MongoDbGenericRepository /// The interface exposing deletion functionality for Key typed repositories. /// /// The type of the document Id. - public interface IBaseMongoRepository_Delete where TKey : IEquatable + public interface IBaseMongoRepository_Delete + where TKey : IEquatable { /// /// Deletes a document. @@ -30,6 +32,16 @@ namespace MongoDbGenericRepository Task DeleteOneAsync(TDocument document) where TDocument : IDocument; + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The document you want to delete. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteOneAsync(TDocument document, CancellationToken cancellationToken) + where TDocument : IDocument; + /// /// Deletes a document matching the condition of the LINQ expression filter. /// @@ -40,6 +52,25 @@ namespace MongoDbGenericRepository long DeleteOne(Expression> filter, string partitionKey = null) where TDocument : IDocument; + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The number of documents deleted. + Task DeleteOneAsync(Expression> filter) + where TDocument : IDocument; + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteOneAsync(Expression> filter, CancellationToken cancellationToken) + where TDocument : IDocument; + /// /// Asynchronously deletes a document matching the condition of the LINQ expression filter. /// @@ -47,7 +78,37 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. - Task DeleteOneAsync(Expression> filter, string partitionKey = null) + Task DeleteOneAsync(Expression> filter, string partitionKey) + where TDocument : IDocument; + + /// + /// Asynchronously deletes a document matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteOneAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The number of documents deleted. + Task DeleteManyAsync(Expression> filter) + where TDocument : IDocument; + + /// + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteManyAsync(Expression> filter, CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -57,7 +118,18 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. - Task DeleteManyAsync(Expression> filter, string partitionKey = null) + Task DeleteManyAsync(Expression> filter, string partitionKey) + where TDocument : IDocument; + + /// + /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteManyAsync(Expression> filter, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -69,6 +141,16 @@ namespace MongoDbGenericRepository Task DeleteManyAsync(IEnumerable documents) where TDocument : IDocument; + /// + /// Asynchronously deletes a list of documents. + /// + /// The type representing a Document. + /// The list of documents to delete. + /// The cancellation token. + /// The number of documents deleted. + Task DeleteManyAsync(IEnumerable documents, CancellationToken cancellationToken) + where TDocument : IDocument; + /// /// Deletes a list of documents. ///