From 7cb5eb2de1acda1e38aefac65f4571f4e146fe36 Mon Sep 17 00:00:00 2001 From: LocInside Date: Mon, 4 Jan 2016 14:09:04 +0530 Subject: [PATCH 1/2] Added Support for 2.0 async driver --- MongoRepository/Repository/IRepository.cs | 53 ++++++++++++ MongoRepository/Repository/MongoRepository.cs | 82 ++++++++++++++++++- 2 files changed, 134 insertions(+), 1 deletion(-) diff --git a/MongoRepository/Repository/IRepository.cs b/MongoRepository/Repository/IRepository.cs index 90852fa..9ac5512 100644 --- a/MongoRepository/Repository/IRepository.cs +++ b/MongoRepository/Repository/IRepository.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; + using System.Threading.Tasks; /// /// IRepository definition. @@ -45,6 +46,19 @@ public interface IRepository : IQueryable /// The entities of type T. void Add(IEnumerable entities); + /// + /// Adds the new entity in the repository asynchronously. + /// + /// The entity to add. + /// The added entity including its new ObjectId. + Task AddAsync(T entity); + + /// + /// Adds the new entities in the repository asynchronously. + /// The entities to add. + /// + Task AddAsync(IEnumerable entities); + /// /// Upserts an entity. /// @@ -58,28 +72,67 @@ public interface IRepository : IQueryable /// The entities to update. void Update(IEnumerable entities); + /// + /// asynchronously Updates entities that match the expression + /// + /// The expression. + /// UpdateDefinition object for the entity + /// Modified count. + Task UpdateAsync(Expression> predicate, UpdateDefinition updateDefinition); + + /// + /// Updates one entity asynchronously. + /// + /// The expression. + /// UpdateDefinition object for the entity + Task UpdateOneAsync(Expression> predicate, UpdateDefinition updateDefinition); + + /// /// Deletes an entity from the repository by its id. /// /// The entity's id. void Delete(TKey id); + /// + /// asynchronously Deletes an entity from the repository by its ObjectId. + /// + /// The ObjectId of the entity. + void DeleteAsync(TKey id); + /// /// Deletes the given entity. /// /// The entity to delete. void Delete(T entity); + /// + /// asynchronously Deletes the given entity. + /// + /// The entity to delete. + Task DeleteAsync(T entity); + /// /// Deletes the entities matching the predicate. /// /// The expression. void Delete(Expression> predicate); + /// + /// asynchronously Deletes the entities matching the predicate. + /// + /// The expression. + Task DeleteAsync(Expression> predicate); + /// /// Deletes all entities in the repository. /// void DeleteAll(); + + /// + /// asynchronously Deletes all entities in the repository. + /// + void DeleteAllAsync(); /// /// Counts the total entities in the repository. diff --git a/MongoRepository/Repository/MongoRepository.cs b/MongoRepository/Repository/MongoRepository.cs index 787efe3..0c7ed3d 100644 --- a/MongoRepository/Repository/MongoRepository.cs +++ b/MongoRepository/Repository/MongoRepository.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; + using System.Threading.Tasks; /// /// Deals with entities in MongoDb. @@ -131,6 +132,26 @@ public virtual void Add(IEnumerable entities) this.collection.InsertMany(entities); } + /// + /// Adds the new entity in the repository asynchronously. + /// + /// The entity T. + /// The added entity including its new ObjectId. + public virtual async Task AddAsync(T entity) + { + await this.collection.InsertOneAsync(entity); + return entity; + } + + /// + /// Adds the new entities in the repository asynchronously. + /// + /// The entities of type T. + public virtual async Task AddAsync(IEnumerable entities) + { + await this.collection.InsertManyAsync(entities); + } + /// /// Upserts an entity. /// @@ -155,6 +176,28 @@ public virtual void Update(IEnumerable entities) this.collection.ReplaceOne(GetIDFilter(entity.Id), entity, new UpdateOptions { IsUpsert = true }); } + /// + /// asynchronously Updates entities that match the expression + /// + /// The expression. + /// UpdateDefinition object for the entity + /// Modified count. + public virtual async Task UpdateAsync(Expression> predicate, UpdateDefinition updateDefinition) + { + var result = await this.collection.UpdateManyAsync(predicate, updateDefinition); + return result.ModifiedCount; + } + + /// + /// Updates one entity asynchronously. + /// + /// The expression. + /// UpdateDefinition object for the entity + public virtual async Task UpdateOneAsync(Expression> predicate, UpdateDefinition updateDefinition) + { + await this.collection.UpdateOneAsync(predicate, updateDefinition); + } + /// /// Deletes an entity from the repository by its id. /// @@ -164,6 +207,16 @@ public virtual void Delete(TKey id) this.collection.DeleteOne(GetIDFilter(id)); } + /// + /// asynchronously Deletes an entity from the repository by its ObjectId. + /// + /// The ObjectId of the entity. + public virtual void DeleteAsync(TKey id) + { + var filter = Builders.Filter.Where(x => x.Id.Equals(id)); + this.collection.DeleteOneAsync(filter); + } + /// /// Deletes an entity from the repository by its ObjectId. /// @@ -172,7 +225,7 @@ public virtual void Delete(ObjectId id) { this.collection.DeleteOne(GetIDFilter(id)); } - + /// /// Deletes the given entity. /// @@ -182,6 +235,15 @@ public virtual void Delete(T entity) this.Delete(entity.Id); } + /// + /// asynchronously Deletes the given entity. + /// + /// The entity to delete. + public virtual async Task DeleteAsync(T entity) + { + await DeleteAsync(x => x.Id.Equals(entity.Id)); + } + /// /// Deletes the entities matching the predicate. /// @@ -191,6 +253,16 @@ public virtual void Delete(Expression> predicate) this.collection.DeleteMany(predicate); } + /// + /// asynchronously Deletes the entities matching the predicate. + /// + /// The expression. + public virtual async Task DeleteAsync(Expression> predicate) + { + var filter = Builders.Filter.Where(predicate); + await this.collection.DeleteManyAsync(filter); + } + /// /// Deletes all entities in the repository. /// @@ -199,6 +271,14 @@ public virtual void DeleteAll() this.collection.DeleteMany(t => true); } + /// + /// asynchronously Deletes all entities in the repository. + /// + public virtual async void DeleteAllAsync() + { + await this.collection.DeleteManyAsync(x => true); + } + /// /// Counts the total entities in the repository. /// From 53ac678efdc57e2166ae368f78bddb2fb1080154 Mon Sep 17 00:00:00 2001 From: LocInside Date: Mon, 4 Jan 2016 16:45:05 +0530 Subject: [PATCH 2/2] 1. use of ReplaceOneAsync(update entire entity) instead of UpdateAsync. 2. Test case written --- MongoRepository/Repository/IRepository.cs | 32 ++++++----- MongoRepository/Repository/MongoRepository.cs | 53 +++++++++++-------- MongoRepositoryTests/RepoTests.cs | 50 +++++++++++++++++ 3 files changed, 95 insertions(+), 40 deletions(-) diff --git a/MongoRepository/Repository/IRepository.cs b/MongoRepository/Repository/IRepository.cs index 9ac5512..d7787e9 100644 --- a/MongoRepository/Repository/IRepository.cs +++ b/MongoRepository/Repository/IRepository.cs @@ -67,25 +67,23 @@ public interface IRepository : IQueryable T Update(T entity); /// - /// Upserts the entities. + /// Upserts an entity asynchronously. /// - /// The entities to update. - void Update(IEnumerable entities); + /// The entity. + /// The updated entity. + Task UpdateAsync(T entity); /// - /// asynchronously Updates entities that match the expression + /// Upserts the entities. /// - /// The expression. - /// UpdateDefinition object for the entity - /// Modified count. - Task UpdateAsync(Expression> predicate, UpdateDefinition updateDefinition); + /// The entities to update. + void Update(IEnumerable entities); /// - /// Updates one entity asynchronously. + /// Upserts the entities asynchronously. /// - /// The expression. - /// UpdateDefinition object for the entity - Task UpdateOneAsync(Expression> predicate, UpdateDefinition updateDefinition); + /// The entities to update. + Task UpdateAsync(IEnumerable entities); /// @@ -95,7 +93,7 @@ public interface IRepository : IQueryable void Delete(TKey id); /// - /// asynchronously Deletes an entity from the repository by its ObjectId. + /// Deletes an entity from the repository by its ObjectId asynchronously. /// /// The ObjectId of the entity. void DeleteAsync(TKey id); @@ -107,7 +105,7 @@ public interface IRepository : IQueryable void Delete(T entity); /// - /// asynchronously Deletes the given entity. + /// Deletes the given entity asynchronously. /// /// The entity to delete. Task DeleteAsync(T entity); @@ -119,7 +117,7 @@ public interface IRepository : IQueryable void Delete(Expression> predicate); /// - /// asynchronously Deletes the entities matching the predicate. + /// Deletes the entities matching the predicate asynchronously. /// /// The expression. Task DeleteAsync(Expression> predicate); @@ -128,9 +126,9 @@ public interface IRepository : IQueryable /// Deletes all entities in the repository. /// void DeleteAll(); - + /// - /// asynchronously Deletes all entities in the repository. + /// Deletes all entities in the repository asynchronously. /// void DeleteAllAsync(); diff --git a/MongoRepository/Repository/MongoRepository.cs b/MongoRepository/Repository/MongoRepository.cs index 0c7ed3d..7898d36 100644 --- a/MongoRepository/Repository/MongoRepository.cs +++ b/MongoRepository/Repository/MongoRepository.cs @@ -166,6 +166,21 @@ public virtual T Update(T entity) return entity; } + /// + /// Upserts an entity asynchronously. + /// + /// The entity. + /// The updated entity. + public virtual async Task UpdateAsync(T entity) + { + if (entity.Id == null) + await this.AddAsync(entity); + else + await this.collection.ReplaceOneAsync(GetIDFilter(entity.Id), entity, new UpdateOptions { IsUpsert = true }); + return entity; + + } + /// /// Upserts the entities. /// @@ -176,28 +191,20 @@ public virtual void Update(IEnumerable entities) this.collection.ReplaceOne(GetIDFilter(entity.Id), entity, new UpdateOptions { IsUpsert = true }); } - /// - /// asynchronously Updates entities that match the expression - /// - /// The expression. - /// UpdateDefinition object for the entity - /// Modified count. - public virtual async Task UpdateAsync(Expression> predicate, UpdateDefinition updateDefinition) - { - var result = await this.collection.UpdateManyAsync(predicate, updateDefinition); - return result.ModifiedCount; - } + /// - /// Updates one entity asynchronously. + /// Upserts the entities asynchronously /// - /// The expression. - /// UpdateDefinition object for the entity - public virtual async Task UpdateOneAsync(Expression> predicate, UpdateDefinition updateDefinition) + /// The entities to update + public virtual async Task UpdateAsync(IEnumerable entities) { - await this.collection.UpdateOneAsync(predicate, updateDefinition); + foreach (T entity in entities) + await this.collection.ReplaceOneAsync(GetIDFilter(entity.Id), entity, new UpdateOptions { IsUpsert = true }); } - + + + /// /// Deletes an entity from the repository by its id. /// @@ -208,7 +215,7 @@ public virtual void Delete(TKey id) } /// - /// asynchronously Deletes an entity from the repository by its ObjectId. + /// Deletes an entity from the repository by its ObjectId asynchronously. /// /// The ObjectId of the entity. public virtual void DeleteAsync(TKey id) @@ -225,7 +232,7 @@ public virtual void Delete(ObjectId id) { this.collection.DeleteOne(GetIDFilter(id)); } - + /// /// Deletes the given entity. /// @@ -236,7 +243,7 @@ public virtual void Delete(T entity) } /// - /// asynchronously Deletes the given entity. + /// Deletes the given entity asynchronously. /// /// The entity to delete. public virtual async Task DeleteAsync(T entity) @@ -254,7 +261,7 @@ public virtual void Delete(Expression> predicate) } /// - /// asynchronously Deletes the entities matching the predicate. + /// Deletes the entities matching the predicate asynchronously. /// /// The expression. public virtual async Task DeleteAsync(Expression> predicate) @@ -272,13 +279,13 @@ public virtual void DeleteAll() } /// - /// asynchronously Deletes all entities in the repository. + /// Deletes all entities in the repository asynchronously. /// public virtual async void DeleteAllAsync() { await this.collection.DeleteManyAsync(x => true); } - + /// /// Counts the total entities in the repository. /// diff --git a/MongoRepositoryTests/RepoTests.cs b/MongoRepositoryTests/RepoTests.cs index 7fa2100..6e41ee0 100644 --- a/MongoRepositoryTests/RepoTests.cs +++ b/MongoRepositoryTests/RepoTests.cs @@ -84,6 +84,56 @@ public void AddAndUpdateTest() Assert.IsTrue(_customerRepo.Exists(c => c.HomeAddress.Country == "Alaska")); } + [TestMethod] + public void AddAndUpdateAsyncTest() + { + IRepository _customerRepo = new MongoRepository(); + IRepositoryManager _customerMan = new MongoRepositoryManager(); + + Assert.IsFalse(_customerMan.Exists); + + var customer = new Customer(); + customer.FirstName = "BobAsync"; + customer.LastName = "Dillon"; + customer.Phone = "0900999899"; + customer.Email = "Bob.dil@snailmail.com"; + customer.HomeAddress = new Address + { + Address1 = "North kingdom 15 west", + Address2 = "1 north way", + PostCode = "40990", + City = "George Town", + Country = "Alaska" + }; + + _customerRepo.AddAsync(customer).Wait(); + + Assert.IsTrue(_customerMan.Exists); + + Assert.IsNotNull(customer.Id); + + // fetch it back + var alreadyAddedCustomer = _customerRepo.Where(c => c.FirstName == "BobAsync").Single(); + + Assert.IsNotNull(alreadyAddedCustomer); + Assert.AreEqual(customer.FirstName, alreadyAddedCustomer.FirstName); + Assert.AreEqual(customer.HomeAddress.Address1, alreadyAddedCustomer.HomeAddress.Address1); + + alreadyAddedCustomer.Phone = "10110111"; + alreadyAddedCustomer.Email = "dil.bob@fastmail.org"; + + _customerRepo.UpdateAsync(alreadyAddedCustomer).Wait(); + + // fetch by id now + var updatedCustomer = _customerRepo.GetById(customer.Id); + + Assert.IsNotNull(updatedCustomer); + Assert.AreEqual(alreadyAddedCustomer.Phone, updatedCustomer.Phone); + Assert.AreEqual(alreadyAddedCustomer.Email, updatedCustomer.Email); + + Assert.IsTrue(_customerRepo.Exists(c => c.HomeAddress.Country == "Alaska")); + } + [TestMethod] public void ComplexEntityTest() {