From 658c7539897df671ff5b7a665493f6bf7a611911 Mon Sep 17 00:00:00 2001 From: Alexandre SPIESER Date: Wed, 10 Mar 2021 08:44:50 +0000 Subject: [PATCH 1/5] Added cancellation tokens (ongoing) --- .../CoreIntegrationTests.csproj | 4 +- .../IBaseMongoRepository_Update.cs | 3 + ...aseMongoRepository_Update_ClientSession.cs | 67 +++++++------ .../Abstractions/IBaseReadOnlyRepository.cs | 13 ++- ...aseMongoRepository.Update.ClientSession.cs | 20 ++-- .../DataAccess/Base/DataAccessBase.cs | 11 ++- .../DataAccess/Read/MongoDbReader.Main.cs | 98 ++++++++++++++----- .../Models/IndexCreationOptions.cs | 4 - .../MongoDbGenericRepository.csproj | 21 ++-- .../ReadOnlyMongoRepository.cs | 21 ++-- 10 files changed, 161 insertions(+), 101 deletions(-) diff --git a/CoreIntegrationTests/CoreIntegrationTests.csproj b/CoreIntegrationTests/CoreIntegrationTests.csproj index 69f6379..b89f770 100644 --- a/CoreIntegrationTests/CoreIntegrationTests.csproj +++ b/CoreIntegrationTests/CoreIntegrationTests.csproj @@ -7,8 +7,8 @@ - - + + all diff --git a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update.cs b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update.cs index d30bc5c..86ec936 100644 --- a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update.cs +++ b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update.cs @@ -6,6 +6,9 @@ using System.Threading.Tasks; namespace MongoDbGenericRepository { + /// + /// The IBaseMongoRepository_Update interface exposing update functionality for documents with Guid Ids. + /// public interface IBaseMongoRepository_Update : IBaseMongoRepository_Update { /// diff --git a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs index 8ed2e1e..9e9223b 100644 --- a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs +++ b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs @@ -7,6 +7,9 @@ using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository.DataAccess.Update { + /// + /// The IBaseMongoRepository_Update_ClientSession interface exposing update functionality with a IClientSessionHandle. + /// public interface IBaseMongoRepository_Update_ClientSession { /// @@ -14,10 +17,14 @@ namespace MongoDbGenericRepository.DataAccess.Update /// /// The type representing a Document. /// The type of the primary key for a Document. + /// The type of the field to update. /// The client session. - /// The document with the modifications you want to persist. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// The optional partition key. /// The optional cancellation token. - /// + /// A boolean value indicating success. bool UpdateOne(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable; @@ -27,10 +34,14 @@ namespace MongoDbGenericRepository.DataAccess.Update /// /// The type representing a Document. /// The type of the primary key for a Document. + /// The type of the field to update. /// The client session. - /// The document with the modifications you want to persist. + /// The filter for the update. + /// The field to update. + /// The value of the field. + /// The optional partition key. /// The optional cancellation token. - /// + /// A boolean value indicating success. bool UpdateOne(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable; @@ -40,11 +51,13 @@ namespace MongoDbGenericRepository.DataAccess.Update /// /// The type representing a Document. /// The type of the primary key for a Document. + /// The type of the field to update. /// The client session. /// The document to modify. - /// The update definition. + /// The field to update. + /// The value of the field. /// The optional cancellation token. - /// + /// A boolean value indicating success. bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable; @@ -55,10 +68,9 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The type representing a Document. /// The type of the primary key for a Document. /// The client session. - /// The document to modify. - /// The update definition. + /// The document with the modifications you want to persist. /// The optional cancellation token. - /// + /// A boolean value indicating success. bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable; @@ -68,13 +80,11 @@ namespace MongoDbGenericRepository.DataAccess.Update /// /// The type representing a Document. /// The type of the primary key for a Document. - /// The type of the field to update. - /// The client session. + /// The client session. /// The document to modify. - /// The field to update. - /// The value of the field. + /// The update definition. /// The optional cancellation token. - /// + /// A boolean value indicating success. bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable; @@ -86,11 +96,12 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The type of the primary key for a Document. /// The type of the field to update. /// The client session. - /// The document to modify. + /// The filter for the update. /// The field to update. /// The value of the field. + /// The optional partition key. /// The optional cancellation token. - /// + /// A boolean value indicating success. Task UpdateOneAsync(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable; @@ -118,13 +129,12 @@ namespace MongoDbGenericRepository.DataAccess.Update /// The type representing a Document. /// The type of the primary key for a Document. /// The type of the field to update. - /// The client session. - /// The filter for the update. + /// The client session. + /// The document to modify. /// The field to update. /// The value of the field. - /// The optional partition key. /// The optional cancellation token. - /// + /// A boolean value indicating success. Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable; @@ -134,14 +144,10 @@ namespace MongoDbGenericRepository.DataAccess.Update /// /// The type representing a Document. /// The type of the primary key for a Document. - /// The type of the field to update. /// The client session. - /// The filter for the update. - /// The field to update. - /// The value of the field. - /// The optional partition key. + /// The document with the modifications you want to persist. /// The optional cancellation token. - /// + /// A boolean value indicating success. Task UpdateOneAsync(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable; @@ -151,14 +157,11 @@ namespace MongoDbGenericRepository.DataAccess.Update /// /// The type representing a Document. /// The type of the primary key for a Document. - /// The type of the field to update. /// The client session. - /// The filter for the update. - /// The field to update. - /// The value of the field. - /// The optional partition key. + /// The document to modify. + /// The update definition. /// The optional cancellation token. - /// + /// A boolean value indicating success. Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable; diff --git a/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs b/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs index 1c4866d..9370653 100644 --- a/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs +++ b/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository @@ -32,7 +33,8 @@ namespace MongoDbGenericRepository /// The type of the primary key for a Document. /// The Id of the document you want to get. /// An optional partition key. - Task GetByIdAsync(TKey id, string partitionKey = null) + /// An optional cancellation Token. + Task GetByIdAsync(TKey id, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -54,7 +56,8 @@ namespace MongoDbGenericRepository /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. - Task GetOneAsync(Expression> filter, string partitionKey = null) + /// An optional cancellation Token. + Task GetOneAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -88,7 +91,8 @@ namespace MongoDbGenericRepository /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. - Task AnyAsync(Expression> filter, string partitionKey = null) + /// An optional cancellation Token. + Task AnyAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -110,7 +114,8 @@ namespace MongoDbGenericRepository /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. - Task> GetAllAsync(Expression> filter, string partitionKey = null) + /// An optional cancellation Token. + Task> GetAllAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; diff --git a/MongoDbGenericRepository/BaseMongoRepository.Update.ClientSession.cs b/MongoDbGenericRepository/BaseMongoRepository.Update.ClientSession.cs index 86d3b9b..aa661b0 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Update.ClientSession.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Update.ClientSession.cs @@ -18,7 +18,7 @@ namespace MongoDbGenericRepository /// The client session. /// The document with the modifications you want to persist. /// The optional cancellation token. - /// + /// A boolean value indicating success. public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable @@ -34,7 +34,7 @@ namespace MongoDbGenericRepository /// The client session. /// The document with the modifications you want to persist. /// The optional cancellation token. - /// + /// A boolean value indicating success. public virtual bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable @@ -51,7 +51,7 @@ namespace MongoDbGenericRepository /// The document to modify. /// The update definition. /// The optional cancellation token. - /// + /// A boolean value indicating success. public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable @@ -68,7 +68,7 @@ namespace MongoDbGenericRepository /// The document to modify. /// The update definition. /// The optional cancellation token. - /// + /// A boolean value indicating success. public virtual bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable @@ -87,7 +87,7 @@ namespace MongoDbGenericRepository /// The field to update. /// The value of the field. /// The optional cancellation token. - /// + /// A boolean value indicating success. public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable @@ -106,7 +106,7 @@ namespace MongoDbGenericRepository /// The field to update. /// The value of the field. /// The optional cancellation token. - /// + /// A boolean value indicating success. public virtual bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable @@ -126,7 +126,7 @@ namespace MongoDbGenericRepository /// The value of the field. /// The optional partition key. /// The optional cancellation token. - /// + /// A boolean value indicating success. public virtual async Task UpdateOneAsync(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable @@ -146,7 +146,7 @@ namespace MongoDbGenericRepository /// The value of the field. /// The optional partition key. /// The optional cancellation token. - /// + /// A boolean value indicating success. public virtual async Task UpdateOneAsync(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable @@ -166,7 +166,7 @@ namespace MongoDbGenericRepository /// The value of the field. /// The optional partition key. /// The optional cancellation token. - /// + /// A boolean value indicating success. public virtual bool UpdateOne(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable @@ -186,7 +186,7 @@ namespace MongoDbGenericRepository /// The value of the field. /// The optional partition key. /// The optional cancellation token. - /// + /// A boolean value indicating success. public virtual bool UpdateOne(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) where TDocument : IDocument where TKey : IEquatable diff --git a/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs b/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs index f7a4949..c0d324f 100644 --- a/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs +++ b/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs @@ -7,10 +7,20 @@ using System.Linq.Expressions; namespace MongoDbGenericRepository.DataAccess.Base { + /// + /// A base class for accessing the Database and its Collections. + /// public class DataAccessBase { + /// + /// The MongoDbContext + /// protected IMongoDbContext MongoDbContext; + /// + /// The constructor of the DataAccessBase class + /// + /// public DataAccessBase(IMongoDbContext mongoDbContext) { MongoDbContext = mongoDbContext; @@ -108,7 +118,6 @@ namespace MongoDbGenericRepository.DataAccess.Base LanguageOverride = indexCreationOptions.LanguageOverride, ExpireAfter = indexCreationOptions.ExpireAfter, DefaultLanguage = indexCreationOptions.DefaultLanguage, - BucketSize = indexCreationOptions.BucketSize, Bits = indexCreationOptions.Bits, Background = indexCreationOptions.Background, Version = indexCreationOptions.Version diff --git a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs index b07dc0f..375c7fb 100644 --- a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs +++ b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Read @@ -25,12 +26,13 @@ namespace MongoDbGenericRepository.DataAccess.Read /// The type of the primary key for a Document. /// The Id of the document you want to get. /// An optional partition key. - public async virtual Task GetByIdAsync(TKey id, string partitionKey = null) + /// An optional cancellation Token. + public async virtual Task GetByIdAsync(TKey id, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { var filter = Builders.Filter.Eq("Id", id); - return await HandlePartitioned(partitionKey).Find(filter).FirstOrDefaultAsync(); + return await HandlePartitioned(partitionKey).Find(filter).FirstOrDefaultAsync(cancellationToken); } /// @@ -55,11 +57,12 @@ namespace MongoDbGenericRepository.DataAccess.Read /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. - public async virtual Task GetOneAsync(Expression> filter, string partitionKey = null) + /// An optional cancellation Token. + public async virtual Task GetOneAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await HandlePartitioned(partitionKey).Find(filter).FirstOrDefaultAsync(); + return await HandlePartitioned(partitionKey).Find(filter).FirstOrDefaultAsync(cancellationToken); } /// @@ -97,11 +100,12 @@ namespace MongoDbGenericRepository.DataAccess.Read /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. - public async virtual Task AnyAsync(Expression> filter, string partitionKey = null) + /// An optional cancellation Token. + public async virtual Task AnyAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - var count = await HandlePartitioned(partitionKey).CountDocumentsAsync(filter); + var count = await HandlePartitioned(partitionKey).CountDocumentsAsync(filter, cancellationToken: cancellationToken); return (count > 0); } @@ -127,11 +131,12 @@ namespace MongoDbGenericRepository.DataAccess.Read /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. - public async virtual Task> GetAllAsync(Expression> filter, string partitionKey = null) + /// An optional cancellation Token. + public async virtual Task> GetAllAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await HandlePartitioned(partitionKey).Find(filter).ToListAsync(); + return await HandlePartitioned(partitionKey).Find(filter).ToListAsync(cancellationToken); } /// @@ -155,11 +160,12 @@ namespace MongoDbGenericRepository.DataAccess.Read /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partitionKey - public async virtual Task CountAsync(Expression> filter, string partitionKey = null) + /// An optional cancellation Token. + public async virtual Task CountAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await HandlePartitioned(partitionKey).CountDocumentsAsync(filter); + return await HandlePartitioned(partitionKey).CountDocumentsAsync(filter, cancellationToken: cancellationToken); } /// @@ -187,15 +193,16 @@ namespace MongoDbGenericRepository.DataAccess.Read /// The type of the primary key. /// A LINQ expression filter. /// A property selector to order by descending. - /// An optional partitionKey. - public async virtual Task GetByMaxAsync(Expression> filter, Expression> maxValueSelector, string partitionKey = null) + /// An optional partitionKey.An optional cancellation Token. + public async virtual Task GetByMaxAsync(Expression> filter, Expression> maxValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return await GetCollection(partitionKey).Find(Builders.Filter.Where(filter)) .SortByDescending(maxValueSelector) .Limit(1) - .FirstOrDefaultAsync(); + .FirstOrDefaultAsync(cancellationToken); } /// @@ -224,14 +231,15 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partitionKey. - public async virtual Task GetByMinAsync(Expression> filter, Expression> minValueSelector, string partitionKey = null) + /// An optional cancellation Token. + public async virtual Task GetByMinAsync(Expression> filter, Expression> minValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return await GetCollection(partitionKey).Find(Builders.Filter.Where(filter)) .SortBy(minValueSelector) .Limit(1) - .FirstOrDefaultAsync(); + .FirstOrDefaultAsync(cancellationToken); } /// @@ -257,16 +265,18 @@ namespace MongoDbGenericRepository.DataAccess.Read /// /// The document type. /// The type of the primary key. + /// The type of the field for which you want the maximum value. /// A LINQ expression filter. - /// A property selector to order by ascending. + /// A property selector to order by ascending. /// An optional partitionKey. - public async virtual Task GetMaxValueAsync(Expression> filter, Expression> maxValueSelector, string partitionKey = null) + /// An optional cancellation Token. + public async virtual Task GetMaxValueAsync(Expression> filter, Expression> maxValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { return await GetMaxMongoQuery(filter, maxValueSelector, partitionKey) .Project(maxValueSelector) - .FirstOrDefaultAsync(); + .FirstOrDefaultAsync(cancellationToken); } /// @@ -296,11 +306,12 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partition key. - public virtual async Task GetMinValueAsync(Expression> filter, Expression> minValueSelector, string partitionKey = null) + /// An optional cancellation Token. + public virtual async Task GetMinValueAsync(Expression> filter, Expression> minValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await GetMinMongoQuery(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefaultAsync(); + return await GetMinMongoQuery(filter, minValueSelector, partitionKey).Project(minValueSelector).FirstOrDefaultAsync(cancellationToken); } /// @@ -366,13 +377,14 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A LINQ expression filter. /// The field you want to sum. /// The partition key of your document, if any. + /// An optional cancellation Token. public virtual async Task SumByAsync(Expression> filter, Expression> selector, - string partitionKey = null) + string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await GetQuery(filter, partitionKey).SumAsync(selector); + return await GetQuery(filter, partitionKey).SumAsync(selector, cancellationToken); } /// @@ -451,6 +463,36 @@ namespace MongoDbGenericRepository.DataAccess.Read .ToList(); } + /// + /// Groups filtered a collection of documents given a grouping criteria, + /// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the grouping criteria. + /// The type of the projected group. + /// A LINQ expression filter. + /// The grouping criteria. + /// The projected group result. + /// The partition key of your document, if any. + /// An optional cancellation Token. + public virtual async Task> GroupByAsync( + Expression> filter, + Expression> selector, + Expression, TProjection>> projection, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class, new() + { + var collection = HandlePartitioned(partitionKey); + return await collection.Aggregate() + .Match(Builders.Filter.Where(filter)) + .Group(selector, projection) + .ToListAsync(cancellationToken); + } + /// /// Asynchronously returns a paginated list of the documents matching the filter condition. /// @@ -462,13 +504,15 @@ namespace MongoDbGenericRepository.DataAccess.Read /// The number of documents you want to skip. Default value is 0. /// The number of documents you want to take. Default value is 50. /// An optional partition key. + /// An optional cancellation Token. public virtual async Task> GetSortedPaginatedAsync( Expression> filter, Expression> sortSelector, bool ascending = true, int skipNumber = 0, int takeNumber = 50, - string partitionKey = null) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { @@ -481,7 +525,7 @@ namespace MongoDbGenericRepository.DataAccess.Read .Sort(sorting) .Skip(skipNumber) .Limit(takeNumber) - .ToListAsync(); + .ToListAsync(cancellationToken); } /// @@ -494,12 +538,14 @@ namespace MongoDbGenericRepository.DataAccess.Read /// The number of documents you want to skip. Default value is 0. /// The number of documents you want to take. Default value is 50. /// An optional partition key. + /// An optional cancellation Token. public virtual async Task> GetSortedPaginatedAsync( Expression> filter, SortDefinition sortDefinition, int skipNumber = 0, int takeNumber = 50, - string partitionKey = null) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { @@ -508,7 +554,7 @@ namespace MongoDbGenericRepository.DataAccess.Read .Sort(sortDefinition) .Skip(skipNumber) .Limit(takeNumber) - .ToListAsync(); + .ToListAsync(cancellationToken); } } } diff --git a/MongoDbGenericRepository/Models/IndexCreationOptions.cs b/MongoDbGenericRepository/Models/IndexCreationOptions.cs index a117b2a..ad2d60c 100644 --- a/MongoDbGenericRepository/Models/IndexCreationOptions.cs +++ b/MongoDbGenericRepository/Models/IndexCreationOptions.cs @@ -52,10 +52,6 @@ namespace MongoDbGenericRepository.Models /// public string DefaultLanguage { get; set; } /// - /// Gets or sets the size of a geohash bucket. - /// - public double? BucketSize { get; set; } - /// /// Gets or sets the precision, in bits, used with geohash indexes. /// public int? Bits { get; set; } diff --git a/MongoDbGenericRepository/MongoDbGenericRepository.csproj b/MongoDbGenericRepository/MongoDbGenericRepository.csproj index f569520..402a376 100644 --- a/MongoDbGenericRepository/MongoDbGenericRepository.csproj +++ b/MongoDbGenericRepository/MongoDbGenericRepository.csproj @@ -2,8 +2,9 @@ net452;netstandard2.0;netstandard1.5; + true MongoDbGenericRepository - 1.4.5 + 1.4.6 Alexandre Spieser MongoDb Generic Repository A generic repository implementation using the MongoDB C# Sharp 2.0 driver. @@ -14,25 +15,17 @@ Copyright 2020 (c) Alexandre Spieser. All rights reserved. MongoDb Repository Generic NoSql true - 1.4.5 + 1.4.6 https://github.com/alexandre-spieser/mongodb-generic-repository Git - - bin\Release\net45\MongoDbGenericRepository.xml - - - - - - - - D:\development\mongodb-generic-repository\MongoDbGenericRepository\MongoDbGenericRepository.xml - + + bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml + - + diff --git a/MongoDbGenericRepository/ReadOnlyMongoRepository.cs b/MongoDbGenericRepository/ReadOnlyMongoRepository.cs index e9ed276..260625e 100644 --- a/MongoDbGenericRepository/ReadOnlyMongoRepository.cs +++ b/MongoDbGenericRepository/ReadOnlyMongoRepository.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 @@ -49,11 +50,12 @@ namespace MongoDbGenericRepository /// The type of the primary key for a Document. /// The Id of the document you want to get. /// An optional partition key. - public async virtual Task GetByIdAsync(TKey id, string partitionKey = null) + /// An optional cancellation Token. + public async virtual Task GetByIdAsync(TKey id, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbReader.GetByIdAsync(id, partitionKey); + return await MongoDbReader.GetByIdAsync(id, partitionKey, cancellationToken); } /// @@ -77,11 +79,12 @@ namespace MongoDbGenericRepository /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. - public async virtual Task GetOneAsync(Expression> filter, string partitionKey = null) + /// An optional cancellation Token. + public async virtual Task GetOneAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbReader.GetOneAsync(filter, partitionKey); + return await MongoDbReader.GetOneAsync(filter, partitionKey, cancellationToken); } /// @@ -119,11 +122,12 @@ namespace MongoDbGenericRepository /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. - public async virtual Task AnyAsync(Expression> filter, string partitionKey = null) + /// An optional cancellation Token. + public async virtual Task AnyAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbReader.AnyAsync(filter, partitionKey); + return await MongoDbReader.AnyAsync(filter, partitionKey, cancellationToken); } /// @@ -147,11 +151,12 @@ namespace MongoDbGenericRepository /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partition key. - public async virtual Task> GetAllAsync(Expression> filter, string partitionKey = null) + /// An optional cancellation Token. + public async virtual Task> GetAllAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbReader.GetAllAsync(filter, partitionKey); + return await MongoDbReader.GetAllAsync(filter, partitionKey, cancellationToken); } /// From eebdc8957549f8058ee027bad31d312c024ece77 Mon Sep 17 00:00:00 2001 From: Alexandre SPIESER Date: Fri, 12 Mar 2021 10:13:28 +0000 Subject: [PATCH 2/5] adding cancellation token --- .../CoreIntegrationTests.csproj | 5 +- .../Abstractions/IBaseReadOnlyRepository.cs | 75 +++++++++++++------ .../DataAccess/Read/MongoDbReader.Main.cs | 6 +- .../DataAccess/Read/MongoDbReader.Project.cs | 19 ++++- .../ReadOnlyMongoRepository.cs | 74 ++++++++++++------ 5 files changed, 130 insertions(+), 49 deletions(-) diff --git a/CoreIntegrationTests/CoreIntegrationTests.csproj b/CoreIntegrationTests/CoreIntegrationTests.csproj index b89f770..2512b40 100644 --- a/CoreIntegrationTests/CoreIntegrationTests.csproj +++ b/CoreIntegrationTests/CoreIntegrationTests.csproj @@ -8,7 +8,6 @@ - all @@ -18,6 +17,10 @@ + + + + ..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Configuration.dll diff --git a/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs b/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs index 9370653..304607f 100644 --- a/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs +++ b/MongoDbGenericRepository/Abstractions/IBaseReadOnlyRepository.cs @@ -137,7 +137,8 @@ namespace MongoDbGenericRepository /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partitionKey - Task CountAsync(Expression> filter, string partitionKey = null) + /// An optional cancellation Token. + Task CountAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -162,9 +163,13 @@ namespace MongoDbGenericRepository /// The document type. /// The type of the primary key. /// A LINQ expression filter. - /// A property selector to order by descending. + /// A property selector to order by descending. /// An optional partitionKey. - Task GetByMaxAsync(Expression> filter, Expression> orderByDescending, string partitionKey = null) + /// An optional cancellation Token. + Task GetByMaxAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -186,9 +191,13 @@ namespace MongoDbGenericRepository /// The document type. /// The type of the primary key. /// A LINQ expression filter. - /// A property selector to order by ascending. + /// A property selector for the minimum value you are looking for. /// An optional partitionKey. - Task GetByMinAsync(Expression> filter, Expression> orderByAscending, string partitionKey = null) + /// An optional cancellation Token. + Task GetByMinAsync(Expression> filter, + Expression> minValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -198,9 +207,9 @@ namespace MongoDbGenericRepository /// The document type. /// The type of the primary key. /// A LINQ expression filter. - /// A property selector to order by ascending. + /// A property selector for the minimum value you are looking for. /// An optional partitionKey. - TDocument GetByMin(Expression> filter, Expression> orderByAscending, string partitionKey = null) + TDocument GetByMin(Expression> filter, Expression> minValueSelector, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; @@ -211,9 +220,14 @@ namespace MongoDbGenericRepository /// The type of the primary key. /// The type of the value used to order the query. /// A LINQ expression filter. - /// A property selector to order by ascending. + /// A property selector for the maximum value you are looking for. /// An optional partitionKey. - Task GetMaxValueAsync(Expression> filter, Expression> orderByAscending, string partitionKey = null) + /// An optional cancellation Token. + Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -224,9 +238,9 @@ namespace MongoDbGenericRepository /// The type of the primary key. /// The type of the value used to order the query. /// A LINQ expression filter. - /// A property selector to order by ascending. + /// A property selector to order by ascending. /// An optional partitionKey. - TValue GetMaxValue(Expression> filter, Expression> orderByDescending, string partitionKey = null) + TValue GetMaxValue(Expression> filter, Expression> maxValueSelector, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; @@ -239,7 +253,12 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partition key. - Task GetMinValueAsync(Expression> filter, Expression> minValueSelector, string partitionKey = null) + /// An optional cancellation Token. + Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -268,9 +287,11 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The field you want to sum. /// The partition key of your document, if any. + /// An optional cancellation Token. Task SumByAsync(Expression> filter, Expression> selector, - string partitionKey = null) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -326,10 +347,15 @@ namespace MongoDbGenericRepository /// The type representing a Document. /// The type of the primary key for a Document. /// The type representing the model you want to project to. - /// + /// A LINQ expression filter. /// The projection expression. /// An optional partition key. - Task ProjectOneAsync(Expression> filter, Expression> projection, string partitionKey = null) + /// An optional cancellation Token. + Task ProjectOneAsync( + Expression> filter, + Expression> projection, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable where TProjection : class; @@ -354,13 +380,18 @@ namespace MongoDbGenericRepository /// The type representing a Document. /// The type of the primary key for a Document. /// The type representing the model you want to project to. - /// + /// A LINQ expression filter. /// The projection expression. /// An optional partition key. - Task> ProjectManyAsync(Expression> filter, Expression> projection, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable - where TProjection : class; + /// An optional cancellation Token. + Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; /// /// Asynchronously returns a list of projected documents matching the filter condition. @@ -455,12 +486,14 @@ namespace MongoDbGenericRepository /// The number of documents you want to skip. Default value is 0. /// The number of documents you want to take. Default value is 50. /// An optional partition key. + /// An optional cancellation Token. Task> GetSortedPaginatedAsync( Expression> filter, SortDefinition sortDefinition, int skipNumber = 0, int takeNumber = 50, - string partitionKey = null) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; diff --git a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs index 375c7fb..ab6e54c 100644 --- a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs +++ b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs @@ -343,13 +343,15 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A LINQ expression filter. /// The field you want to sum. /// The partition key of your document, if any. + /// An optional cancellation Token. public virtual async Task SumByAsync(Expression> filter, Expression> selector, - string partitionKey = null) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await GetQuery(filter, partitionKey).SumAsync(selector); + return await GetQuery(filter, partitionKey).SumAsync(selector, cancellationToken); } /// diff --git a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Project.cs b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Project.cs index 3499a66..378e86e 100644 --- a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Project.cs +++ b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Project.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.Read @@ -20,14 +21,19 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A LINQ expression filter. /// The projection expression. /// An optional partition key. - public virtual async Task ProjectOneAsync(Expression> filter, Expression> projection, string partitionKey = null) + /// An optional cancellation Token. + public virtual async Task ProjectOneAsync( + Expression> filter, + Expression> projection, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable where TProjection : class { return await HandlePartitioned(partitionKey).Find(filter) .Project(projection) - .FirstOrDefaultAsync(); + .FirstOrDefaultAsync(cancellationToken); } /// @@ -58,14 +64,19 @@ namespace MongoDbGenericRepository.DataAccess.Read /// A LINQ expression filter. /// The projection expression. /// An optional partition key. - public virtual async Task> ProjectManyAsync(Expression> filter, Expression> projection, string partitionKey = null) + /// An optional cancellation Token. + public virtual async Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable where TProjection : class { return await HandlePartitioned(partitionKey).Find(filter) .Project(projection) - .ToListAsync(); + .ToListAsync(cancellationToken); } /// diff --git a/MongoDbGenericRepository/ReadOnlyMongoRepository.cs b/MongoDbGenericRepository/ReadOnlyMongoRepository.cs index 260625e..9c9e33e 100644 --- a/MongoDbGenericRepository/ReadOnlyMongoRepository.cs +++ b/MongoDbGenericRepository/ReadOnlyMongoRepository.cs @@ -180,11 +180,12 @@ namespace MongoDbGenericRepository /// The type of the primary key for a Document. /// A LINQ expression filter. /// An optional partitionKey - public async virtual Task CountAsync(Expression> filter, string partitionKey = null) + /// An optional cancellation Token. + public async virtual Task CountAsync(Expression> filter, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbReader.CountAsync(filter, partitionKey); + return await MongoDbReader.CountAsync(filter, partitionKey, cancellationToken); } /// @@ -209,11 +210,15 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by descending. /// An optional partitionKey. - public async virtual Task GetByMaxAsync(Expression> filter, Expression> maxValueSelector, string partitionKey = null) + /// An optional cancellation Token. + public async virtual Task GetByMaxAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbReader.GetByMaxAsync(filter, maxValueSelector, partitionKey); + return await MongoDbReader.GetByMaxAsync(filter, maxValueSelector, partitionKey, cancellationToken); } /// @@ -237,13 +242,17 @@ namespace MongoDbGenericRepository /// The document type. /// The type of the primary key. /// A LINQ expression filter. - /// A property selector to order by ascending. + /// A property selector for the minimum value you are looking for. /// An optional partitionKey. - public async virtual Task GetByMinAsync(Expression> filter, Expression> minValueSelector, string partitionKey = null) + /// An optional cancellation Token. + public async virtual Task GetByMinAsync(Expression> filter, + Expression> minValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbReader.GetByMinAsync(filter, minValueSelector, partitionKey); + return await MongoDbReader.GetByMinAsync(filter, minValueSelector, partitionKey, cancellationToken); } /// @@ -252,7 +261,7 @@ namespace MongoDbGenericRepository /// The document type. /// The type of the primary key. /// A LINQ expression filter. - /// A property selector to order by ascending. + /// A property selector for the minimum value you are looking for. /// An optional partitionKey. public virtual TDocument GetByMin(Expression> filter, Expression> minValueSelector, string partitionKey = null) where TDocument : IDocument @@ -268,13 +277,18 @@ namespace MongoDbGenericRepository /// The type of the primary key. /// The type of the value used to order the query. /// A LINQ expression filter. - /// A property selector to select the max value. + /// A property selector for the maximum value you are looking for. /// An optional partitionKey. - public async virtual Task GetMaxValueAsync(Expression> filter, Expression> maxValueSelector, string partitionKey = null) + /// An optional cancellation Token. + public async virtual Task GetMaxValueAsync( + Expression> filter, + Expression> maxValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbReader.GetMaxValueAsync(filter, maxValueSelector, partitionKey); + return await MongoDbReader.GetMaxValueAsync(filter, maxValueSelector, partitionKey, cancellationToken); } /// @@ -302,11 +316,16 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// A property selector to order by ascending. /// An optional partition key. - public virtual async Task GetMinValueAsync(Expression> filter, Expression> minValueSelector, string partitionKey = null) + /// An optional cancellation Token. + public virtual async Task GetMinValueAsync( + Expression> filter, + Expression> minValueSelector, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbReader.GetMinValueAsync(filter, minValueSelector, partitionKey); + return await MongoDbReader.GetMinValueAsync(filter, minValueSelector, partitionKey, cancellationToken); } /// @@ -337,13 +356,15 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The field you want to sum. /// The partition key of your document, if any. + /// An optional cancellation Token. public virtual async Task SumByAsync(Expression> filter, Expression> selector, - string partitionKey = null) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbReader.SumByAsync(filter, selector, partitionKey); + return await MongoDbReader.SumByAsync(filter, selector, partitionKey, cancellationToken); } /// @@ -410,12 +431,17 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The projection expression. /// An optional partition key. - public virtual async Task ProjectOneAsync(Expression> filter, Expression> projection, string partitionKey = null) + /// An optional cancellation Token. + public virtual async Task ProjectOneAsync( + Expression> filter, + Expression> projection, + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable where TProjection : class { - return await MongoDbReader.ProjectOneAsync(filter, projection, partitionKey); + return await MongoDbReader.ProjectOneAsync(filter, projection, partitionKey, cancellationToken); } /// @@ -444,12 +470,16 @@ namespace MongoDbGenericRepository /// A LINQ expression filter. /// The projection expression. /// An optional partition key. - public virtual async Task> ProjectManyAsync(Expression> filter, Expression> projection, string partitionKey = null) + /// An optional cancellation Token. + public virtual async Task> ProjectManyAsync( + Expression> filter, + Expression> projection, + string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable where TProjection : class { - return await MongoDbReader.ProjectManyAsync(filter, projection, partitionKey); + return await MongoDbReader.ProjectManyAsync(filter, projection, partitionKey, cancellationToken); } /// @@ -567,12 +597,14 @@ namespace MongoDbGenericRepository /// The number of documents you want to skip. Default value is 0. /// The number of documents you want to take. Default value is 50. /// An optional partition key. + /// An optional cancellation Token. public virtual async Task> GetSortedPaginatedAsync( Expression> filter, SortDefinition sortDefinition, int skipNumber = 0, int takeNumber = 50, - string partitionKey = null) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { @@ -581,7 +613,7 @@ namespace MongoDbGenericRepository .Sort(sortDefinition) .Skip(skipNumber) .Limit(takeNumber) - .ToListAsync(); + .ToListAsync(cancellationToken); } #endregion Pagination From 76a1aab671e4e2be346aefe4062f3b7879302ea2 Mon Sep 17 00:00:00 2001 From: Alexandre SPIESER Date: Mon, 15 Mar 2021 08:03:50 +0000 Subject: [PATCH 3/5] pushed cancellation token changes --- .../BaseMongoRepository.Create.cs | 35 +++++++++++++------ .../DataAccess/Base/DataAccessBase.cs | 8 +++++ .../DataAccess/Create/MongoDbCreator.cs | 20 ++++++++--- .../DataAccess/Read/MongoDbReader.Main.cs | 9 ++++- .../BaseMongoRepository.TKey.Create.cs | 15 +++++--- 5 files changed, 66 insertions(+), 21 deletions(-) diff --git a/MongoDbGenericRepository/BaseMongoRepository.Create.cs b/MongoDbGenericRepository/BaseMongoRepository.Create.cs index d2b319e..70d20e5 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Create.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Create.cs @@ -2,10 +2,15 @@ using MongoDbGenericRepository.Models; using System; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository { + /// + /// The IBaseMongoRepository_Create interface to expose document creation functionality + /// with document having an Id of type Guid. + /// public interface IBaseMongoRepository_Create : IBaseMongoRepository_Create { /// @@ -15,7 +20,8 @@ namespace MongoDbGenericRepository /// The type representing a Document. /// The type of the primary key for a Document. /// The document you want to add. - Task AddOneAsync(TDocument document) + /// An optional cancellation Token. + Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -37,7 +43,8 @@ namespace MongoDbGenericRepository /// The type representing a Document. /// The type of the primary key for a Document. /// The documents you want to add. - Task AddManyAsync(IEnumerable documents) + /// An optional cancellation Token. + Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -61,6 +68,10 @@ namespace MongoDbGenericRepository { private readonly object _initLock = new object(); private MongoDbCreator _mongoDbCreator; + + /// + /// The MongoDbCreator field. + /// protected virtual MongoDbCreator MongoDbCreator { get @@ -87,11 +98,12 @@ namespace MongoDbGenericRepository /// The type representing a Document. /// The type of the primary key for a Document. /// The document you want to add. - public virtual async Task AddOneAsync(TDocument document) + /// An optional cancellation Token. + public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - await MongoDbCreator.AddOneAsync(document); + await MongoDbCreator.AddOneAsync(document, cancellationToken); } /// @@ -100,10 +112,11 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The document you want to add. - public virtual async Task AddOneAsync(TDocument document) + /// An optional cancellation Token. + public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument { - await MongoDbCreator.AddOneAsync(document); + await MongoDbCreator.AddOneAsync(document, cancellationToken); } /// @@ -138,11 +151,12 @@ namespace MongoDbGenericRepository /// The type representing a Document. /// The type of the primary key for a Document. /// The documents you want to add. - public virtual async Task AddManyAsync(IEnumerable documents) + /// An optional cancellation Token. + public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - await MongoDbCreator.AddManyAsync(documents); + await MongoDbCreator.AddManyAsync(documents, cancellationToken); } /// @@ -151,10 +165,11 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The documents you want to add. - public virtual async Task AddManyAsync(IEnumerable documents) + /// An optional cancellation Token. + public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument { - await MongoDbCreator.AddManyAsync(documents); + await MongoDbCreator.AddManyAsync(documents, cancellationToken); } /// diff --git a/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs b/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs index c0d324f..eded8d5 100644 --- a/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs +++ b/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs @@ -28,6 +28,14 @@ namespace MongoDbGenericRepository.DataAccess.Base #region Utility Methods + /// + /// Gets a IMongoQueryable for a potentially partitioned document type and a filter. + /// + /// The document type. + /// The type of the primary key. + /// The filter definition. + /// The collection partition key. + /// public virtual IMongoQueryable GetQuery(Expression> filter, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable diff --git a/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs b/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs index bc490c7..7847030 100644 --- a/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs +++ b/MongoDbGenericRepository/DataAccess/Create/MongoDbCreator.cs @@ -6,12 +6,20 @@ using MongoDbGenericRepository.Utils; using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Create { + /// + /// A class to insert MongoDb document. + /// public class MongoDbCreator : DataAccessBase { + /// + /// The construct of the MongoDbCreator class. + /// + /// A instance. public MongoDbCreator(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } @@ -25,12 +33,13 @@ namespace MongoDbGenericRepository.DataAccess.Create /// The type representing a Document. /// The type of the primary key for a Document. /// The document you want to add. - public virtual async Task AddOneAsync(TDocument document) + /// An optional cancellation Token. + public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { FormatDocument(document); - await HandlePartitioned(document).InsertOneAsync(document); + await HandlePartitioned(document).InsertOneAsync(document, null, cancellationToken); } /// @@ -55,7 +64,8 @@ namespace MongoDbGenericRepository.DataAccess.Create /// The type representing a Document. /// The type of the primary key for a Document. /// The documents you want to add. - public virtual async Task AddManyAsync(IEnumerable documents) + /// An optional cancellation Token. + public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { @@ -72,12 +82,12 @@ namespace MongoDbGenericRepository.DataAccess.Create { foreach (var group in documents.GroupBy(e => ((IPartitionedDocument)e).PartitionKey)) { - await HandlePartitioned(group.FirstOrDefault()).InsertManyAsync(group.ToList()); + await HandlePartitioned(group.FirstOrDefault()).InsertManyAsync(group.ToList(), null, cancellationToken); } } else { - await GetCollection().InsertManyAsync(documents.ToList()); + await GetCollection().InsertManyAsync(documents.ToList(), null, cancellationToken); } } diff --git a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs index ab6e54c..832b4ae 100644 --- a/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs +++ b/MongoDbGenericRepository/DataAccess/Read/MongoDbReader.Main.cs @@ -11,8 +11,15 @@ using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Read { + /// + /// A class to read MongoDb document. + /// public partial class MongoDbReader : DataAccessBase { + /// + /// The construct of the MongoDbReader class. + /// + /// A instance. public MongoDbReader(IMongoDbContext mongoDbContext) : base(mongoDbContext) { } @@ -193,7 +200,7 @@ namespace MongoDbGenericRepository.DataAccess.Read /// The type of the primary key. /// A LINQ expression filter. /// A property selector to order by descending. - /// An optional partitionKey.An optional partitionKey. /// An optional cancellation Token. public async virtual Task GetByMaxAsync(Expression> filter, Expression> maxValueSelector, string partitionKey = null, CancellationToken cancellationToken = default) where TDocument : IDocument diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs index c445c2f..1a1b439 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs @@ -2,6 +2,7 @@ using MongoDbGenericRepository.Models; using System; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository @@ -18,7 +19,8 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The document you want to add. - Task AddOneAsync(TDocument document) where TDocument : IDocument; + /// An optional cancellation Token. + Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument; /// /// Adds a document to the collection. @@ -34,7 +36,8 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The documents you want to add. - Task AddManyAsync(IEnumerable documents) where TDocument : IDocument; + /// An optional cancellation Token. + Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument; /// /// Adds a list of documents to the collection. @@ -81,7 +84,8 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The document you want to add. - public virtual async Task AddOneAsync(TDocument document) where TDocument : IDocument + /// An optional cancellation Token. + public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument { await MongoDbCreator.AddOneAsync(document); } @@ -103,9 +107,10 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The documents you want to add. - public virtual async Task AddManyAsync(IEnumerable documents) where TDocument : IDocument + /// An optional cancellation Token. + public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default) where TDocument : IDocument { - await MongoDbCreator.AddManyAsync(documents); + await MongoDbCreator.AddManyAsync(documents, cancellationToken); } /// From 6d8e358ccc802129de8f9e5c3b8c359deb2656d7 Mon Sep 17 00:00:00 2001 From: Alexandre SPIESER Date: Mon, 15 Mar 2021 09:45:15 +0000 Subject: [PATCH 4/5] reference package in tests --- CoreIntegrationTests/CoreIntegrationTests.csproj | 5 +---- .../Infrastructure/MongoDbTKeyDocumentTestBase.Main.cs | 2 +- MongoDbGenericRepository/MongoDbGenericRepository.csproj | 2 +- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/CoreIntegrationTests/CoreIntegrationTests.csproj b/CoreIntegrationTests/CoreIntegrationTests.csproj index 2512b40..b89f770 100644 --- a/CoreIntegrationTests/CoreIntegrationTests.csproj +++ b/CoreIntegrationTests/CoreIntegrationTests.csproj @@ -8,6 +8,7 @@ + all @@ -17,10 +18,6 @@ - - - - ..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Configuration.dll diff --git a/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.Main.cs b/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.Main.cs index ddd8ff9..fba2045 100644 --- a/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.Main.cs +++ b/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.Main.cs @@ -796,7 +796,7 @@ namespace CoreIntegrationTests.Infrastructure #region Index Management - static SemaphoreSlim textIndexSemaphore = new SemaphoreSlim(1, 1); + static readonly SemaphoreSlim textIndexSemaphore = new SemaphoreSlim(1, 1); [Fact] public async Task CreateTextIndexNoOptionAsync() diff --git a/MongoDbGenericRepository/MongoDbGenericRepository.csproj b/MongoDbGenericRepository/MongoDbGenericRepository.csproj index 402a376..c22899f 100644 --- a/MongoDbGenericRepository/MongoDbGenericRepository.csproj +++ b/MongoDbGenericRepository/MongoDbGenericRepository.csproj @@ -12,7 +12,7 @@ http://www.opensource.org/licenses/mit-license.php false Release notes are at https://github.com/alexandre-spieser/mongodb-generic-repository/releases - Copyright 2020 (c) Alexandre Spieser. All rights reserved. + Copyright 2021 (c) Alexandre Spieser. All rights reserved. MongoDb Repository Generic NoSql true 1.4.6 From 5f6e015bf28693bbc0930c411eabb10c9fc04672 Mon Sep 17 00:00:00 2001 From: Alexandre SPIESER Date: Mon, 15 Mar 2021 11:07:35 +0000 Subject: [PATCH 5/5] fix mistake: missing cancellation token --- .../KeyTypedRepository/BaseMongoRepository.TKey.Create.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs index 1a1b439..98131fd 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Create.cs @@ -87,7 +87,7 @@ namespace MongoDbGenericRepository /// An optional cancellation Token. public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default) where TDocument : IDocument { - await MongoDbCreator.AddOneAsync(document); + await MongoDbCreator.AddOneAsync(document, cancellationToken); } ///