diff --git a/MongoRepository/Repository/IRepository.cs b/MongoRepository/Repository/IRepository.cs index 90852fa..d7787e9 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. /// @@ -52,35 +66,72 @@ public interface IRepository : IQueryable /// The updated entity. T Update(T entity); + /// + /// Upserts an entity asynchronously. + /// + /// The entity. + /// The updated entity. + Task UpdateAsync(T entity); + /// /// Upserts the entities. /// /// The entities to update. void Update(IEnumerable entities); + /// + /// Upserts the entities asynchronously. + /// + /// The entities to update. + Task UpdateAsync(IEnumerable entities); + + /// /// Deletes an entity from the repository by its id. /// /// The entity's id. void Delete(TKey id); + /// + /// Deletes an entity from the repository by its ObjectId asynchronously. + /// + /// The ObjectId of the entity. + void DeleteAsync(TKey id); + /// /// Deletes the given entity. /// /// The entity to delete. void Delete(T entity); + /// + /// Deletes the given entity asynchronously. + /// + /// The entity to delete. + Task DeleteAsync(T entity); + /// /// Deletes the entities matching the predicate. /// /// The expression. void Delete(Expression> predicate); + /// + /// Deletes the entities matching the predicate asynchronously. + /// + /// The expression. + Task DeleteAsync(Expression> predicate); + /// /// Deletes all entities in the repository. /// void DeleteAll(); + /// + /// Deletes all entities in the repository asynchronously. + /// + void DeleteAllAsync(); + /// /// Counts the total entities in the repository. /// diff --git a/MongoRepository/Repository/MongoRepository.cs b/MongoRepository/Repository/MongoRepository.cs index 787efe3..7898d36 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. /// @@ -145,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. /// @@ -155,6 +191,20 @@ public virtual void Update(IEnumerable entities) this.collection.ReplaceOne(GetIDFilter(entity.Id), entity, new UpdateOptions { IsUpsert = true }); } + + + /// + /// Upserts the entities asynchronously + /// + /// The entities to update + public virtual async Task UpdateAsync(IEnumerable entities) + { + 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. /// @@ -164,6 +214,16 @@ public virtual void Delete(TKey id) this.collection.DeleteOne(GetIDFilter(id)); } + /// + /// Deletes an entity from the repository by its ObjectId asynchronously. + /// + /// 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. /// @@ -182,6 +242,15 @@ public virtual void Delete(T entity) this.Delete(entity.Id); } + /// + /// Deletes the given entity asynchronously. + /// + /// 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 +260,16 @@ public virtual void Delete(Expression> predicate) this.collection.DeleteMany(predicate); } + /// + /// Deletes the entities matching the predicate asynchronously. + /// + /// 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 +278,14 @@ public virtual void DeleteAll() this.collection.DeleteMany(t => true); } + /// + /// 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() {