From b1cda2ac50f416bc1b259a109e3b7d1cae08477f Mon Sep 17 00:00:00 2001 From: Alexandre SPIESER Date: Sat, 18 May 2019 19:49:44 +0100 Subject: [PATCH] adding client session to mongodb updater methods --- .../IBaseMongoRepository_Update.cs | 135 +++++++++++ ...aseMongoRepository_Update_ClientSession.cs | 166 ++++++++++++++ .../IReadOnlyMongoRepository.TKey.cs | 42 ++++ ...aseMongoRepository.Update.ClientSession.cs | 197 ++++++++++++++++ .../BaseMongoRepository.Update.cs | 127 +--------- .../Update/MongoDbUpdater.ClientSession.cs | 217 ++++++++++++++++++ .../DataAccess/Update/MongoDbUpdater.cs | 7 +- 7 files changed, 760 insertions(+), 131 deletions(-) create mode 100644 MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update.cs create mode 100644 MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs create mode 100644 MongoDbGenericRepository/BaseMongoRepository.Update.ClientSession.cs create mode 100644 MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs diff --git a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update.cs b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update.cs new file mode 100644 index 0000000..878fdb7 --- /dev/null +++ b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update.cs @@ -0,0 +1,135 @@ +using MongoDB.Driver; +using MongoDbGenericRepository.Models; +using System; +using System.Linq.Expressions; +using System.Threading.Tasks; + +namespace MongoDbGenericRepository +{ + public interface IBaseMongoRepository_Update : IBaseMongoRepository_Update + { + /// + /// Asynchronously Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document with the modifications you want to persist. + Task UpdateOneAsync(TDocument modifiedDocument) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document with the modifications you want to persist. + bool UpdateOne(TDocument modifiedDocument) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Takes a document you want to modify and applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to modify. + /// The update definition for the document. + Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Takes a document you want to modify and applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document you want to modify. + /// The update definition for the document. + bool UpdateOne(TDocument documentToModify, UpdateDefinition update) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entity selected by the filter, updates the property field with the given value.. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document you want to modify. + /// The field selector. + /// The new value of the property field. + Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entity selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document you want to modify. + /// The field selector. + /// The new value of the property field. + bool UpdateOne(TDocument documentToModify, Expression> field, TField value) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates the property field with the given value update a property field in entities. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + } +} diff --git a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs new file mode 100644 index 0000000..8ed2e1e --- /dev/null +++ b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update_ClientSession.cs @@ -0,0 +1,166 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDbGenericRepository.Models; + +namespace MongoDbGenericRepository.DataAccess.Update +{ + public interface IBaseMongoRepository_Update_ClientSession + { + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The client session. + /// The document with the modifications you want to persist. + /// The optional cancellation token. + /// + bool UpdateOne(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The client session. + /// The document with the modifications you want to persist. + /// The optional cancellation token. + /// + bool UpdateOne(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// 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 field to update. + /// The value of the field. + /// The optional cancellation token. + /// + bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// 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 field to update. + /// The value of the field. + /// The optional cancellation token. + /// + Task UpdateOneAsync(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + Task UpdateOneAsync(IClientSessionHandle session, FilterDefinition filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + Task UpdateOneAsync(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable; + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs b/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs index 7790fc8..9b311f0 100644 --- a/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs +++ b/MongoDbGenericRepository/Abstractions/IReadOnlyMongoRepository.TKey.cs @@ -335,5 +335,47 @@ namespace MongoDbGenericRepository where TProjection : class, new(); #endregion Group By + + #region Pagination + + /// + /// 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 property selector. + /// Order of the sorting. + /// 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. + Task> GetSortedPaginatedAsync( + Expression> filter, + Expression> sortSelector, + bool ascending = true, + int skipNumber = 0, + int takeNumber = 50, + string partitionKey = null) + where TDocument : IDocument; + + /// + /// 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 sort definition. + /// 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. + Task> GetSortedPaginatedAsync( + Expression> filter, + SortDefinition sortDefinition, + int skipNumber = 0, + int takeNumber = 50, + string partitionKey = null) + where TDocument : IDocument; + + #endregion Pagination } } \ No newline at end of file diff --git a/MongoDbGenericRepository/BaseMongoRepository.Update.ClientSession.cs b/MongoDbGenericRepository/BaseMongoRepository.Update.ClientSession.cs new file mode 100644 index 0000000..86d3b9b --- /dev/null +++ b/MongoDbGenericRepository/BaseMongoRepository.Update.ClientSession.cs @@ -0,0 +1,197 @@ +using MongoDB.Driver; +using MongoDbGenericRepository.DataAccess.Update; +using MongoDbGenericRepository.Models; +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; + +namespace MongoDbGenericRepository +{ + public abstract partial class BaseMongoRepository : IBaseMongoRepository_Update_ClientSession + { + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The client session. + /// The document with the modifications you want to persist. + /// The optional cancellation token. + /// + public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbUpdater.UpdateOneAsync(session, modifiedDocument, cancellationToken); + } + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The client session. + /// The document with the modifications you want to persist. + /// The optional cancellation token. + /// + public virtual bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbUpdater.UpdateOne(session, modifiedDocument, cancellationToken); + } + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbUpdater.UpdateOneAsync(session, documentToModify, update, cancellationToken); + } + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + public virtual bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbUpdater.UpdateOne(session, documentToModify, update, cancellationToken); + } + + /// + /// Updates a document. + /// + /// 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 field to update. + /// The value of the field. + /// The optional cancellation token. + /// + public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbUpdater.UpdateOneAsync(session, documentToModify, field, value, cancellationToken); + } + + /// + /// Updates a document. + /// + /// 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 field to update. + /// The value of the field. + /// The optional cancellation token. + /// + public virtual bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbUpdater.UpdateOne(session, documentToModify, field, value, cancellationToken); + } + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + 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 + { + return await MongoDbUpdater.UpdateOneAsync(session, filter, field, value, partitionKey, cancellationToken); + } + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + 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 + { + return await MongoDbUpdater.UpdateOneAsync(session, filter, field, value, partitionKey, cancellationToken); + } + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + 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 + { + return MongoDbUpdater.UpdateOne(session, filter, field, value, partitionKey, cancellationToken); + } + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + 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 + { + return UpdateOne(session, Builders.Filter.Where(filter), field, value, partitionKey, cancellationToken); + } + } +} diff --git a/MongoDbGenericRepository/BaseMongoRepository.Update.cs b/MongoDbGenericRepository/BaseMongoRepository.Update.cs index 9447af6..61696a7 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Update.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Update.cs @@ -7,132 +7,6 @@ using System.Threading.Tasks; namespace MongoDbGenericRepository { - public interface IBaseMongoRepository_Update : IBaseMongoRepository_Update - { - /// - /// Asynchronously Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document with the modifications you want to persist. - Task UpdateOneAsync(TDocument modifiedDocument) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Updates a document. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document with the modifications you want to persist. - bool UpdateOne(TDocument modifiedDocument) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Takes a document you want to modify and applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to modify. - /// The update definition for the document. - Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Takes a document you want to modify and applies the update you have defined in MongoDb. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The document you want to modify. - /// The update definition for the document. - bool UpdateOne(TDocument documentToModify, UpdateDefinition update) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// For the entity selected by the filter, updates the property field with the given value.. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document you want to modify. - /// The field selector. - /// The new value of the property field. - Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// For the entity selected by the filter, updates the property field with the given value. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The partition key for the document. - Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document you want to modify. - /// The field selector. - /// The new value of the property field. - bool UpdateOne(TDocument documentToModify, Expression> field, TField value) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - - /// - /// Updates the property field with the given value update a property field in entities. - /// - /// The type representing a Document. - /// The type of the primary key for a Document. - /// The type of the field. - /// The document filter. - /// The field selector. - /// The new value of the property field. - /// The value of the partition key. - bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) - where TDocument : IDocument - where TKey : IEquatable; - } /// /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. /// Its constructor must be given a connection string and a database name. @@ -452,6 +326,7 @@ namespace MongoDbGenericRepository return await MongoDbUpdater.UpdateOneAsync(Builders.Filter.Where(filter), field, value, partitionKey); } + #endregion Update } diff --git a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs new file mode 100644 index 0000000..372b0c9 --- /dev/null +++ b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.ClientSession.cs @@ -0,0 +1,217 @@ +using MongoDB.Driver; +using MongoDbGenericRepository.Models; +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; + +namespace MongoDbGenericRepository.DataAccess.Update +{ + public partial class MongoDbUpdater + { + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The client session. + /// The document with the modifications you want to persist. + /// The optional cancellation token. + /// + public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable + { + var filter = Builders.Filter.Eq("Id", modifiedDocument.Id); + var updateRes = await HandlePartitioned(modifiedDocument) + .ReplaceOneAsync(session, filter, modifiedDocument, cancellationToken: cancellationToken) + .ConfigureAwait(false); + return updateRes.ModifiedCount == 1; + } + + /// + /// Updates a document. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The client session. + /// The document with the modifications you want to persist. + /// The optional cancellation token. + /// + public virtual bool UpdateOne(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable + { + var filter = Builders.Filter.Eq("Id", modifiedDocument.Id); + var updateRes = HandlePartitioned(modifiedDocument).ReplaceOne(session, filter, modifiedDocument, cancellationToken: cancellationToken); + return updateRes.ModifiedCount == 1; + } + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable + { + var filter = Builders.Filter.Eq("Id", documentToModify.Id); + var updateRes = await HandlePartitioned(documentToModify).UpdateOneAsync(session, filter, update, new UpdateOptions { IsUpsert = true }, cancellationToken).ConfigureAwait(false); + return updateRes.ModifiedCount == 1; + } + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + public virtual bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition update, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable + { + var filter = Builders.Filter.Eq("Id", documentToModify.Id); + var updateRes = HandlePartitioned(documentToModify).UpdateOne(session, filter, update, new UpdateOptions { IsUpsert = true }, cancellationToken); + return updateRes.ModifiedCount == 1; + } + + /// + /// Updates a document. + /// + /// 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 field to update. + /// The value of the field. + /// The optional cancellation token. + /// + public virtual async Task UpdateOneAsync(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable + { + var filter = Builders.Filter.Eq("Id", documentToModify.Id); + var updateRes = await HandlePartitioned(documentToModify) + .UpdateOneAsync(session, filter, Builders.Update.Set(field, value), cancellationToken: cancellationToken) + .ConfigureAwait(false); + + return updateRes.ModifiedCount == 1; + } + + /// + /// Updates a document. + /// + /// 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 field to update. + /// The value of the field. + /// The optional cancellation token. + /// + public virtual bool UpdateOne(IClientSessionHandle session, TDocument documentToModify, Expression> field, TField value, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable + { + var filter = Builders.Filter.Eq("Id", documentToModify.Id); + var updateRes = HandlePartitioned(documentToModify).UpdateOne(session, filter, Builders.Update.Set(field, value), cancellationToken: cancellationToken); + return updateRes.ModifiedCount == 1; + } + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + 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 + { + var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); + var updateRes = await collection.UpdateOneAsync(session, filter, Builders.Update.Set(field, value), cancellationToken: cancellationToken).ConfigureAwait(false); + return updateRes.ModifiedCount == 1; + } + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + public virtual Task UpdateOneAsync(IClientSessionHandle session, Expression> filter, Expression> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken)) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateOneAsync(session, Builders.Filter.Where(filter), field, value, partitionKey, cancellationToken); + } + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + 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 + { + var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); + var updateRes = collection.UpdateOne(session, filter, Builders.Update.Set(field, value), cancellationToken: cancellationToken); + return updateRes.ModifiedCount == 1; + } + + /// + /// Updates a document. + /// + /// 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 optional cancellation token. + /// + 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 + { + return UpdateOne(session, Builders.Filter.Where(filter), field, value, partitionKey, cancellationToken); + } + } +} diff --git a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs index 0657132..c14d83c 100644 --- a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs +++ b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs @@ -1,17 +1,14 @@ using MongoDB.Driver; -using MongoDB.Driver.Linq; using MongoDbGenericRepository.DataAccess.Base; using MongoDbGenericRepository.Models; -using MongoDbGenericRepository.Utils; using System; -using System.Collections.Generic; -using System.Linq; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; namespace MongoDbGenericRepository.DataAccess.Update { - public class MongoDbUpdater : DataAccessBase + public partial class MongoDbUpdater : DataAccessBase { public MongoDbUpdater(IMongoDbContext mongoDbContext) : base(mongoDbContext) {