public bool DeletePartyCustomAttribute(IEnumerable <PartyCustomAttribute> entities) { Contract.Requires(entities != null); Contract.Requires(Contract.ForAll(entities, (PartyCustomAttribute e) => e != null)); Contract.Requires(Contract.ForAll(entities, (PartyCustomAttribute e) => e.Id >= 0)); using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository <PartyCustomAttribute> repo = uow.GetRepository <PartyCustomAttribute>(); IRepository <PartyCustomAttributeValue> repoCAV = uow.GetRepository <PartyCustomAttributeValue>(); repo.Evict(); foreach (var entity in entities) { var latest = repo.Reload(entity); //Prevent of deleting if there is a 'customAttributeVaue' for this entity if (latest.CustomAttributeValues.Count() > 0) { BexisException.Throw(latest, "There is one or more 'customAttributeVaue' for this entity.", BexisException.ExceptionType.Delete, true); } //delete the entity repo.Delete(latest); } // commit changes uow.Commit(); } // if any problem was detected during the commit, an exception will be thrown! return(true); }
public bool Delete(IEnumerable <PartyRelationshipType> entities) { Contract.Requires(entities != null); Contract.Requires(Contract.ForAll(entities, (PartyRelationshipType e) => e != null)); Contract.Requires(Contract.ForAll(entities, (PartyRelationshipType e) => e.Id >= 0)); using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository <PartyRelationshipType> repoPR = uow.GetRepository <PartyRelationshipType>(); repoPR.Evict(); IRepository <PartyType> repoType = uow.GetRepository <PartyType>(); foreach (var entity in entities) { var latest = repoPR.Reload(entity); //If there is a relation between entity and a party we couldn't delete it if (latest.PartyRelationships.Count() > 0) { BexisException.Throw(latest, "There are some relations between this 'PartyRelationshipType' and 'Party'", BexisException.ExceptionType.Delete, true); } // remove all associations between the entity and AssociatedPairs latest.AssociatedPairs.ToList().ForEach(item => item.PartyRelationshipType = null); latest.AssociatedPairs.Clear(); repoPR.Delete(latest); } uow.Commit(); } return(true); }
/// <summary> /// If non of the <paramref name="entities"/> are associated to any <see cref="Dateset"/> entity, the method deletes them from the database. /// </summary> /// <param name="entities">The data structure objects to be deleted in a all or none approach.</param> /// <returns>True if all the data structures are deleted, False otherwise.</returns> /// <remarks>If any of the data structure objects is used by any dataset, the whole transaction will be roll backed, so that the other entities are also not deleted. /// <br>Database exceptions are not handled intentionally, so that if the data structure is related to some datasets, a proper exception will be thrown.</br> /// </remarks> public bool DeleteStructuredDataStructure(IEnumerable <StructuredDataStructure> entities) { Contract.Requires(entities != null); Contract.Requires(Contract.ForAll(entities, (StructuredDataStructure e) => e != null)); Contract.Requires(Contract.ForAll(entities, (StructuredDataStructure e) => e.Id >= 0)); using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository <StructuredDataStructure> repo = uow.GetRepository <StructuredDataStructure>(); IReadOnlyRepository <Dataset> datasetRepo = this.GetUnitOfWork().GetReadOnlyRepository <Dataset>(); IRepository <Variable> variableRepo = uow.GetRepository <Variable>(); IRepository <Parameter> paramRepo = uow.GetRepository <Parameter>(); foreach (var entity in entities) { variableRepo.Evict(); paramRepo.Evict(); var latest = repo.Reload(entity); if (datasetRepo.Query(p => p.DataStructure.Id == latest.Id).Count() > 0) { uow.Ignore(); throw new Exception(string.Format("Data structure {0} is used by datasets. Deletion Failed", entity.Id)); } // delete associated variables and thier parameters foreach (var usage in latest.Variables) { var localVar = variableRepo.Reload(usage); paramRepo.Delete(localVar.Parameters.ToList()); variableRepo.Delete(localVar); } //uow.Commit(); // should not be needed repo.Delete(latest); } uow.Commit(); } return(true); }
public void Test_Can_Fetch_Eager_Load_Common(IRepository<Question> r) { var q = new Question { Text = "Hello", Answers = new List<Answer>(), Comments = new List<QuestionComment>() }; var c = new QuestionComment { Question = q, Text = "Hei" }; var a = new Answer { Question = q, Text = "Yes", Comments = new List<AnswerComment>() }; q.Comments.Add(c); var ac = new AnswerComment { Answer = a, Text = "hahah" }; a.Comments.Add(ac); q.Answers.Add(a); r.Save(q); r.Evict(q.QuestionId); var g = r.GetEager(q.QuestionId, "Answers", "Comments"); Assert.AreEqual("Hello", g.Text); // System.Threading.Thread.Sleep(10000); Assert.AreEqual("Hei", g.Comments[0].Text); Assert.AreEqual("Yes", g.Answers[0].Text); Assert.AreEqual("hahah", g.Answers[0].Comments[0].Text); }
void Common_Can_queue_changes(IRepository<Question> repo) { // Arrange Question importantQuestion = Refactored_Common_SaveGraph_Arrange_Create(repo); int questionId = importantQuestion.QuestionId; byte[] rowVersion = importantQuestion.RowVersion; /* Question retrievedQuestion; { var query = repo.All.Where(x => x.QuestionId == questionId); if (repo.All.Provider.GetType() == typeof(NHibernate.Linq.NhQueryProvider)) { retrievedQuestion = repo.Get(questionId); } else { query = query.Include("Answers"); query = query.Include("Comments"); query = query.Include("Answers.Comments"); retrievedQuestion = query.Single(); } }*/ Question retrievedQuestion = repo.GetEager(questionId); retrievedQuestion.Text = "Hello"; retrievedQuestion.Answers.Single(x => x.Poster == "John").Text = "number 9, number 9, number 9..."; repo.Evict(questionId); // must Evict transient changes so it will not affect the next Save // Act repo.Save(new Question { Text = "Hi", Poster = "Optimus" }); /* Question testConflicter; // let's check if the two aggregate root didn't affect each other { var query = repo.All.Where(x => x.QuestionId == questionId); if (repo.All.Provider.GetType() == typeof(NHibernate.Linq.NhQueryProvider)) { testConflicter = repo.Get(questionId); // throw new Exception(testConflicter.Text + " " + testConflicter.Comments.Single().Text); } else { query = query.Include("Answers"); query = query.Include("Comments"); query = query.Include("Answers.Comments"); testConflicter = query.Single(); } } */ Question testConflicter = repo.GetEager(questionId); // Assert Assert.AreNotSame(importantQuestion, retrievedQuestion); Assert.AreNotSame(retrievedQuestion, testConflicter); Assert.AreEqual("The answer to life", testConflicter.Text); Assert.AreEqual("42", testConflicter.Answers.Single(x => x.Poster == "John").Text); Assert.AreEqual("Hello", retrievedQuestion.Text); /* Evicting the object from EF results on collections becoming empty, NHibernate left the stale object as is. throw new Exception(retrievedQuestion.Answers.Count.ToString()); // zero on Entity Framework after marking objects detached // Single won't work, there's no element Assert.AreEqual("number 9, number 9, number 9...", retrievedQuestion.Answers.Single(x => x.Poster == "John").Text); */ }