From 9b1048e31854e82f6e7007b2050345af291d0de3 Mon Sep 17 00:00:00 2001 From: alexandre-spieser Date: Sat, 23 Sep 2017 22:00:31 +0000 Subject: [PATCH] Adding TKey support --- .../BaseMongoDbRepository.cs | 446 +++++++++++++++--- MongoDbGenericRepository/IMongoDbContext.cs | 11 + MongoDbGenericRepository/Models/IDocument.cs | 12 +- .../Models/IPartitionedDocument.cs | 2 +- MongoDbGenericRepository/MongoDbContext.cs | 24 +- 5 files changed, 417 insertions(+), 78 deletions(-) diff --git a/MongoDbGenericRepository/BaseMongoDbRepository.cs b/MongoDbGenericRepository/BaseMongoDbRepository.cs index d9e90e5..1427742 100644 --- a/MongoDbGenericRepository/BaseMongoDbRepository.cs +++ b/MongoDbGenericRepository/BaseMongoDbRepository.cs @@ -27,7 +27,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously adds a document to the collection. /// - /// + /// The type representing a Document. /// The document you want to add. Task AddOneAsync(TDocument document) where TDocument : IDocument; @@ -35,7 +35,7 @@ namespace MongoDbGenericRepository /// Adds a document to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// - /// + /// The type representing a Document. /// The document you want to add. void AddOne(TDocument document) where TDocument : IDocument; @@ -43,7 +43,7 @@ namespace MongoDbGenericRepository /// Asynchronously adds a list of documents to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// - /// + /// The type representing a Document. /// The document you want to add. Task AddManyAsync(IEnumerable documents) where TDocument : IDocument; @@ -51,7 +51,7 @@ namespace MongoDbGenericRepository /// Adds a list of documents to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// - /// + /// The type representing a Document. /// The document you want to add. void AddMany(IEnumerable documents) where TDocument : IDocument; @@ -62,7 +62,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously returns one document given its id. /// - /// + /// The type representing a Document. /// The Id of the document you want to get. /// An optional partition key. Task GetByIdAsync(Guid id, string partitionKey = null) where TDocument : IDocument; @@ -70,7 +70,7 @@ namespace MongoDbGenericRepository /// /// Returns one document given its id. /// - /// + /// The type representing a Document. /// The Id of the document you want to get. /// An optional partition key. TDocument GetById(Guid id, string partitionKey = null) where TDocument : IDocument; @@ -78,7 +78,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously returns one document given an expression filter. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. Task GetOneAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument; @@ -86,7 +86,7 @@ namespace MongoDbGenericRepository /// /// Returns one document given an expression filter. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. TDocument GetOne(Expression> filter, string partitionKey = null) where TDocument : IDocument; @@ -94,7 +94,7 @@ namespace MongoDbGenericRepository /// /// Returns a collection cursor. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. IFindFluent GetCursor(Expression> filter, string partitionKey = null) where TDocument : IDocument; @@ -102,7 +102,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously returns true if any of the document of the collection matches the filter condition. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. Task AnyAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument; @@ -110,7 +110,7 @@ namespace MongoDbGenericRepository /// /// Returns true if any of the document of the collection matches the filter condition. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. bool Any(Expression> filter, string partitionKey = null) where TDocument : IDocument; @@ -118,7 +118,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously returns a list of the documents matching the filter condition. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. Task> GetAllAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument; @@ -126,7 +126,7 @@ namespace MongoDbGenericRepository /// /// Returns a list of the documents matching the filter condition. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. List GetAll(Expression> filter, string partitionKey = null) where TDocument : IDocument; @@ -134,7 +134,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously counts how many documents match the filter condition. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. Task CountAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument; @@ -142,7 +142,7 @@ namespace MongoDbGenericRepository /// /// Counts how many documents match the filter condition. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. long Count(Expression> filter, string partitionKey = null) where TDocument : IDocument; @@ -154,14 +154,14 @@ namespace MongoDbGenericRepository /// /// Asynchronously Updates a document. /// - /// + /// The type representing a Document. /// The document with the modifications you want to persist. Task UpdateOneAsync(TDocument modifiedDocument) where TDocument : IDocument; /// /// Updates a document. /// - /// + /// The type representing a Document. /// The document with the modifications you want to persist. bool UpdateOne(TDocument modifiedDocument) where TDocument : IDocument; @@ -172,7 +172,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously deletes a document. /// - /// + /// The type representing a Document. /// The document you want to delete. /// The number of documents deleted. Task DeleteOneAsync(TDocument document) where TDocument : IDocument; @@ -180,7 +180,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously deletes a document matching the condition of the LINQ expression filter. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. @@ -189,7 +189,7 @@ namespace MongoDbGenericRepository /// /// Deletes a document. /// - /// + /// The type representing a Document. /// The document you want to delete. /// The number of documents deleted. long DeleteOne(TDocument document) where TDocument : IDocument; @@ -197,7 +197,7 @@ namespace MongoDbGenericRepository /// /// Deletes a document matching the condition of the LINQ expression filter. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. @@ -206,7 +206,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. @@ -215,7 +215,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously deletes a list of documents. /// - /// + /// The type representing a Document. /// The list of documents to delete. /// The number of documents deleted. Task DeleteManyAsync(IEnumerable documents) where TDocument : IDocument; @@ -223,7 +223,7 @@ namespace MongoDbGenericRepository /// /// Deletes a list of documents. /// - /// + /// The type representing a Document. /// The list of documents to delete. /// The number of documents deleted. long DeleteMany(IEnumerable documents) where TDocument : IDocument; @@ -231,21 +231,33 @@ namespace MongoDbGenericRepository /// /// Deletes the documents matching the condition of the LINQ expression filter. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. long DeleteMany(Expression> filter, string partitionKey = null) where TDocument : IDocument; + /// + /// Deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The number of documents deleted. + long DeleteMany(Expression> filter, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + #endregion - #region Project + #region Project /// /// Asynchronously returns a projected document matching the filter condition. /// - /// - /// + /// The type representing a Document. + /// The type representing the model you want to project to. /// /// The projection expression. /// An optional partition key. @@ -253,11 +265,25 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TProjection : class; + /// + /// Asynchronously returns a projected document matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// + /// The projection expression. + /// An optional partition key. + Task ProjectOneAsync(Expression> filter, Expression> projection, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + /// /// Returns a projected document matching the filter condition. /// - /// - /// + /// The type representing a Document. + /// The type representing the model you want to project to. /// /// The projection expression. /// An optional partition key. @@ -265,11 +291,25 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TProjection : class; + /// + /// Returns a projected document matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// + /// The projection expression. + /// An optional partition key. + TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + /// /// Asynchronously returns a list of projected documents matching the filter condition. /// - /// - /// + /// The type representing a Document. + /// The type representing the model you want to project to. /// /// The projection expression. /// An optional partition key. @@ -280,8 +320,22 @@ namespace MongoDbGenericRepository /// /// Asynchronously returns a list of projected documents matching the filter condition. /// - /// - /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// + /// 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; + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type representing the model you want to project to. /// /// The projection expression. /// An optional partition key. @@ -289,8 +343,70 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TProjection : class; + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// + /// The projection expression. + /// An optional partition key. + List ProjectMany(Expression> filter, Expression> projection, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class; + #endregion + /// + /// Asynchronously returns a paginated list of the documents matching the filter condition. + /// + /// The type representing a Document. + /// + /// 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> GetPaginatedAsync(Expression> filter, 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. + /// + /// 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> GetPaginatedAsync(Expression> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// GetAndUpdateOne with filter + /// + /// The type representing a Document. + /// + /// + /// + /// + Task GetAndUpdateOne(FilterDefinition filter, UpdateDefinition update, FindOneAndUpdateOptions options) + where TDocument : IDocument; + + /// + /// GetAndUpdateOne with filter + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// + /// + /// + /// + Task GetAndUpdateOne(FilterDefinition filter, UpdateDefinition update, FindOneAndUpdateOptions options) + where TDocument : IDocument + where TKey : IEquatable; + } /// @@ -338,7 +454,7 @@ namespace MongoDbGenericRepository /// Asynchronously adds a document to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// - /// + /// The type representing a Document. /// The document you want to add. public async Task AddOneAsync(TDocument document) where TDocument : IDocument { @@ -350,7 +466,7 @@ namespace MongoDbGenericRepository /// Adds a document to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// - /// + /// The type representing a Document. /// The document you want to add. public void AddOne(TDocument document) where TDocument : IDocument { @@ -362,7 +478,7 @@ namespace MongoDbGenericRepository /// Asynchronously adds a list of documents to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// - /// + /// The type representing a Document. /// The documents you want to add. public async Task AddManyAsync(IEnumerable documents) where TDocument : IDocument { @@ -381,7 +497,7 @@ namespace MongoDbGenericRepository /// Adds a list of documents to the collection. /// Populates the Id and AddedAtUtc fields if necessary. /// - /// + /// The type representing a Document. /// The documents you want to add. public void AddMany(IEnumerable documents) where TDocument : IDocument { @@ -403,7 +519,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously returns one document given its id. /// - /// + /// The type representing a Document. /// 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 @@ -415,7 +531,7 @@ namespace MongoDbGenericRepository /// /// Returns one document given its id. /// - /// + /// The type representing a Document. /// The Id of the document you want to get. /// An optional partition key. public TDocument GetById(Guid id, string partitionKey = null) where TDocument : IDocument @@ -427,7 +543,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously returns one document given an expression filter. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. public async Task GetOneAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument @@ -438,7 +554,7 @@ namespace MongoDbGenericRepository /// /// Returns one document given an expression filter. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. public TDocument GetOne(Expression> filter, string partitionKey = null) where TDocument : IDocument @@ -449,7 +565,7 @@ namespace MongoDbGenericRepository /// /// Returns a collection cursor. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. public IFindFluent GetCursor(Expression> filter, string partitionKey = null) where TDocument : IDocument @@ -460,7 +576,7 @@ namespace MongoDbGenericRepository /// /// Returns true if any of the document of the collection matches the filter condition. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. public async Task AnyAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument @@ -472,7 +588,7 @@ namespace MongoDbGenericRepository /// /// Returns true if any of the document of the collection matches the filter condition. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. public bool Any(Expression> filter, string partitionKey = null) where TDocument : IDocument @@ -484,7 +600,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously returns a list of the documents matching the filter condition. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. public async Task> GetAllAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument @@ -495,7 +611,7 @@ namespace MongoDbGenericRepository /// /// Returns a list of the documents matching the filter condition. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. public List GetAll(Expression> filter, string partitionKey = null) where TDocument : IDocument @@ -506,7 +622,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously counts how many documents match the filter condition. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partitionKey public async Task CountAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument @@ -517,7 +633,7 @@ namespace MongoDbGenericRepository /// /// Counts how many documents match the filter condition. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partitionKey public long Count(Expression> filter, string partitionKey = null) where TDocument : IDocument @@ -532,7 +648,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously Updates a document. /// - /// + /// The type representing a Document. /// The document with the modifications you want to persist. public async Task UpdateOneAsync(TDocument modifiedDocument) where TDocument : IDocument { @@ -543,7 +659,7 @@ namespace MongoDbGenericRepository /// /// Updates a document. /// - /// + /// The type representing a Document. /// The document with the modifications you want to persist. public bool UpdateOne(TDocument modifiedDocument) where TDocument : IDocument { @@ -558,7 +674,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously deletes a document. /// - /// + /// The type representing a Document. /// The document you want to delete. /// The number of documents deleted. public async Task DeleteOneAsync(TDocument document) where TDocument : IDocument @@ -569,7 +685,7 @@ namespace MongoDbGenericRepository /// /// Deletes a document. /// - /// + /// The type representing a Document. /// The document you want to delete. /// The number of documents deleted. public long DeleteOne(TDocument document) where TDocument : IDocument @@ -580,7 +696,7 @@ namespace MongoDbGenericRepository /// /// Deletes a document matching the condition of the LINQ expression filter. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. @@ -592,7 +708,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously deletes a document matching the condition of the LINQ expression filter. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. @@ -604,7 +720,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously deletes the documents matching the condition of the LINQ expression filter. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. @@ -616,7 +732,7 @@ namespace MongoDbGenericRepository /// /// Asynchronously deletes a list of documents. /// - /// + /// The type representing a Document. /// The list of documents to delete. /// The number of documents deleted. public async Task DeleteManyAsync(IEnumerable documents) where TDocument : IDocument @@ -632,7 +748,7 @@ namespace MongoDbGenericRepository /// /// Deletes a list of documents. /// - /// + /// The type representing a Document. /// The list of documents to delete. /// The number of documents deleted. public long DeleteMany(IEnumerable documents) where TDocument : IDocument @@ -645,10 +761,29 @@ namespace MongoDbGenericRepository return HandlePartitioned(documents.FirstOrDefault()).DeleteMany(x => idsTodelete.Contains(x.Id)).DeletedCount; } + /// + /// Deletes a list of documents. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The list of documents to delete. + /// The number of documents deleted. + public long DeleteMany(IEnumerable documents) + where TDocument : IDocument + where TKey : IEquatable + { + if (!documents.Any()) + { + return 0; + } + var idsTodelete = documents.Select(e => e.Id).ToArray(); + return HandlePartitioned(documents.FirstOrDefault()).DeleteMany(x => idsTodelete.Contains(x.Id)).DeletedCount; + } + /// /// Deletes the documents matching the condition of the LINQ expression filter. /// - /// + /// The type representing a Document. /// A LINQ expression filter. /// An optional partition key. /// The number of documents deleted. @@ -657,6 +792,21 @@ namespace MongoDbGenericRepository return HandlePartitioned(partitionKey).DeleteMany(filter).DeletedCount; } + /// + /// Deletes the documents matching the condition of the LINQ expression filter. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// A LINQ expression filter. + /// An optional partition key. + /// The number of documents deleted. + public long DeleteMany(Expression> filter, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return HandlePartitioned(partitionKey).DeleteMany(filter).DeletedCount; + } + #endregion Delete #region Project @@ -664,8 +814,8 @@ namespace MongoDbGenericRepository /// /// Asynchronously returns a projected document matching the filter condition. /// - /// - /// + /// The type representing a Document. + /// The type representing the model you want to project to. /// /// The projection expression. /// An optional partition key. @@ -678,11 +828,30 @@ namespace MongoDbGenericRepository .FirstOrDefaultAsync(); } + /// + /// Asynchronously returns a projected document matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// + /// The projection expression. + /// An optional partition key. + public async Task ProjectOneAsync(Expression> filter, Expression> projection, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return await HandlePartitioned(partitionKey).Find(filter) + .Project(projection) + .FirstOrDefaultAsync(); + } + /// /// Returns a projected document matching the filter condition. /// - /// - /// + /// The type representing a Document. + /// The type representing the model you want to project to. /// /// The projection expression. /// An optional partition key. @@ -695,11 +864,30 @@ namespace MongoDbGenericRepository .FirstOrDefault(); } + /// + /// Returns a projected document matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// + /// The projection expression. + /// An optional partition key. + public TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return HandlePartitioned(partitionKey).Find(filter) + .Project(projection) + .FirstOrDefault(); + } + /// /// Asynchronously returns a list of projected documents matching the filter condition. /// - /// - /// + /// The type representing a Document. + /// The type representing the model you want to project to. /// /// The projection expression. /// An optional partition key. @@ -715,8 +903,27 @@ namespace MongoDbGenericRepository /// /// Asynchronously returns a list of projected documents matching the filter condition. /// - /// - /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// + /// The projection expression. + /// An optional partition key. + public async Task> ProjectManyAsync(Expression> filter, Expression> projection, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return await HandlePartitioned(partitionKey).Find(filter) + .Project(projection) + .ToListAsync(); + } + + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type representing the model you want to project to. /// /// The projection expression. /// An optional partition key. @@ -729,27 +936,63 @@ namespace MongoDbGenericRepository .ToList(); } + /// + /// Asynchronously returns a list of projected documents matching the filter condition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type representing the model you want to project to. + /// + /// The projection expression. + /// An optional partition key. + public List ProjectMany(Expression> filter, Expression> projection, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + where TProjection : class + { + return HandlePartitioned(partitionKey).Find(filter) + .Project(projection) + .ToList(); + } + #endregion /// /// Asynchronously returns a paginated list of the documents matching the filter condition. /// - /// + /// The type representing a Document. /// /// 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 + 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(); } + /// + /// 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. + /// + /// 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 + where TKey : IEquatable + { + return await HandlePartitioned(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(); + } + #region Find And Update /// /// GetAndUpdateOne with filter /// - /// + /// The type representing a Document. /// /// /// @@ -759,6 +1002,22 @@ namespace MongoDbGenericRepository return await GetCollection().FindOneAndUpdateAsync(filter, update, options); } + /// + /// GetAndUpdateOne with filter + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// + /// + /// + /// + public async Task GetAndUpdateOne(FilterDefinition filter, UpdateDefinition update, FindOneAndUpdateOptions options) + where TDocument : IDocument + where TKey : IEquatable + { + return await GetCollection().FindOneAndUpdateAsync(filter, update, options); + } + #endregion Find And Update #region Private Methods @@ -782,6 +1041,17 @@ namespace MongoDbGenericRepository return GetCollection(); } + private IMongoCollection HandlePartitioned(TDocument document) + where TDocument : IDocument + where TKey : IEquatable + { + if (document is IPartitionedDocument) + { + return GetCollection(((IPartitionedDocument)document).PartitionKey); + } + return GetCollection(); + } + private IMongoCollection HandlePartitioned(string partitionKey) where TDocument : IDocument { if (!string.IsNullOrEmpty(partitionKey)) @@ -803,6 +1073,42 @@ namespace MongoDbGenericRepository } } + + private IMongoCollection GetCollection(string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbContext.GetCollection(partitionKey); + } + + private IMongoCollection GetCollection() + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbContext.GetCollection(); + } + + private IMongoCollection HandlePartitioned(string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + if (!string.IsNullOrEmpty(partitionKey)) + { + return GetCollection(partitionKey); + } + return GetCollection(); + } + + private void FormatDocument(TDocument document) + where TDocument : IDocument + where TKey : IEquatable + { + if (document == null) + { + throw new ArgumentNullException(nameof(document)); + } + } + #endregion } } \ No newline at end of file diff --git a/MongoDbGenericRepository/IMongoDbContext.cs b/MongoDbGenericRepository/IMongoDbContext.cs index 31fee2a..ca2b297 100644 --- a/MongoDbGenericRepository/IMongoDbContext.cs +++ b/MongoDbGenericRepository/IMongoDbContext.cs @@ -1,5 +1,6 @@ using MongoDB.Driver; using MongoDbGenericRepository.Models; +using System; namespace MongoDbGenericRepository { @@ -31,6 +32,16 @@ namespace MongoDbGenericRepository /// The value of the partition key. IMongoCollection GetCollection(string partitionKey) where TDocument : IDocument; + /// + /// Returns a collection for a document type that has a partition key. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The value of the partition key. + IMongoCollection GetCollection(string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + /// /// Drops a collection, use very carefully. /// diff --git a/MongoDbGenericRepository/Models/IDocument.cs b/MongoDbGenericRepository/Models/IDocument.cs index bdad597..07d25c4 100644 --- a/MongoDbGenericRepository/Models/IDocument.cs +++ b/MongoDbGenericRepository/Models/IDocument.cs @@ -6,16 +6,24 @@ namespace MongoDbGenericRepository.Models /// This class represents a basic document that can be stored in MongoDb. /// Your document must implement this class in order for the MongoDbRepository to handle them. /// - public interface IDocument + public interface IDocument where TKey : IEquatable { /// /// The Guid, which must be decorated with the [BsonId] attribute /// if you want the MongoDb C# driver to consider it to be the document ID. /// - Guid Id { get; set; } + TKey Id { get; set; } /// /// A version number, to indicate the version of the schema. /// int Version { get; set; } } + + /// + /// This class represents a basic document that can be stored in MongoDb. + /// Your document must implement this class in order for the MongoDbRepository to handle them. + /// + public interface IDocument: IDocument + { + } } \ No newline at end of file diff --git a/MongoDbGenericRepository/Models/IPartitionedDocument.cs b/MongoDbGenericRepository/Models/IPartitionedDocument.cs index 9dbf5e2..21626ca 100644 --- a/MongoDbGenericRepository/Models/IPartitionedDocument.cs +++ b/MongoDbGenericRepository/Models/IPartitionedDocument.cs @@ -6,7 +6,7 @@ /// This can be useful if you are planning to build a Software as a Service (SaaS) Platform, or if you want to reduce indexing. /// You could for example insert Logs in different collections based on the week and year they where created, or their Log category/source. /// - public interface IPartitionedDocument : IDocument + public interface IPartitionedDocument { /// /// The partition key used to partition your collection. diff --git a/MongoDbGenericRepository/MongoDbContext.cs b/MongoDbGenericRepository/MongoDbContext.cs index dd21e9d..2223fe3 100644 --- a/MongoDbGenericRepository/MongoDbContext.cs +++ b/MongoDbGenericRepository/MongoDbContext.cs @@ -1,5 +1,6 @@ using MongoDB.Driver; using MongoDbGenericRepository.Models; +using System; namespace MongoDbGenericRepository { @@ -48,7 +49,7 @@ namespace MongoDbGenericRepository /// /// The private GetCollection method /// - /// + /// The type representing a Document. /// public IMongoCollection GetCollection() { @@ -58,17 +59,30 @@ namespace MongoDbGenericRepository /// /// Returns a collection for a document type that has a partition key. /// - /// + /// The type representing a Document. /// The value of the partition key. public IMongoCollection GetCollection(string partitionKey) where TDocument : IDocument { return Database.GetCollection(partitionKey +"-"+ Pluralize()); } + /// + /// Returns a collection for a document type that has a partition key. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The value of the partition key. + public IMongoCollection GetCollection(string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return Database.GetCollection(partitionKey + "-" + Pluralize()); + } + /// /// Drops a collection, use very carefully. /// - /// + /// The type representing a Document. public void DropCollection() { Database.DropCollection(Pluralize()); @@ -77,7 +91,7 @@ namespace MongoDbGenericRepository /// /// Drops a collection having a partitionkey, use very carefully. /// - /// + /// The type representing a Document. public void DropCollection(string partitionKey) { Database.DropCollection(partitionKey + "-" + Pluralize()); @@ -86,7 +100,7 @@ namespace MongoDbGenericRepository /// /// Very naively pluralizes a TDocument type name. /// - /// + /// The type representing a Document. /// private string Pluralize() {