diff --git a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs index 72009fa..a35c5c1 100644 --- a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs +++ b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using MongoDB.Driver; using MongoDbGenericRepository.Models; @@ -25,11 +26,13 @@ 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> GetPaginatedAsync( Expression> filter, int skipNumber = 0, int takeNumber = 50, - string partitionKey = null) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument; /// @@ -41,11 +44,13 @@ 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> GetPaginatedAsync( Expression> filter, int skipNumber = 0, int takeNumber = 50, - string partitionKey = null) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; @@ -63,6 +68,22 @@ namespace MongoDbGenericRepository FindOneAndUpdateOptions options) where TDocument : IDocument; + /// + /// GetAndUpdateOne with filter + /// + /// The type representing a Document. + /// + /// + /// + /// The cancellation token. + /// + Task GetAndUpdateOne( + FilterDefinition filter, + UpdateDefinition update, + FindOneAndUpdateOptions options, + CancellationToken cancellationToken) + where TDocument : IDocument; + /// /// GetAndUpdateOne with filter /// @@ -78,5 +99,23 @@ namespace MongoDbGenericRepository FindOneAndUpdateOptions options) where TDocument : IDocument where TKey : IEquatable; + + /// + /// GetAndUpdateOne with filter + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// + /// + /// + /// The cancellation token. + /// + Task GetAndUpdateOne( + FilterDefinition filter, + UpdateDefinition update, + FindOneAndUpdateOptions options, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; } } \ No newline at end of file diff --git a/MongoDbGenericRepository/BaseMongoRepository.Main.cs b/MongoDbGenericRepository/BaseMongoRepository.Main.cs index fdbf67f..8595cb4 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Main.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Main.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using MongoDB.Driver; using MongoDbGenericRepository.Models; @@ -39,42 +40,29 @@ namespace MongoDbGenericRepository { } - /// - /// Asynchronously returns a paginated list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// 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. + /// public virtual async Task> GetPaginatedAsync( Expression> filter, int skipNumber = 0, int takeNumber = 50, - string partitionKey = null) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument { - return await HandlePartitioned(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(); + return await HandlePartitioned(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(cancellationToken); } - /// - /// Asynchronously returns a paginated list of the documents matching the filter condition. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// 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. + /// public virtual async Task> GetPaginatedAsync( Expression> filter, int skipNumber = 0, int takeNumber = 50, - string partitionKey = null) + string partitionKey = null, + CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable { - return await HandlePartitioned(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(); + return await HandlePartitioned(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(cancellationToken); } /// @@ -150,33 +138,28 @@ namespace MongoDbGenericRepository } #region Find And Update - - /// - /// GetAndUpdateOne with filter - /// - /// The type representing a Document. - /// A LINQ expression filter. - /// - /// - /// + /// public virtual async Task GetAndUpdateOne( FilterDefinition filter, UpdateDefinition update, FindOneAndUpdateOptions options) where TDocument : IDocument { - return await GetCollection().FindOneAndUpdateAsync(filter, update, options); + return await GetAndUpdateOne(filter, update, options, CancellationToken.None); } - /// - /// GetAndUpdateOne with filter - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// A LINQ expression filter. - /// - /// - /// + /// + public virtual async Task GetAndUpdateOne( + FilterDefinition filter, + UpdateDefinition update, + FindOneAndUpdateOptions options, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await GetCollection().FindOneAndUpdateAsync(filter, update, options, cancellationToken); + } + + /// public virtual async Task GetAndUpdateOne( FilterDefinition filter, UpdateDefinition update, @@ -184,7 +167,19 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable { - return await GetCollection().FindOneAndUpdateAsync(filter, update, options); + return await GetAndUpdateOne(filter, update, options, CancellationToken.None); + } + + /// + public virtual async Task GetAndUpdateOne( + FilterDefinition filter, + UpdateDefinition update, + FindOneAndUpdateOptions options, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetCollection().FindOneAndUpdateAsync(filter, update, options, cancellationToken); } #endregion Find And Update