From 3b45c594156740f51f4bdd7e5f8562866c2dac11 Mon Sep 17 00:00:00 2001 From: alexandre-spieser Date: Sun, 27 Aug 2017 17:51:46 +0000 Subject: [PATCH] Added interface support for partitioned collections --- IntegrationTests/CreatePartitionedTests.cs | 8 +-- MongoDbGenericRepository/MongoDbRepository.cs | 69 ++++++++++++------- 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/IntegrationTests/CreatePartitionedTests.cs b/IntegrationTests/CreatePartitionedTests.cs index 2061a18..051bd81 100644 --- a/IntegrationTests/CreatePartitionedTests.cs +++ b/IntegrationTests/CreatePartitionedTests.cs @@ -18,7 +18,7 @@ namespace IntegrationTests public class CreatePartitionedTests : BaseMongoDbRepositoryTests { [Test] - public void AddOne() + public void PartitionedAddOne() { // Arrange var document = new CreateTestsPartitionedDocument(); @@ -30,7 +30,7 @@ namespace IntegrationTests } [Test] - public async Task AddOneAsync() + public async Task PartitionedAddOneAsync() { // Arrange var document = new CreateTestsPartitionedDocument(); @@ -42,7 +42,7 @@ namespace IntegrationTests } [Test] - public void AddMany() + public void PartitionedAddMany() { // Arrange var documents = new List { new CreateTestsPartitionedDocument(), new CreateTestsPartitionedDocument() }; @@ -54,7 +54,7 @@ namespace IntegrationTests } [Test] - public async Task AddManyAsync() + public async Task PartitionedAddManyAsync() { // Arrange var documents = new List { new CreateTestsPartitionedDocument(), new CreateTestsPartitionedDocument() }; diff --git a/MongoDbGenericRepository/MongoDbRepository.cs b/MongoDbGenericRepository/MongoDbRepository.cs index d7dc163..bf7563a 100644 --- a/MongoDbGenericRepository/MongoDbRepository.cs +++ b/MongoDbGenericRepository/MongoDbRepository.cs @@ -52,69 +52,79 @@ namespace MongoDbGenericRepository /// /// /// The Id of the document you want to get. - Task GetById(Guid id) where TDocument : IDocument; + /// 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. - Task GetOneAsync(Expression> filter) where TDocument : IDocument; + /// 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. - TDocument GetOne(Expression> filter) where TDocument : IDocument; + /// An optional partition key. + TDocument GetOne(Expression> filter, string partitionKey = null) where TDocument : IDocument; /// /// Returns a collection cursor. /// /// /// A LINQ expression filter. - IFindFluent GetCursor(Expression> filter) where TDocument : IDocument; + /// 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. - Task AnyAsync(Expression> filter) where TDocument : IDocument; + /// 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. - bool Any(Expression> filter) where TDocument : IDocument; + /// 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. - Task> GetAllAsync(Expression> filter) where TDocument : IDocument; + /// 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. - List GetAll(Expression> filter) where TDocument : IDocument; + /// 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. - Task CountAsync(Expression> filter) where TDocument : IDocument; + /// 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 @@ -211,10 +221,11 @@ namespace MongoDbGenericRepository /// /// /// The Id of the document you want to get. - public async Task GetById(Guid id) where TDocument : IDocument + /// 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 GetCollection().Find(filter).FirstOrDefaultAsync(); + return await HandlePartitioned(partitionKey).Find(filter).FirstOrDefaultAsync(); } /// @@ -222,7 +233,8 @@ namespace MongoDbGenericRepository /// /// /// A LINQ expression filter. - public async Task GetOneAsync(Expression> filter) where TDocument : IDocument + /// An optional partition key. + public async Task GetOneAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { return await GetCollection().Find(filter).FirstOrDefaultAsync(); } @@ -232,7 +244,8 @@ namespace MongoDbGenericRepository /// /// /// A LINQ expression filter. - public TDocument GetOne(Expression> filter) where TDocument : IDocument + /// An optional partition key. + public TDocument GetOne(Expression> filter, string partitionKey = null) where TDocument : IDocument { return GetCollection().Find(filter).FirstOrDefault(); } @@ -242,7 +255,8 @@ namespace MongoDbGenericRepository /// /// /// A LINQ expression filter. - public IFindFluent GetCursor(Expression> filter) where TDocument : IDocument + /// An optional partition key. + public IFindFluent GetCursor(Expression> filter, string partitionKey = null) where TDocument : IDocument { return GetCollection().Find(filter); } @@ -252,7 +266,8 @@ namespace MongoDbGenericRepository /// /// /// A LINQ expression filter. - public async Task AnyAsync(Expression> filter) where TDocument : IDocument + /// An optional partition key. + public async Task AnyAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { var count = await GetCollection().CountAsync(filter); return (count > 0); @@ -263,7 +278,8 @@ namespace MongoDbGenericRepository /// /// /// A LINQ expression filter. - public bool Any(Expression> filter) where TDocument : IDocument + /// An optional partition key. + public bool Any(Expression> filter, string partitionKey = null) where TDocument : IDocument { var count = GetCollection().Count(filter); return (count > 0); @@ -274,7 +290,8 @@ namespace MongoDbGenericRepository /// /// /// A LINQ expression filter. - public async Task> GetAllAsync(Expression> filter) where TDocument : IDocument + /// An optional partition key. + public async Task> GetAllAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { return await GetCollection().Find(filter).ToListAsync(); } @@ -284,7 +301,8 @@ namespace MongoDbGenericRepository /// /// /// A LINQ expression filter. - public List GetAll(Expression> filter) where TDocument : IDocument + /// An optional partition key. + public List GetAll(Expression> filter, string partitionKey = null) where TDocument : IDocument { return GetCollection().Find(filter).ToList(); } @@ -295,7 +313,7 @@ namespace MongoDbGenericRepository /// /// A LINQ expression filter. /// An optional partitionKey - public async Task CountAsync(Expression> filter) where TDocument : IDocument + public async Task CountAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { return await GetCollection().CountAsync(filter); } @@ -318,7 +336,8 @@ namespace MongoDbGenericRepository /// /// The number of documents you want to skip. Default value is 0. /// The number of documents you want to take. Default value is 50. - public async Task> GetPaginatedAsync(Expression> filter, int skipNumber = 0, int takeNumber = 50) where TDocument : IDocument + /// An optional partition key. + public async Task> GetPaginatedAsync(Expression> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null) where TDocument : IDocument { return await GetCollection().Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(); } @@ -330,7 +349,7 @@ namespace MongoDbGenericRepository /// T is a DbEntity /// /// - public async Task ProjectBy(Expression> filter, Expression> projection) + public async Task ProjectBy(Expression> filter, Expression> projection, string partitionKey = null) where TDocument : IDocument where TProjection : class, new() { @@ -371,7 +390,7 @@ namespace MongoDbGenericRepository /// /// /// - public long DeleteOne(Expression> filter) where TDocument : IDocument + public long DeleteOne(Expression> filter, string partitionKey = null) where TDocument : IDocument { return GetCollection().DeleteOne(filter).DeletedCount; } @@ -382,7 +401,7 @@ namespace MongoDbGenericRepository /// /// /// - public async Task DeleteOneAsync(Expression> filter) where TDocument : IDocument + public async Task DeleteOneAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { return (await GetCollection().DeleteOneAsync(filter)).DeletedCount; } @@ -393,7 +412,7 @@ namespace MongoDbGenericRepository /// /// /// - public async Task DeleteManyAsync(Expression> filter) where TDocument : IDocument + public async Task DeleteManyAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { var deleteRes = await GetCollection().DeleteManyAsync(filter); return deleteRes.DeletedCount; @@ -439,7 +458,7 @@ namespace MongoDbGenericRepository /// /// /// - public long DeleteMany(Expression> filter) where TDocument : IDocument + public long DeleteMany(Expression> filter, string partitionKey = null) where TDocument : IDocument { var deleteRes = GetCollection().DeleteMany(filter); return deleteRes.DeletedCount;