public static void Test(IKeyedRepository <TKey, TEntity> repo, List <string> protectedPropertyNames, TEntity seed) { // Get the inital count int countBefore = repo.All().Count(); // Look for the seed.Id item in the DB; if found, refresh up to 5 times then error. int loopCount = 0; while (true) { var item = repo.FindBy(seed.Id); if (item == null) { break; } if (loopCount > 4) { Assert.Fail("Can't find a unique new key for: " + repo.GetType().Name); return; } seed = Random <TEntity> .UpdateSpecifiedProperties(seed, new List <string> { "Id" }); loopCount += 1; } // add repo.Insert(seed); int countAfter = repo.All().Count(); Assert.IsTrue(countBefore + 1 == countAfter); // read TEntity entity2 = repo.FindBy(seed.Id); //Assert.IsTrue(seed.Equals(entity2)); Assert.IsTrue(Comparison.PublicInstancePropertiesEqual(seed, entity2, protectedPropertyNames)); // update protectedPropertyNames.Add("Id"); // Make sure Id is now in the list. seed = Random <TEntity> .Update(seed, protectedPropertyNames); repo.Update(seed); TEntity entity3 = repo.FindBy(seed.Id); Assert.IsFalse(Comparison.PublicInstancePropertiesEqual(entity2, seed, protectedPropertyNames)); Assert.IsTrue(Comparison.PublicInstancePropertiesEqual(entity3, seed, protectedPropertyNames)); // delete repo.Delete(seed.Id); countAfter = repo.All().Count(); Assert.IsTrue(countAfter == countBefore); }
public void Read_Returns_Null_If_Id_Does_Not_Exist() { using (IDbSession dbSession = _dbSessionFactory.Create()) { IKeyedRepository <Guid, Person> repo = dbSession.CreateKeyedRepository <Guid, Person>(); Person shouldBeNull = repo.FindBy(Guid.NewGuid()); Assert.IsNull(shouldBeNull); dbSession.Commit(); } }
public void Commit_And_Rollback_Work() { Person person = new Person { Id = Guid.NewGuid(), FirstName = Guid.NewGuid().ToString(), LastName = Guid.NewGuid().ToString() }; // Rollback using (DbSession dbSession = GetSession()) { IKeyedRepository <Guid, Person> repo = dbSession.CreateKeyedRepository <Guid, Person>(); repo.Add(person); dbSession.Rollback(); } using (DbSession dbSession = GetSession()) { IKeyedRepository <Guid, Person> repo = dbSession.CreateKeyedRepository <Guid, Person>(); Assert.IsNull(repo.FindBy(person.Id)); } // Commit using (DbSession dbSession = GetSession()) { IKeyedRepository <Guid, Person> repo = dbSession.CreateKeyedRepository <Guid, Person>(); repo.Add(person); dbSession.Commit(); } using (DbSession dbSession = GetSession()) { IKeyedRepository <Guid, Person> repo = dbSession.CreateKeyedRepository <Guid, Person>(); Assert.IsNotNull(repo.FindBy(person.Id)); } // Cleanup using (DbSession dbSession = GetSession()) { IKeyedRepository <Guid, Person> repo = dbSession.CreateKeyedRepository <Guid, Person>(); repo.Delete(person); dbSession.Commit(); } }
public void Read_Finds_Exising() { using (IDbSession dbSession = _dbSessionFactory.Create()) { IKeyedRepository <Guid, Person> repo = dbSession.CreateKeyedRepository <Guid, Person>(); foreach (Person person in _persons) { Person copy = repo.FindBy(person.Id); Assert.IsNotNull(copy); Assert.IsTrue(copy.Equals(person)); } dbSession.Commit(); } }
public void Update_Returns_True_And_Modifies_Exising() { using (IDbSession dbSession = _dbSessionFactory.Create()) { IKeyedRepository <Guid, Person> repo = dbSession.CreateKeyedRepository <Guid, Person>(); int countBefore = repo.All().Count(); Person person1Updated = repo.FindBy(_persons[0].Id); person1Updated.LastName = Guid.NewGuid().ToString(); Assert.IsTrue(repo.Update(person1Updated)); Assert.AreNotEqual(person1Updated, _persons[0]); Assert.IsTrue(repo.All().Count() == countBefore); dbSession.Commit(); } }
public ActionResult Save(PageModel model) { Page page = null; Role adminRole, memberRole, guestRole; bool isNew = model.Id == Guid.Empty; if (isNew) // if page is new create it { page = new Page(new Tenant(Portal.Tenant.Id)); if (model.ParentId.HasValue) { page.Parent = new Page(model.ParentId.Value); } } using (TransactionScope ts = new TransactionScope()) { // check if slug is unique var anotherPageWithSameSlug = _pageRepository.FindBy(p => p.Tenant.Id == Portal.Tenant.Id && p.Slug == model.Slug && p.Id != model.Id) != null; if (anotherPageWithSameSlug == true) { Alert(AlertType.warning, "Slug not unique", "Another page with this slug already exists."); return(View("Page", model)); } // if page exists, retrieve page and permissions if (!isNew) { page = (from p in _pageRepository.All() where p.Id == model.Id select p).FetchMany(p => p.Permissions).Single(); } // retrieve system roles adminRole = _roleRepository.FindBy(r => r.Tenant.Id == Portal.Tenant.Id && r.Name == "Administrator"); memberRole = _roleRepository.FindBy(r => r.Tenant.Id == Portal.Tenant.Id && r.Name == "Member"); guestRole = _roleRepository.FindBy(r => r.Tenant.Id == Portal.Tenant.Id && r.Name == "Guest"); ts.Complete(); } // update page model with new data Mapper.Map <PageModel, Page>(model, page); try { /* add / update page permissions */ var permissions = page.Permissions; // add / update admin permission PagePermission adminPermission = permissions.SingleOrDefault(p => p.Role.Name == adminRole.Name); if (adminPermission == null) { permissions.Add(new PagePermission(page, adminRole, true, true, true)); } else { adminPermission.SetPermissionRights(true, true, true); } // add / update member permission PagePermission memberPermission = permissions.SingleOrDefault(p => p.Role.Name == memberRole.Name); if (memberPermission == null && model.MembersVisible) { permissions.Add(new PagePermission(page, memberRole, true, false, false)); } else if (memberPermission != null) { memberPermission.SetPermissionRights(model.MembersVisible, false, false); } // add / update guest permission PagePermission guestPermission = permissions.SingleOrDefault(p => p.Role.Name == guestRole.Name); if (guestPermission == null && model.GuestsVisible) { permissions.Add(new PagePermission(page, guestRole, true, false, false)); } else if (guestPermission != null) { guestPermission.SetPermissionRights(model.GuestsVisible, false, false); } // save the page with permissions using (TransactionScope ts = new TransactionScope()) { _pageRepository.Save(page); //save the page foreach (var permission in permissions) { _pagePermissionRepository.Save(permission); } ts.Complete(); } model = Mapper.Map <PageModel>(page); //todo: reads permissions without transaction } catch { Alert(AlertType.danger, "Error", "Failed to create/update page."); return(View("Page", model)); } Alert(AlertType.success, "Success", "Page successfully created/updated."); return(RedirectToAction("edit", "page", new { slug = model.Slug })); }