From eebdc8957549f8058ee027bad31d312c024ece77 Mon Sep 17 00:00:00 2001 From: Alexandre SPIESER Date: Fri, 12 Mar 2021 10:13:28 +0000 Subject: [PATCH] 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