using MongoDB.Driver; using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Linq.Expressions; using MongoDbGenericRepository.Models; using System.Linq; namespace MongoDbGenericRepository { public interface IBaseMongoRepository { #region Create /// /// Asynchronously adds a document to the collection. /// /// /// The document you want to add. Task AddOneAsync(TDocument document) where TDocument : IDocument; /// /// Adds a document to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// /// /// The document you want to add. void AddOne(TDocument document) where TDocument : IDocument; /// /// Asynchronously adds a list of documents to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// /// /// The document you want to add. Task AddManyAsync(IEnumerable documents) where TDocument : IDocument; /// /// Adds a list of documents to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// /// /// The document you want to add. void AddMany(IEnumerable documents) where TDocument : IDocument; #endregion #region Read /// /// Asynchronously returns one document given its id. /// /// /// The Id of the document you want to get. /// An optional partition key. Task GetByIdAsync(Guid id, string partitionKey = null) where TDocument : IDocument; /// /// Asynchronously returns one document given an expression filter. /// /// /// A LINQ expression filter. /// An optional partition key. Task GetOneAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument; /// /// Returns one document given an expression filter. /// /// /// A LINQ expression filter. /// An optional partition key. TDocument GetOne(Expression> filter, string partitionKey = null) where TDocument : IDocument; /// /// Returns a collection cursor. /// /// /// A LINQ expression filter. /// An optional partition key. IFindFluent GetCursor(Expression> filter, string partitionKey = null) where TDocument : IDocument; /// /// Asynchronously returns true if any of the document of the collection matches the filter condition. /// /// /// A LINQ expression filter. /// An optional partition key. Task AnyAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument; /// /// Returns true if any of the document of the collection matches the filter condition. /// /// /// A LINQ expression filter. /// An optional partition key. bool Any(Expression> filter, string partitionKey = null) where TDocument : IDocument; /// /// Asynchronously returns a list of the documents matching the filter condition. /// /// /// A LINQ expression filter. /// An optional partition key. Task> GetAllAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument; /// /// Returns a list of the documents matching the filter condition. /// /// /// A LINQ expression filter. /// An optional partition key. List GetAll(Expression> filter, string partitionKey = null) where TDocument : IDocument; /// /// Asynchronously counts how many documents match the filter condition. /// /// /// A LINQ expression filter. /// An optional partition key. Task CountAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument; /// /// Counts how many documents match the filter condition. /// /// /// A LINQ expression filter. /// An optional partition key. long Count(Expression> filter, string partitionKey = null) where TDocument : IDocument; #endregion Get } /// /// 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 class BaseMongoRepository : IBaseMongoRepository { public string ConnectionString { get; set; } public string DatabaseName { get; set; } /// /// The base constructor /// /// The connection string of the MongoDb server. /// The name of the database against which you want to perform operations. protected BaseMongoRepository(string connectionString, string databaseName) { _mongoDbContext = new MongoDbContext(connectionString, databaseName); } protected IMongoDbContext _mongoDbContext = null; #region Create /// /// Asynchronously adds a document to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// /// /// The document you want to add. public async Task AddOneAsync(TDocument document) where TDocument : IDocument { FormatDocument(document); await HandlePartitioned(document).InsertOneAsync(document); } /// /// Adds a document to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// /// /// The document you want to add. public void AddOne(TDocument document) where TDocument : IDocument { FormatDocument(document); HandlePartitioned(document).InsertOne(document); } /// /// Asynchronously adds a list of documents to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// /// /// The document you want to add. public async Task AddManyAsync(IEnumerable documents) where TDocument : IDocument { if (!documents.Any()) { return; } foreach (var doc in documents) { FormatDocument(doc); } await HandlePartitioned(documents.FirstOrDefault()).InsertManyAsync(documents); } /// /// Adds a list of documents to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// /// /// The document you want to add. public void AddMany(IEnumerable documents) where TDocument : IDocument { if (!documents.Any()) { return; } foreach (var document in documents) { FormatDocument(document); } HandlePartitioned(documents.FirstOrDefault()).InsertMany(documents.ToList()); } #endregion Create #region Read /// /// Asynchronously returns one document given its id. /// /// /// The Id of the document you want to get. /// An optional partition key. public async Task GetByIdAsync(Guid id, string partitionKey = null) where TDocument : IDocument { var filter = Builders.Filter.Eq("Id", id); return await HandlePartitioned(partitionKey).Find(filter).FirstOrDefaultAsync(); } /// /// Asynchronously returns one document given an expression filter. /// /// /// A LINQ expression filter. /// An optional partition key. public async Task GetOneAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { return await HandlePartitioned(partitionKey).Find(filter).FirstOrDefaultAsync(); } /// /// Returns one document given an expression filter. /// /// /// A LINQ expression filter. /// An optional partition key. public TDocument GetOne(Expression> filter, string partitionKey = null) where TDocument : IDocument { return HandlePartitioned(partitionKey).Find(filter).FirstOrDefault(); } /// /// Returns a collection cursor. /// /// /// A LINQ expression filter. /// An optional partition key. public IFindFluent GetCursor(Expression> filter, string partitionKey = null) where TDocument : IDocument { return HandlePartitioned(partitionKey).Find(filter); } /// /// Returns true if any of the document of the collection matches the filter condition. /// /// /// A LINQ expression filter. /// An optional partition key. public async Task AnyAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { var count = await HandlePartitioned(partitionKey).CountAsync(filter); return (count > 0); } /// /// Returns true if any of the document of the collection matches the filter condition. /// /// /// A LINQ expression filter. /// An optional partition key. public bool Any(Expression> filter, string partitionKey = null) where TDocument : IDocument { var count = HandlePartitioned(partitionKey).Count(filter); return (count > 0); } /// /// Asynchronously returns a list of the documents matching the filter condition. /// /// /// A LINQ expression filter. /// An optional partition key. public async Task> GetAllAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { return await HandlePartitioned(partitionKey).Find(filter).ToListAsync(); } /// /// Returns a list of the documents matching the filter condition. /// /// /// A LINQ expression filter. /// An optional partition key. public List GetAll(Expression> filter, string partitionKey = null) where TDocument : IDocument { return HandlePartitioned(partitionKey).Find(filter).ToList(); } /// /// Asynchronously counts how many documents match the filter condition. /// /// /// A LINQ expression filter. /// An optional partitionKey public async Task CountAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { return await HandlePartitioned(partitionKey).CountAsync(filter); } /// /// Counts how many documents match the filter condition. /// /// /// A LINQ expression filter. /// An optional partitionKey public long Count(Expression> filter, string partitionKey = null) where TDocument : IDocument { return HandlePartitioned(partitionKey).Find(filter).Count(); } /// /// Asynchronously returns a paginated list of the documents matching the filter condition. /// /// /// /// 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 async Task> GetPaginatedAsync(Expression> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null) where TDocument : IDocument { return await HandlePartitioned(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(); } /// /// Returns a list of projected objects /// /// T is a DbEntity /// /// public async Task ProjectBy(Expression> filter, Expression> projection, string partitionKey = null) where TDocument : IDocument where TProjection : class, new() { return await HandlePartitioned(partitionKey).Find(filter) .Project(projection) .FirstOrDefaultAsync(); } #endregion Get #region Delete /// /// A generic delete one method /// /// /// /// public async Task DeleteOneAsync(TDocument document) where TDocument : IDocument { return await DeleteOneAsync(x => x.Id == document.Id); } /// /// A generic delete one method /// /// /// /// public long DeleteOne(TDocument document) where TDocument : IDocument { return DeleteOne(x => x.Id == document.Id); } /// /// A generic delete one method /// /// /// /// public long DeleteOne(Expression> filter, string partitionKey = null) where TDocument : IDocument { return GetCollection().DeleteOne(filter).DeletedCount; } /// /// A generic delete one method /// /// /// /// public async Task DeleteOneAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { return (await GetCollection().DeleteOneAsync(filter)).DeletedCount; } /// /// A generic delete many method /// /// /// /// public async Task DeleteManyAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { var deleteRes = await GetCollection().DeleteManyAsync(filter); return deleteRes.DeletedCount; } /// /// A generic delete many method /// /// /// /// public async Task DeleteManyAsync(IEnumerable documents) where TDocument : IDocument { if (!documents.Any()) { return 0; } var idsTodelete = documents.Select(e => e.Id).ToArray(); var deleteRes = await GetCollection().DeleteManyAsync(x => idsTodelete.Contains(x.Id)); return deleteRes.DeletedCount; } /// /// A generic delete many method /// /// /// /// public long DeleteMany(IEnumerable documents) where TDocument : IDocument { if (!documents.Any()) { return 0; } var idsTodelete = documents.Select(e => e.Id).ToArray(); var deleteRes = GetCollection().DeleteMany(x => idsTodelete.Contains(x.Id)); return deleteRes.DeletedCount; } /// /// A generic delete many method /// /// /// /// public long DeleteMany(Expression> filter, string partitionKey = null) where TDocument : IDocument { var deleteRes = GetCollection().DeleteMany(filter); return deleteRes.DeletedCount; } #endregion Delete #region Update /// /// Updates a document /// /// /// /// public async Task UpdateOneAsync(TDocument entity) where TDocument : IDocument { var updateRes = await GetCollection().ReplaceOneAsync(x => x.Id == entity.Id, entity); return updateRes.ModifiedCount < 1; } /// /// UpdateOne with filter /// /// /// /// /// private async Task UpdateOne(FilterDefinition filter, UpdateDefinition update) where TDocument : IDocument { var updateRes = await GetCollection().UpdateOneAsync(filter, update); return updateRes.ModifiedCount; } /// /// UpdateMany with filter /// /// /// /// /// public async Task UpdateMany(FilterDefinition filter, UpdateDefinition update) where TDocument : IDocument { var collection = GetCollection(); var updateRes = await collection.UpdateManyAsync(filter, update); return updateRes.ModifiedCount; } #endregion Update #region Find And Update /// /// GetAndUpdateOne with filter /// /// /// /// /// /// public async Task GetAndUpdateOne(FilterDefinition filter, UpdateDefinition update, FindOneAndUpdateOptions options) where TDocument : IDocument { return await GetCollection().FindOneAndUpdateAsync(filter, update, options); } #endregion Find And Update /// /// /// /// /// /// private IMongoCollection GetCollection(string partitionKey) where TDocument : IDocument { return _mongoDbContext.GetCollection(partitionKey); } /// /// The private GetCollection method /// /// /// private IMongoCollection GetCollection() where TDocument : IDocument { return _mongoDbContext.GetCollection(); } private IMongoCollection HandlePartitioned(TDocument document) where TDocument : IDocument { if (document is IPartitionedDocument) { return GetCollection(((IPartitionedDocument)document).PartitionKey); } return GetCollection(); } private IMongoCollection HandlePartitioned(string partitionKey) where TDocument : IDocument { if (!string.IsNullOrEmpty(partitionKey)) { return GetCollection(partitionKey); } return GetCollection(); } private void FormatDocument(TDocument document) where TDocument : IDocument { if (document == null) { throw new ArgumentNullException(nameof(document)); } if (document.Id == default(Guid)) { document.Id = Guid.NewGuid(); } if (document.AddedAtUtc == default(DateTime)) { document.AddedAtUtc = DateTime.UtcNow; } } } }