using MongoDB.Driver; using MongoDbGenericRepository.DataAccess.Update; using MongoDbGenericRepository.Models; using System; using System.Linq.Expressions; using System.Threading.Tasks; namespace MongoDbGenericRepository { /// /// 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. /// public abstract partial class BaseMongoRepository : IBaseMongoRepository_Update { private MongoDbUpdater _mongoDbUpdater; protected virtual MongoDbUpdater MongoDbUpdater { get { if (_mongoDbUpdater != null) { return _mongoDbUpdater; } lock (_initLock) { if (_mongoDbUpdater == null) { _mongoDbUpdater = new MongoDbUpdater(MongoDbContext); } } return _mongoDbUpdater; } set { _mongoDbUpdater = value; } } #region Update /// /// Asynchronously Updates a document. /// /// The type representing a Document. /// The document with the modifications you want to persist. public virtual async Task UpdateOneAsync(TDocument modifiedDocument) where TDocument : IDocument { return await MongoDbUpdater.UpdateOneAsync(modifiedDocument); } /// /// Updates a document. /// /// The type representing a Document. /// The document with the modifications you want to persist. public virtual bool UpdateOne(TDocument modifiedDocument) where TDocument : IDocument { return MongoDbUpdater.UpdateOne(modifiedDocument); } /// /// Takes a document you want to modify and applies the update you have defined in MongoDb. /// /// The type representing a Document. /// The document you want to modify. /// The update definition for the document. public virtual async Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update) where TDocument : IDocument { return await MongoDbUpdater.UpdateOneAsync(documentToModify, update); } /// /// Takes a document you want to modify and applies the update you have defined in MongoDb. /// /// The type representing a Document. /// The document you want to modify. /// The update definition for the document. public virtual bool UpdateOne(TDocument documentToModify, UpdateDefinition update) where TDocument : IDocument { return MongoDbUpdater.UpdateOne(documentToModify, update); } /// /// Updates the property field with the given value update a property field in entities. /// /// The type representing a Document. /// The type of the field. /// The document you want to modify. /// The field selector. /// The new value of the property field. public virtual bool UpdateOne(TDocument documentToModify, Expression> field, TField value) where TDocument : IDocument { return MongoDbUpdater.UpdateOne(documentToModify, field, value); } /// /// Updates the property field with the given value update a property field in entities. /// /// The type representing a Document. /// The type of the field. /// The document you want to modify. /// The field selector. /// The new value of the property field. public virtual async Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value) where TDocument : IDocument { return await MongoDbUpdater.UpdateOneAsync(documentToModify, field, value); } /// /// Updates the property field with the given value update a property field in entities. /// /// The type representing 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. public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument { return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey); } /// /// For the entity selected by the filter, updates the property field with the given value. /// /// The type representing 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. public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument { return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey); } /// /// Updates the property field with the given value update a property field in entities. /// /// The type representing 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. public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument { return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey); } /// /// For the entity selected by the filter, updates the property field with the given value. /// /// The type representing 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. public virtual async Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument { return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey); } #endregion Update #region Update TKey /// /// 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. public virtual async Task UpdateOneAsync(TDocument modifiedDocument) where TDocument : IDocument where TKey : IEquatable { return await MongoDbUpdater.UpdateOneAsync(modifiedDocument); } /// /// 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. public virtual bool UpdateOne(TDocument modifiedDocument) where TDocument : IDocument where TKey : IEquatable { return MongoDbUpdater.UpdateOne(modifiedDocument); } /// /// 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. public virtual async Task UpdateOneAsync(TDocument documentToModify, UpdateDefinition update) where TDocument : IDocument where TKey : IEquatable { return await MongoDbUpdater.UpdateOneAsync(documentToModify, update); } /// /// 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. public virtual bool UpdateOne(TDocument documentToModify, UpdateDefinition update) where TDocument : IDocument where TKey : IEquatable { return MongoDbUpdater.UpdateOne(documentToModify, update); } /// /// 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. public virtual bool UpdateOne(TDocument documentToModify, Expression> field, TField value) where TDocument : IDocument where TKey : IEquatable { return MongoDbUpdater.UpdateOne(documentToModify, field, value); } /// /// 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. public virtual async Task UpdateOneAsync(TDocument documentToModify, Expression> field, TField value) where TDocument : IDocument where TKey : IEquatable { return await MongoDbUpdater.UpdateOneAsync(documentToModify, field, value); } /// /// 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 value of the partition key. public virtual bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey); } /// /// 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. public virtual bool UpdateOne(Expression> filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return MongoDbUpdater.UpdateOne(filter, field, value, partitionKey); } /// /// 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. public virtual async Task UpdateOneAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey); } /// /// 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. public virtual async Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { return await MongoDbUpdater.UpdateOneAsync(Builders.Filter.Where(filter), field, value, partitionKey); } #endregion Update } }