public string AssignProductsAsync(List<String> productIds, string productSetId) { Guid entityId; if (!Guid.TryParse(productSetId, out entityId) || entityId == Guid.Empty) return "false"; using (var uow = new UnitOfWork(UnitOfWorkScopeType.New)) { var productSetEntity = Repository.Data.Get<ProductSet>(Guid.Parse(productSetId)); if (productSetEntity.Products == null) { productSetEntity.Products = new List<Product>(); } foreach (var productId in productIds.Distinct()) { var product = Repository.Data.Get<Product>(Guid.Parse(productId)); product.ProductSets.Add(productSetEntity); Repository.Data.Save(product); } uow.Commit(); } return "true"; }
public void FileContentIsntStoredIfTransactionWasRolledBack() { string storagePath = string.Empty; using (var unitOfWork = new UnitOfWork()) { var folder = new Folder { Name = "Test folder", }; Repository.Data.Save(folder); var file = new File { //Folder = folder, DisplayName = "Test file", Name = "TestFile.txt", ContentType = FileType.txt }; Stream stream = new MemoryStream(); byte[] buffer = Guid.NewGuid().ToByteArray(); stream.Write(buffer, 0, buffer.Length); file.FileStream = new Lazy<Stream>(() => stream); Repository.Data.Save(file); storagePath = file.StoragePath; unitOfWork.Rollback(); } string path = string.Format(@"{0}\{1}", ConfiguredStorageDirectory, storagePath); Assert.False(System.IO.File.Exists(path)); }
public ActionResult InsertProductAsync() { var productModel = new ProductModel (); if (TryUpdateModel (productModel)) { using (var uow = new UnitOfWork(UnitOfWorkScopeType.New)) { var product = productModel.GetProduct(); product.CreateDate = DateTime.Now; product.UpdateDate = DateTime.Now; //var productProperty = new ProductProperty { // Brend = productModel.ProductProperty.Brend, // Capacity = productModel.ProductProperty.Capacity, // Country = productModel.ProductProperty.Country, // Weight = productModel.ProductProperty.Weight //}; Repository.Data.Save(product.ProductProperty); //product.ProductProperty = productProperty; Repository.Data.Save(product); uow.Commit(); } } return GetProductsAsync(new GridCommand { Page = 1, PageSize = 20 }); }
public Page CopyPasteAsTop(Guid pageToCopyId, Guid webSiteToPasteInId, bool recursive) { if (pageToCopyId == Guid.Empty) throw new ArgumentOutOfRangeException("pageToCopyId"); if (webSiteToPasteInId == Guid.Empty) throw new ArgumentOutOfRangeException("webSiteToPasteInId"); var pageToCopy = Repository.Data.Get<Page>(pageToCopyId); if (pageToCopy == null) throw new NullReferenceException(string.Format("Page with Id={0} doesn't exist", pageToCopyId)); var webSiteToPasteIn = Repository.Data.Get<WebSite>(webSiteToPasteInId); if (webSiteToPasteIn == null) throw new NullReferenceException(string.Format("WebSite with Id={0} doesn't exist", webSiteToPasteInId)); var copy = (Page) pageToCopy.Clone(); copy.Parent = null; copy.WebSiteId = webSiteToPasteInId; using (var unit = new UnitOfWork()) { Repository.Data.Save(copy); if (recursive) { CopyPasteRecursive(pageToCopy, copy); } unit.Commit(); } return copy; }
public void NestedUnitsCombinatedTest() { var entity = new SimpleEntity { Name = "UnitTest" }; var entity2 = new SimpleEntity { Name = "UnitTest2" }; var entity3 = new SimpleEntity { Name = "UnitTest3" }; var entity4 = new SimpleEntity { Name = "UnitTest4" }; using (var unit = new UnitOfWork()) { Repository.Data.Save(entity); using (var unit2 = new UnitOfWork()) { Repository.Data.Save(entity2); using (var unit3 = new UnitOfWork()) { Repository.Data.Save(entity3); unit3.Commit(); } using (var unit4 = new UnitOfWork(UnitOfWorkScopeType.New)) { Repository.Data.Save(entity4); unit4.Commit(); } Assert.Throws<UnitOfWorkException>(() => unit2.Rollback()); } Assert.Throws<UnitOfWorkException>(() => unit.Commit()); } Repository.Data.Cache.Clear(typeof (SimpleEntity)); ConversationHelper.ReOpen(); entity = Repository.Data.Get<SimpleEntity>(entity.Id); entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id); entity3 = Repository.Data.Get<SimpleEntity>(entity3.Id); entity4 = Repository.Data.Get<SimpleEntity>(entity4.Id); Assert.Null(entity); Assert.Null(entity2); Assert.Null(entity3); Assert.NotNull(entity4); }
public void AssignProductsToCategory(List<String> productIds, String category) { using (var uow = new UnitOfWork(UnitOfWorkScopeType.New)) { var categoryEntity = Repository.Data.Get<Category>(Guid.Parse(category)); foreach (var productId in productIds.Distinct()) { Product product = Repository.Data.Get<Product>(Guid.Parse(productId)); product.Category = categoryEntity; Repository.Data.Save(product); } uow.Commit(); } }
public void DeleteProductSet(string productSetId) { using (var uow = new UnitOfWork(UnitOfWorkScopeType.New)) { var productSet = Repository.Data.Get<ProductSet>(Guid.Parse(productSetId)); foreach (var product in productSet.Products) { product.ProductSets.Remove(productSet); Repository.Data.Save(product); } productSet.Products.Clear(); Repository.Data.Delete(productSet); uow.Commit(); } }
public void NestedUnitsWithInnerScopeRollbackAndCommitActionTest() { var entity = new SimpleEntity { Name = "UnitTest" }; var entity2 = new SimpleEntity { Name = "UnitTest2" }; using (var unit = new UnitOfWork()) { UnitOfWork.Current.PostCommitActions.Add(() => { }); UnitOfWork.Current.PostRollbackActions.Add(() => { }); Repository.Data.Save(entity); Assert.Equal(1, UnitOfWork.Current.PostCommitActions.Count); Assert.Equal(1, UnitOfWork.Current.PostRollbackActions.Count); using (var unit2 = new UnitOfWork(UnitOfWorkScopeType.New)) { UnitOfWork.Current.PostCommitActions.Add(() => { }); UnitOfWork.Current.PostRollbackActions.Add(() => { }); Repository.Data.Save(entity2); unit2.Rollback(); } Assert.Equal(1, UnitOfWork.Current.PostCommitActions.Count); Assert.Equal(1, UnitOfWork.Current.PostRollbackActions.Count); unit.Commit(); } Repository.Data.Cache.Clear(typeof (SimpleEntity)); ConversationHelper.ReOpen(); entity = Repository.Data.Get<SimpleEntity>(entity.Id); entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id); Assert.NotNull(entity); Assert.Null(entity2); }
public void NestedUnitsWithSameScopeRollbackBothTest() { var entity = new SimpleEntity { Name = "UnitTest" }; var entity2 = new SimpleEntity { Name = "UnitTest2" }; using (var unit = new UnitOfWork()) { Repository.Data.Save(entity); using (var unit2 = new UnitOfWork()) { Repository.Data.Save(entity2); Assert.Throws<UnitOfWorkException>(() => unit2.Rollback()); } unit.Rollback(); } Repository.Data.Cache.Clear(typeof (SimpleEntity)); ConversationHelper.ReOpen(); entity = Repository.Data.Get<SimpleEntity>(entity.Id); entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id); Assert.Null(entity); Assert.Null(entity2); }
public void NestedUnitsWithInnerScopeWithoutException() { var entity = new SimpleEntity { Name = "UnitTest" }; var entity2 = new SimpleEntity { Name = "UnitTest2" }; using (var unit = new UnitOfWork()) { Repository.Data.Save(entity); using (var unit2 = new UnitOfWork(UnitOfWorkScopeType.New)) { Repository.Data.Save(entity2); unit2.Rollback(); // will not throw exception because this is own transaction } // user can commit the outer UOW unit.Commit(); } }
public void SaveOrder() { var orderModel = HttpContext.Current.Session[WebStroreResource.CART] as OrderModel; var user = WebStoreSecurity.GetLoggedInUser(); var order = new Litium.Domain.Entities.ECommerce.Order { CreateDate = orderModel.CreateDate, OrderNumber = orderModel.OrderNumber, Customer = user, OrderState = State.Created, DeliveryMethod = orderModel.DeliveryMethod, Description = orderModel.DeliveryDescription, OrderSumma = orderModel.OrderSumma, OrderTotal = orderModel.OrderTotal }; var products = new List<OrderProduct>(); foreach (var cartItem in orderModel.OrderRows) { var orderRow = new OrderProduct(); orderRow.CopyFromModel(cartItem); products.Add(orderRow); } using (var uow = new UnitOfWork()) { Repository.Data.Save(products); order.OrderProducts = products; Repository.Data.Save(order); uow.Commit(); } }
public Page CutAndPaste(Guid pageToCutId, Guid pageToPasteInId) { if (pageToCutId == Guid.Empty) throw new ArgumentOutOfRangeException("pageToCutId"); if (pageToPasteInId == Guid.Empty) throw new ArgumentOutOfRangeException("pageToPasteInId"); var pageToCut = Repository.Data.Get<Page>(pageToCutId); if (pageToCut == null) throw new NullReferenceException(string.Format("Page with Id={0} doesn't exist", pageToCutId)); var pageToPasteIn = Repository.Data.Get<Page>(pageToPasteInId); if (pageToPasteIn == null) throw new NullReferenceException(string.Format("Page with Id={0} doesn't exist", pageToPasteInId)); var parent = pageToPasteIn.Parent; while (parent != null) { if (parent.Id == pageToCutId) throw new ValidationArgumentException(pageToCut, "MoveToChildException"); parent = parent.Parent; } using (var unit = new UnitOfWork()) { pageToCut.Parent = pageToPasteIn; var changeWebSite = pageToCut.WebSiteId != pageToPasteIn.WebSiteId; if (changeWebSite) { pageToCut.WebSiteId = pageToPasteIn.WebSiteId; Repository.Data.Save(pageToCut); ChangeWebSiteRecursive(pageToCut); } else { Repository.Data.Save(pageToCut); } unit.Commit(); } return pageToCut; }
public void NestedUnitsWithNewInnerScopeRollbackAndCommitTest() { var entity = new SimpleEntity { Name = "UnitTest" }; var entity2 = new SimpleEntity { Name = "UnitTest2" }; using (var unit = new UnitOfWork()) { Repository.Data.Save(entity); using (var unit2 = new UnitOfWork(UnitOfWorkScopeType.New)) { Repository.Data.Save(entity2); unit2.Rollback(); } unit.Commit(); } Repository.Data.Cache.Clear(typeof (SimpleEntity)); ConversationHelper.ReOpen(); entity = Repository.Data.Get<SimpleEntity>(entity.Id); entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id); Assert.NotNull(entity); Assert.Null(entity2); }
public Page CutAndPasteAsTop(Guid pageToCutId, Guid webSiteToPasteInId) { if (pageToCutId == Guid.Empty) throw new ArgumentOutOfRangeException("pageToCutId"); if (webSiteToPasteInId == Guid.Empty) throw new ArgumentOutOfRangeException("webSiteToPasteInId"); var pageToCut = Repository.Data.Get<Page>(pageToCutId); if (pageToCut == null) throw new NullReferenceException(string.Format("Page with Id={0} doesn't exist", pageToCutId)); var webSiteToPasteIn = Repository.Data.Get<WebSite>(webSiteToPasteInId); if (webSiteToPasteIn == null) throw new NullReferenceException(string.Format("WebSite with Id={0} doesn't exist", webSiteToPasteInId)); using (var unit = new UnitOfWork()) { pageToCut.Parent = null; var changeWebSite = pageToCut.WebSiteId != webSiteToPasteInId; if (changeWebSite) { pageToCut.WebSiteId = webSiteToPasteInId; Repository.Data.Save(pageToCut); ChangeWebSiteRecursive(pageToCut); } else { Repository.Data.Save(pageToCut); } unit.Commit(); } return pageToCut; }
public void UnitOfWorkRollbackTest() { var entity = new SimpleEntity { Name = "UnitTest" }; using (var unit = new UnitOfWork()) { Repository.Data.Save(entity); unit.Rollback(); } Repository.Data.Cache.Clear(entity); ConversationHelper.ReOpen(); entity = Repository.Data.Get<SimpleEntity>(entity.Id); Assert.Null(entity); }
public void UnitOfWorkRollBackAfterRollbackTest() { var entity = new SimpleEntity { Name = "UnitTest" }; var entity2 = new SimpleEntity { Name = "UnitTest2" }; using (var unit = new UnitOfWork()) { //Some external method Repository.Data.Save(entity); UnitOfWork.Current.Rollback(); Assert.Throws<UnitOfWorkException>(() => Repository.Data.Save(entity2)); unit.Rollback(); } Repository.Data.Cache.Clear(entity); ConversationHelper.ReOpen(); entity = Repository.Data.Get<SimpleEntity>(entity.Id); Assert.Null(entity); entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id); Assert.Null(entity2); }
public void ScopeWithoutException() { var entity = new SimpleEntity { Name = "UnitTest" }; using (var unit = new UnitOfWork()) { Repository.Data.Save(entity); unit.Rollback(); // will not throw exception } Repository.Data.Cache.Clear(typeof (SimpleEntity)); ConversationHelper.ReOpen(); entity = Repository.Data.Get<SimpleEntity>(entity.Id); Assert.Null(entity); }
public ActionResult UpdateProductAsync() { var productModel = new ProductModel(); if (TryUpdateModel (productModel)) { using (var uow = new UnitOfWork()) { var product = Repository.Data.Get<Product>(productModel.Id); product.CopyFrom(productModel, true); product.UpdateDate = DateTime.Now; Repository.Data.Save(product); uow.Commit(); } } ViewBag.Message = WebStroreResource.ProductUpdatedSuccesfull; return GetProductsAsync(new GridCommand { Page = 1, PageSize = 20 }); }
public void NestedUnitsWithSameScopeRollbackAndCommitActionTest() { var entity = new SimpleEntity { Name = "UnitTest" }; var entity2 = new SimpleEntity { Name = "UnitTest2" }; using (var unit = new UnitOfWork()) { int onCommitActionsCount; int onRollBackActionsCount; UnitOfWork.Current.PostCommitActions.Add(() => { }); UnitOfWork.Current.PostRollbackActions.Add(() => { }); Repository.Data.Save(entity); Assert.Equal(1, UnitOfWork.Current.PostCommitActions.Count); Assert.Equal(1, UnitOfWork.Current.PostRollbackActions.Count); using (var unit2 = new UnitOfWork()) { UnitOfWork.Current.PostCommitActions.Add(() => { }); UnitOfWork.Current.PostRollbackActions.Add(() => { }); onCommitActionsCount = UnitOfWork.Current.PostCommitActions.Count; onRollBackActionsCount = UnitOfWork.Current.PostRollbackActions.Count; Repository.Data.Save(entity2); Assert.Throws<UnitOfWorkException>(() => unit2.Rollback()); } Assert.Equal(2, onCommitActionsCount); Assert.Equal(2, onRollBackActionsCount); Assert.Throws<UnitOfWorkException>(() => unit.Commit()); } Repository.Data.Cache.Clear(typeof (SimpleEntity)); ConversationHelper.ReOpen(); entity = Repository.Data.Get<SimpleEntity>(entity.Id); entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id); Assert.Null(entity); Assert.Null(entity2); }
public void NestedUnitsWithSameScopeOuterDisposeTest() { var entity = new SimpleEntity { Name = "UnitTest" }; var entity2 = new SimpleEntity { Name = "UnitTest2" }; using (new UnitOfWork()) { Repository.Data.Save(entity); using (var unit2 = new UnitOfWork()) { Repository.Data.Save(entity2); unit2.Commit(); } } Repository.Data.Cache.Clear(typeof (SimpleEntity)); ConversationHelper.ReOpen(); entity = Repository.Data.Get<SimpleEntity>(entity.Id); entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id); Assert.Null(entity); Assert.Null(entity2); }
public void NestedUnitsWithSameScopeInnerException() { var entity = new SimpleEntity { Name = "UnitTest" }; var entity2 = new SimpleEntity { Name = "UnitTest2" }; using (var unit = new UnitOfWork()) { Repository.Data.Save(entity); using (var unit2 = new UnitOfWork()) { Repository.Data.Save(entity2); Assert.Throws<UnitOfWorkException>(() => unit2.Rollback()); // will throw exception because this is not the owner of the UOW } // user will not come to this point and try to commit transaction // else it should throw exception Assert.Throws<UnitOfWorkException>(() => unit.Commit()); } Repository.Data.Cache.Clear(typeof (SimpleEntity)); ConversationHelper.ReOpen(); entity = Repository.Data.Get<SimpleEntity>(entity.Id); entity2 = Repository.Data.Get<SimpleEntity>(entity2.Id); Assert.Null(entity); Assert.Null(entity2); }