fixed various warnings

This commit is contained in:
Sean Garrett
2023-06-11 20:23:44 +01:00
parent 14e261261c
commit 02b9385fd8
30 changed files with 245 additions and 655 deletions
@@ -2,13 +2,14 @@ using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using MongoDbGenericRepository.DataAccess.Base;
using MongoDbGenericRepository.Models;
namespace MongoDbGenericRepository.DataAccess.Delete
{
/// <summary>
/// The MongoDbEraser interface. used to delete documents from the collections.
/// </summary>
public interface IMongoDbEraser : IDataAccessBase
{
/// <summary>
@@ -102,50 +103,5 @@ namespace MongoDbGenericRepository.DataAccess.Delete
long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Gets a IMongoQueryable for a potentially partitioned document type and a filter.
/// </summary>
/// <typeparam name="TDocument">The document type.</typeparam>
/// <typeparam name="TKey">The type of the primary key.</typeparam>
/// <param name="filter">The filter definition.</param>
/// <param name="partitionKey">The collection partition key.</param>
/// <returns></returns>
IMongoQueryable<TDocument> GetQuery<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Gets a collections for a potentially partitioned document type.
/// </summary>
/// <typeparam name="TDocument">The document type.</typeparam>
/// <typeparam name="TKey">The type of the primary key.</typeparam>
/// <param name="document">The document.</param>
/// <returns></returns>
IMongoCollection<TDocument> HandlePartitioned<TDocument, TKey>(TDocument document)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Gets a collections for a potentially partitioned document type.
/// </summary>
/// <typeparam name="TDocument">The document type.</typeparam>
/// <typeparam name="TKey">The type of the primary key.</typeparam>
/// <param name="partitionKey">The collection partition key.</param>
/// <returns></returns>
IMongoCollection<TDocument> HandlePartitioned<TDocument, TKey>(string partitionKey)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Gets a collections for the type TDocument with a partition key.
/// </summary>
/// <typeparam name="TDocument">The document type.</typeparam>
/// <typeparam name="TKey">The type of the primary key.</typeparam>
/// <param name="partitionKey">The collection partition key.</param>
/// <returns></returns>
IMongoCollection<TDocument> GetCollection<TDocument, TKey>(string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
}
}
@@ -1,5 +1,4 @@
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using MongoDbGenericRepository.DataAccess.Base;
using MongoDbGenericRepository.Models;
using System;
@@ -10,8 +9,13 @@ using System.Threading.Tasks;
namespace MongoDbGenericRepository.DataAccess.Delete
{
/// <inheritdoc cref="MongoDbGenericRepository.DataAccess.Delete.IMongoDbEraser" />
public class MongoDbEraser : DataAccessBase, IMongoDbEraser
{
/// <summary>
/// The MongoDbEraser constructor.
/// </summary>
/// <param name="mongoDbContext">the MongoDb Context</param>
public MongoDbEraser(IMongoDbContext mongoDbContext) : base(mongoDbContext)
{
}
@@ -104,26 +108,27 @@ namespace MongoDbGenericRepository.DataAccess.Delete
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
if (!documents.Any())
var documentList = documents.ToList();
if (!documentList.Any())
{
return 0;
}
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
if (documents.Any(e => e is IPartitionedDocument))
if (documentList.Any(e => e is IPartitionedDocument))
{
long deleteCount = 0;
foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
{
var groupIdsTodelete = group.Select(e => e.Id).ToArray();
deleteCount += (await HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).DeleteManyAsync(x => groupIdsTodelete.Contains(x.Id))).DeletedCount;
var groupIdsToDelete = group.Select(e => e.Id).ToArray();
deleteCount += (await HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).DeleteManyAsync(x => groupIdsToDelete.Contains(x.Id))).DeletedCount;
}
return deleteCount;
}
else
{
var idsTodelete = documents.Select(e => e.Id).ToArray();
return (await HandlePartitioned<TDocument, TKey>(documents.FirstOrDefault()).DeleteManyAsync(x => idsTodelete.Contains(x.Id))).DeletedCount;
}
var idsToDelete = documentList.Select(e => e.Id).ToArray();
return (await HandlePartitioned<TDocument, TKey>(documentList.FirstOrDefault()).DeleteManyAsync(x => idsToDelete.Contains(x.Id))).DeletedCount;
}
/// <summary>
@@ -137,26 +142,27 @@ namespace MongoDbGenericRepository.DataAccess.Delete
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
if (!documents.Any())
var documentList = documents.ToList();
if (!documentList.Any())
{
return 0;
}
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
if (documents.Any(e => e is IPartitionedDocument))
if (documentList.Any(e => e is IPartitionedDocument))
{
long deleteCount = 0;
foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
{
var groupIdsTodelete = group.Select(e => e.Id).ToArray();
deleteCount += (HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).DeleteMany(x => groupIdsTodelete.Contains(x.Id))).DeletedCount;
var groupIdsToDelete = group.Select(e => e.Id).ToArray();
deleteCount += (HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).DeleteMany(x => groupIdsToDelete.Contains(x.Id))).DeletedCount;
}
return deleteCount;
}
else
{
var idsTodelete = documents.Select(e => e.Id).ToArray();
return (HandlePartitioned<TDocument, TKey>(documents.FirstOrDefault()).DeleteMany(x => idsTodelete.Contains(x.Id))).DeletedCount;
}
var idsToDelete = documentList.Select(e => e.Id).ToArray();
return HandlePartitioned<TDocument, TKey>(documentList.FirstOrDefault()).DeleteMany(x => idsToDelete.Contains(x.Id)).DeletedCount;
}
/// <summary>