added overloads for delete

This commit is contained in:
Sean Garrett
2023-06-15 21:35:51 +01:00
parent e23f659ffe
commit 0cf6a7e5d3
10 changed files with 545 additions and 237 deletions
+2 -2
View File
@@ -8,8 +8,8 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0"/> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1"/> <PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
@@ -0,0 +1,6 @@
namespace CoreUnitTests.Infrastructure;
public class TestDocument
{
}
@@ -0,0 +1,6 @@
namespace CoreUnitTests.Infrastructure;
public class TestKeyedMongoRepository : KeyedMongoRepository<TestDocument, TestDocumentKey>
{
}
@@ -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<TKey> {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<TKey> {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<TKey> {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<TKey> {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<TKey> {Name = "DeleteOneAsync_WithFilterAndCancellationToken_ShouldDeleteOne"};
await repository.InsertOneAsync(document);
// Act
var result = await repository.DeleteOneAsync(x => x
}
}
@@ -3,6 +3,7 @@ using MongoDbGenericRepository.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MongoDbGenericRepository namespace MongoDbGenericRepository
@@ -35,94 +36,115 @@ namespace MongoDbGenericRepository
#region Delete #region Delete
/// <summary> /// <inheritdoc />
/// Asynchronously deletes a document. public virtual async Task<long> DeleteOneAsync<TDocument>(TDocument document)
/// </summary> where TDocument : IDocument<Guid>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="document">The document you want to delete.</param>
/// <returns>The number of documents deleted.</returns>
public virtual async Task<long> DeleteOneAsync<TDocument>(TDocument document) where TDocument : IDocument<Guid>
{ {
return await MongoDbEraser.DeleteOneAsync<TDocument, Guid>(document); return await DeleteOneAsync(document, CancellationToken.None);
} }
/// <summary> /// <inheritdoc />
/// Deletes a document. public virtual async Task<long> DeleteOneAsync<TDocument>(TDocument document, CancellationToken cancellationToken)
/// </summary> where TDocument : IDocument<Guid>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> {
/// <param name="document">The document you want to delete.</param> return await MongoDbEraser.DeleteOneAsync<TDocument, Guid>(document, cancellationToken);
/// <returns>The number of documents deleted.</returns> }
public virtual long DeleteOne<TDocument>(TDocument document) where TDocument : IDocument<Guid>
/// <inheritdoc />
public virtual long DeleteOne<TDocument>(TDocument document)
where TDocument : IDocument<Guid>
{ {
return MongoDbEraser.DeleteOne<TDocument, Guid>(document); return MongoDbEraser.DeleteOne<TDocument, Guid>(document);
} }
/// <summary> /// <inheritdoc />
/// Deletes a document matching the condition of the LINQ expression filter.
/// </summary> public virtual long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
/// <typeparam name="TDocument">The type representing a Document.</typeparam> where TDocument : IDocument<Guid>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns>
public virtual long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<Guid>
{ {
return MongoDbEraser.DeleteOne<TDocument, Guid>(filter, partitionKey); return MongoDbEraser.DeleteOne<TDocument, Guid>(filter, partitionKey);
} }
/// <summary> /// <inheritdoc />
/// Asynchronously deletes a document matching the condition of the LINQ expression filter. public virtual async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter)
/// </summary> where TDocument : IDocument<Guid>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns>
public virtual async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<Guid>
{ {
return await MongoDbEraser.DeleteOneAsync<TDocument, Guid>(filter, partitionKey); return await DeleteOneAsync(filter, null, CancellationToken.None);
} }
/// <summary> /// <inheritdoc />
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter. public virtual async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken)
/// </summary> where TDocument : IDocument<Guid>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns>
public virtual async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<Guid>
{ {
return await MongoDbEraser.DeleteManyAsync<TDocument, Guid>(filter, partitionKey); return await DeleteOneAsync(filter, null, cancellationToken);
} }
/// <summary> /// <inheritdoc />
/// Asynchronously deletes a list of documents. public virtual async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey)
/// </summary> where TDocument : IDocument<Guid>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> {
/// <param name="documents">The list of documents to delete.</param> return await DeleteOneAsync(filter, partitionKey, CancellationToken.None);
/// <returns>The number of documents deleted.</returns> }
public virtual async Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument<Guid>
/// <inheritdoc />
public virtual async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<Guid>
{
return await MongoDbEraser.DeleteOneAsync<TDocument, Guid>(filter, partitionKey, cancellationToken);
}
/// <inheritdoc />
public async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter)
where TDocument : IDocument<Guid>
{
return await DeleteManyAsync(filter, null, CancellationToken.None);
}
/// <inheritdoc />
public async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken)
where TDocument : IDocument<Guid>
{
return await DeleteManyAsync(filter, null, cancellationToken);
}
/// <inheritdoc />
public virtual async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey)
where TDocument : IDocument<Guid>
{
return await DeleteManyAsync(filter, partitionKey, CancellationToken.None);
}
/// <inheritdoc />
public async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<Guid>
{
return await MongoDbEraser.DeleteManyAsync<TDocument, Guid>(filter, partitionKey, CancellationToken.None);
}
/// <inheritdoc />
public virtual async Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents)
where TDocument : IDocument<Guid>
{ {
return await DeleteManyAsync<TDocument, Guid>(documents); return await DeleteManyAsync<TDocument, Guid>(documents);
} }
/// <summary> /// <inheritdoc />
/// Deletes a list of documents. public virtual async Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
/// </summary> where TDocument : IDocument<Guid>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> {
/// <param name="documents">The list of documents to delete.</param> return await DeleteManyAsync<TDocument, Guid>(documents, cancellationToken);
/// <returns>The number of documents deleted.</returns> }
public virtual long DeleteMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument<Guid>
/// <inheritdoc />
public virtual long DeleteMany<TDocument>(IEnumerable<TDocument> documents)
where TDocument : IDocument<Guid>
{ {
return DeleteMany<TDocument, Guid>(documents); return DeleteMany<TDocument, Guid>(documents);
} }
/// <summary> /// <inheritdoc />
/// Deletes the documents matching the condition of the LINQ expression filter. public virtual long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
/// </summary> where TDocument : IDocument<Guid>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns>
public virtual long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument<Guid>
{ {
return MongoDbEraser.DeleteMany<TDocument, Guid>(filter, partitionKey); return MongoDbEraser.DeleteMany<TDocument, Guid>(filter, partitionKey);
} }
@@ -131,13 +153,7 @@ namespace MongoDbGenericRepository
#region Delete TKey #region Delete TKey
/// <summary> /// <inheritdoc />
/// Deletes a document.
/// </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 delete.</param>
/// <returns>The number of documents deleted.</returns>
public virtual long DeleteOne<TDocument, TKey>(TDocument document) public virtual long DeleteOne<TDocument, TKey>(TDocument document)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
@@ -145,18 +161,20 @@ namespace MongoDbGenericRepository
return MongoDbEraser.DeleteOne<TDocument, TKey>(document); return MongoDbEraser.DeleteOne<TDocument, TKey>(document);
} }
/// <summary> /// <inheritdoc />
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
/// </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 delete.</param>
/// <returns>The number of documents deleted.</returns>
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document) public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
return await MongoDbEraser.DeleteOneAsync<TDocument, TKey>(document); return await DeleteOneAsync<TDocument, TKey>(document, CancellationToken.None);
}
/// <inheritdoc />
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return await MongoDbEraser.DeleteOneAsync<TDocument, TKey>(document, cancellationToken);
} }
/// <summary> /// <summary>
@@ -174,48 +192,84 @@ namespace MongoDbGenericRepository
return MongoDbEraser.DeleteOne<TDocument, TKey>(filter, partitionKey); return MongoDbEraser.DeleteOne<TDocument, TKey>(filter, partitionKey);
} }
/// <summary> /// <inheritdoc />
/// Asynchronously deletes a document matching the condition of the LINQ expression filter. public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter)
/// </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="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns>
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
return await MongoDbEraser.DeleteOneAsync<TDocument, TKey>(filter, partitionKey); return await DeleteOneAsync<TDocument, TKey>(filter, null, CancellationToken.None);
} }
/// <summary> /// <inheritdoc />
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter. public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken)
/// </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="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns>
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
return await MongoDbEraser.DeleteManyAsync<TDocument, TKey>(filter, partitionKey); return await DeleteOneAsync<TDocument, TKey>(filter, null, cancellationToken);
} }
/// <summary> /// <inheritdoc />
/// Asynchronously deletes a list of documents. public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey)
/// </summary> where TDocument : IDocument<TKey>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> where TKey : IEquatable<TKey>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam> {
/// <param name="documents">The list of documents to delete.</param> return await DeleteOneAsync<TDocument, TKey>(filter, partitionKey, CancellationToken.None);
/// <returns>The number of documents deleted.</returns> }
/// <inheritdoc />
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return await MongoDbEraser.DeleteOneAsync<TDocument, TKey>(filter, partitionKey, cancellationToken);
}
/// <inheritdoc />
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return await DeleteManyAsync<TDocument, TKey>(filter, null, CancellationToken.None);
}
/// <inheritdoc />
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return await DeleteManyAsync<TDocument, TKey>(filter, null, cancellationToken);
}
/// <inheritdoc />
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return await DeleteManyAsync<TDocument, TKey>(filter, partitionKey, CancellationToken.None);
}
/// <inheritdoc />
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return await MongoDbEraser.DeleteManyAsync<TDocument, TKey>(filter, partitionKey, cancellationToken);
}
/// <inheritdoc />
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents) public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
return await MongoDbEraser.DeleteManyAsync<TDocument, TKey>(documents); return await DeleteManyAsync<TDocument, TKey>(documents, CancellationToken.None);
}
/// <inheritdoc />
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return await MongoDbEraser.DeleteManyAsync<TDocument, TKey>(documents, cancellationToken);
} }
/// <summary> /// <summary>
@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MongoDbGenericRepository.DataAccess.Base; using MongoDbGenericRepository.DataAccess.Base;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
@@ -41,8 +42,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam> /// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="document">The document you want to delete.</param> /// <param name="document">The document you want to delete.</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document) Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
@@ -53,8 +55,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete
/// <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="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
@@ -65,8 +68,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete
/// <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="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
@@ -76,8 +80,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam> /// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="documents">The list of documents to delete.</param> /// <param name="documents">The list of documents to delete.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents) Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
@@ -5,6 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MongoDbGenericRepository.DataAccess.Delete namespace MongoDbGenericRepository.DataAccess.Delete
@@ -22,13 +23,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
#region Delete TKey #region Delete TKey
/// <summary> /// <inheritdoc />
/// Deletes a document.
/// </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 delete.</param>
/// <returns>The number of documents deleted.</returns>
public virtual long DeleteOne<TDocument, TKey>(TDocument document) public virtual long DeleteOne<TDocument, TKey>(TDocument document)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
@@ -37,29 +32,16 @@ namespace MongoDbGenericRepository.DataAccess.Delete
return HandlePartitioned<TDocument, TKey>(document).DeleteOne(filter).DeletedCount; return HandlePartitioned<TDocument, TKey>(document).DeleteOne(filter).DeletedCount;
} }
/// <summary> /// <inheritdoc />
/// Asynchronously deletes a document matching the condition of the LINQ expression filter. public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken)
/// </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 delete.</param>
/// <returns>The number of documents deleted.</returns>
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
var filter = Builders<TDocument>.Filter.Eq("Id", document.Id); var filter = Builders<TDocument>.Filter.Eq("Id", document.Id);
return (await HandlePartitioned<TDocument, TKey>(document).DeleteOneAsync(filter)).DeletedCount; return (await HandlePartitioned<TDocument, TKey>(document).DeleteOneAsync(filter, cancellationToken)).DeletedCount;
} }
/// <summary> /// <inheritdoc />
/// Deletes a document matching the condition of the LINQ expression filter.
/// </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="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns>
public virtual long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) public virtual long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
@@ -67,44 +49,24 @@ namespace MongoDbGenericRepository.DataAccess.Delete
return HandlePartitioned<TDocument, TKey>(partitionKey).DeleteOne(filter).DeletedCount; return HandlePartitioned<TDocument, TKey>(partitionKey).DeleteOne(filter).DeletedCount;
} }
/// <summary> /// <inheritdoc />
/// Asynchronously deletes a document matching the condition of the LINQ expression filter. public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
/// </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="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns>
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
return (await HandlePartitioned<TDocument, TKey>(partitionKey).DeleteOneAsync(filter)).DeletedCount; return (await HandlePartitioned<TDocument, TKey>(partitionKey).DeleteOneAsync(filter, cancellationToken)).DeletedCount;
} }
/// <summary> /// <inheritdoc />
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter. public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
/// </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="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns>
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
return (await HandlePartitioned<TDocument, TKey>(partitionKey).DeleteManyAsync(filter)).DeletedCount; return (await HandlePartitioned<TDocument, TKey>(partitionKey).DeleteManyAsync(filter)).DeletedCount;
} }
/// <summary> /// <inheritdoc />
/// Asynchronously deletes a list of documents. public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
/// </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 list of documents to delete.</param>
/// <returns>The number of documents deleted.</returns>
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -122,13 +84,15 @@ namespace MongoDbGenericRepository.DataAccess.Delete
foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument)e).PartitionKey)) foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
{ {
var groupIdsToDelete = group.Select(e => e.Id).ToArray(); var groupIdsToDelete = group.Select(e => e.Id).ToArray();
deleteCount += (await HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).DeleteManyAsync(x => groupIdsToDelete.Contains(x.Id))).DeletedCount; deleteCount += (await HandlePartitioned<TDocument, TKey>(group.FirstOrDefault())
.DeleteManyAsync(x => groupIdsToDelete.Contains(x.Id), cancellationToken: cancellationToken))
.DeletedCount;
} }
return deleteCount; return deleteCount;
} }
var idsToDelete = documentList.Select(e => e.Id).ToArray(); var idsToDelete = documentList.Select(e => e.Id).ToArray();
return (await HandlePartitioned<TDocument, TKey>(documentList.FirstOrDefault()).DeleteManyAsync(x => idsToDelete.Contains(x.Id))).DeletedCount; return (await HandlePartitioned<TDocument, TKey>(documentList.FirstOrDefault()).DeleteManyAsync(x => idsToDelete.Contains(x.Id), cancellationToken: cancellationToken)).DeletedCount;
} }
/// <summary> /// <summary>
@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
@@ -33,6 +34,18 @@ namespace MongoDbGenericRepository
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
/// <summary>
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
/// </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 delete.</param>
/// <param name="cancellationToken">The Cancellation token</param>
/// <returns>The number of documents deleted.</returns>
Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary> /// <summary>
/// Deletes a document matching the condition of the LINQ expression filter. /// Deletes a document matching the condition of the LINQ expression filter.
/// </summary> /// </summary>
@@ -45,6 +58,29 @@ namespace MongoDbGenericRepository
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
/// <summary>
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
/// </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="filter">A LINQ expression filter.</param>
/// <returns>The number of documents deleted.</returns>
Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
/// </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="filter">A LINQ expression filter.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The number of documents deleted.</returns>
Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary> /// <summary>
/// Asynchronously deletes a document matching the condition of the LINQ expression filter. /// Asynchronously deletes a document matching the condition of the LINQ expression filter.
/// </summary> /// </summary>
@@ -53,7 +89,43 @@ namespace MongoDbGenericRepository
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
/// </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="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The number of documents deleted.</returns>
Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
/// </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="filter">A LINQ expression filter.</param>
/// <returns>The number of documents deleted.</returns>
Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
/// </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="filter">A LINQ expression filter.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The number of documents deleted.</returns>
Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
@@ -65,7 +137,20 @@ namespace MongoDbGenericRepository
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
/// </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="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The number of documents deleted.</returns>
Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
@@ -80,6 +165,18 @@ namespace MongoDbGenericRepository
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
/// <summary>
/// Asynchronously deletes a list of documents.
/// </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 list of documents to delete.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The number of documents deleted.</returns>
Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary> /// <summary>
/// Deletes a list of documents. /// Deletes a list of documents.
/// </summary> /// </summary>
@@ -3,11 +3,12 @@ using MongoDbGenericRepository.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MongoDbGenericRepository namespace MongoDbGenericRepository
{ {
public abstract partial class BaseMongoRepository<TKey>: IBaseMongoRepository_Delete<TKey> public abstract partial class BaseMongoRepository<TKey> : IBaseMongoRepository_Delete<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
private IMongoDbEraser _mongoDbEraser; private IMongoDbEraser _mongoDbEraser;
@@ -33,100 +34,113 @@ namespace MongoDbGenericRepository
set { _mongoDbEraser = value; } set { _mongoDbEraser = value; }
} }
/// <summary> /// <inheritdoc />
/// Deletes a document.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="document">The document you want to delete.</param>
/// <returns>The number of documents deleted.</returns>
public virtual long DeleteOne<TDocument>(TDocument document) public virtual long DeleteOne<TDocument>(TDocument document)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
{ {
return MongoDbEraser.DeleteOne<TDocument, TKey>(document); return MongoDbEraser.DeleteOne<TDocument, TKey>(document);
} }
/// <summary> /// <inheritdoc />
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="document">The document you want to delete.</param>
/// <returns>The number of documents deleted.</returns>
public virtual async Task<long> DeleteOneAsync<TDocument>(TDocument document) public virtual async Task<long> DeleteOneAsync<TDocument>(TDocument document)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
{ {
return await MongoDbEraser.DeleteOneAsync<TDocument, TKey>(document); return await DeleteOneAsync(document, CancellationToken.None);
} }
/// <summary> /// <inheritdoc />
/// Deletes a document matching the condition of the LINQ expression filter. public virtual async Task<long> DeleteOneAsync<TDocument>(TDocument document, CancellationToken cancellationToken)
/// </summary> where TDocument : IDocument<TKey>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> {
/// <param name="filter">A LINQ expression filter.</param> return await MongoDbEraser.DeleteOneAsync<TDocument, TKey>(document, cancellationToken);
/// <param name="partitionKey">An optional partition key.</param> }
/// <returns>The number of documents deleted.</returns>
/// <inheritdoc />
public virtual async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter)
where TDocument : IDocument<TKey>
{
return await DeleteOneAsync(filter, null, CancellationToken.None);
}
/// <inheritdoc />
public virtual async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
{
return await DeleteOneAsync(filter, null, cancellationToken);
}
/// <inheritdoc />
public virtual async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey)
where TDocument : IDocument<TKey>
{
return await DeleteOneAsync(filter, partitionKey, CancellationToken.None);
}
/// <inheritdoc />
public virtual async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
{
return await MongoDbEraser.DeleteOneAsync<TDocument, TKey>(filter, partitionKey, cancellationToken);
}
/// <inheritdoc />
public virtual long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) public virtual long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
{ {
return MongoDbEraser.DeleteOne<TDocument, TKey>(filter, partitionKey); return MongoDbEraser.DeleteOne<TDocument, TKey>(filter, partitionKey);
} }
/// <summary> /// <inheritdoc />
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns>
public virtual async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
{
return await MongoDbEraser.DeleteOneAsync<TDocument, TKey>(filter, partitionKey);
}
/// <summary>
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns>
public virtual async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
{
return await MongoDbEraser.DeleteManyAsync<TDocument, TKey>(filter, partitionKey);
}
/// <summary>
/// Asynchronously deletes a list of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="documents">The list of documents to delete.</param>
/// <returns>The number of documents deleted.</returns>
public virtual async Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents) public virtual async Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
{ {
return await MongoDbEraser.DeleteManyAsync<TDocument, TKey>(documents); return await DeleteManyAsync(documents, CancellationToken.None);
} }
/// <summary> /// <inheritdoc />
/// Deletes a list of documents. public virtual async Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
/// </summary> where TDocument : IDocument<TKey>
/// <typeparam name="TDocument">The type representing a Document.</typeparam> {
/// <param name="documents">The list of documents to delete.</param> return await MongoDbEraser.DeleteManyAsync<TDocument, TKey>(documents, cancellationToken);
/// <returns>The number of documents deleted.</returns> }
/// <inheritdoc />
public async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter)
where TDocument : IDocument<TKey>
{
return await DeleteManyAsync(filter, null, CancellationToken.None);
}
/// <inheritdoc />
public async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
{
return await DeleteManyAsync(filter, null, cancellationToken);
}
/// <inheritdoc />
public virtual async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey)
where TDocument : IDocument<TKey>
{
return await DeleteManyAsync(filter, partitionKey, CancellationToken.None);
}
/// <inheritdoc />
public virtual async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
{
return await MongoDbEraser.DeleteManyAsync<TDocument, TKey>(filter, partitionKey, cancellationToken);
}
/// <inheritdoc />
public virtual long DeleteMany<TDocument>(IEnumerable<TDocument> documents) public virtual long DeleteMany<TDocument>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
{ {
return MongoDbEraser.DeleteMany<TDocument, TKey>(documents); return MongoDbEraser.DeleteMany<TDocument, TKey>(documents);
} }
/// <summary> /// <inheritdoc />
/// Deletes the documents matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns>
public virtual long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) public virtual long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
{ {
@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
@@ -10,7 +11,8 @@ namespace MongoDbGenericRepository
/// The interface exposing deletion functionality for Key typed repositories. /// The interface exposing deletion functionality for Key typed repositories.
/// </summary> /// </summary>
/// <typeparam name="TKey">The type of the document Id.</typeparam> /// <typeparam name="TKey">The type of the document Id.</typeparam>
public interface IBaseMongoRepository_Delete<TKey> where TKey : IEquatable<TKey> public interface IBaseMongoRepository_Delete<TKey>
where TKey : IEquatable<TKey>
{ {
/// <summary> /// <summary>
/// Deletes a document. /// Deletes a document.
@@ -30,6 +32,16 @@ namespace MongoDbGenericRepository
Task<long> DeleteOneAsync<TDocument>(TDocument document) Task<long> DeleteOneAsync<TDocument>(TDocument document)
where TDocument : IDocument<TKey>; where TDocument : IDocument<TKey>;
/// <summary>
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="document">The document you want to delete.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The number of documents deleted.</returns>
Task<long> DeleteOneAsync<TDocument>(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>;
/// <summary> /// <summary>
/// Deletes a document matching the condition of the LINQ expression filter. /// Deletes a document matching the condition of the LINQ expression filter.
/// </summary> /// </summary>
@@ -40,6 +52,25 @@ namespace MongoDbGenericRepository
long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>; where TDocument : IDocument<TKey>;
/// <summary>
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <returns>The number of documents deleted.</returns>
Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter)
where TDocument : IDocument<TKey>;
/// <summary>
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The number of documents deleted.</returns>
Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>;
/// <summary> /// <summary>
/// Asynchronously deletes a document matching the condition of the LINQ expression filter. /// Asynchronously deletes a document matching the condition of the LINQ expression filter.
/// </summary> /// </summary>
@@ -47,7 +78,37 @@ namespace MongoDbGenericRepository
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey)
where TDocument : IDocument<TKey>;
/// <summary>
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The number of documents deleted.</returns>
Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>;
/// <summary>
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <returns>The number of documents deleted.</returns>
Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter)
where TDocument : IDocument<TKey>;
/// <summary>
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The number of documents deleted.</returns>
Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>; where TDocument : IDocument<TKey>;
/// <summary> /// <summary>
@@ -57,7 +118,18 @@ namespace MongoDbGenericRepository
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey)
where TDocument : IDocument<TKey>;
/// <summary>
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The number of documents deleted.</returns>
Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>; where TDocument : IDocument<TKey>;
/// <summary> /// <summary>
@@ -69,6 +141,16 @@ namespace MongoDbGenericRepository
Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents) Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey>; where TDocument : IDocument<TKey>;
/// <summary>
/// Asynchronously deletes a list of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="documents">The list of documents to delete.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The number of documents deleted.</returns>
Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>;
/// <summary> /// <summary>
/// Deletes a list of documents. /// Deletes a list of documents.
/// </summary> /// </summary>