/// <summary> /// Update Product /// </summary> /// <param name="product"></param> /// <param name="transaction"></param> public void UpdateProduct(Product product, out TransactionalInformation transaction) { transaction = new TransactionalInformation(); try { ProductBusinessRules productBusinessRules = new ProductBusinessRules(); ValidationResult results = productBusinessRules.Validate(product); bool validationSucceeded = results.IsValid; IList <ValidationFailure> failures = results.Errors; if (validationSucceeded == false) { transaction = ValidationErrors.PopulateValidationErrors(failures); return; } _productDataService.CreateSession(); _productDataService.BeginTransaction(); Product existingProduct = _productDataService.GetProduct(product.ProductID); existingProduct.ProductName = product.ProductName; existingProduct.QuantityPerUnit = product.QuantityPerUnit; existingProduct.UnitPrice = product.UnitPrice; _productDataService.UpdateProduct(product); _productDataService.CommitTransaction(true); transaction.ReturnStatus = true; transaction.ReturnMessage.Add("Product was successfully updated."); } catch (Exception ex) { string errorMessage = ex.Message; transaction.ReturnMessage.Add(errorMessage); transaction.ReturnStatus = false; } finally { _productDataService.CloseSession(); } }
/// <summary> /// Create Product /// </summary> /// <param name="product"></param> /// <param name="transaction"></param> /// <returns></returns> public Product CreateProduct(Product product, out TransactionalInformation transaction) { transaction = new TransactionalInformation(); try { ProductBusinessRules productBusinessRules = new ProductBusinessRules(); ValidationResult results = productBusinessRules.Validate(product); bool validationSucceeded = results.IsValid; IList <ValidationFailure> failures = results.Errors; if (validationSucceeded == false) { transaction = ValidationErrors.PopulateValidationErrors(failures); return(product); } _productDataService.CreateSession(); _productDataService.BeginTransaction(); _productDataService.CreateProduct(product); _productDataService.CommitTransaction(true); transaction.ReturnStatus = true; transaction.ReturnMessage.Add("Product successfully created."); } catch (Exception ex) { string errorMessage = ex.Message; transaction.ReturnMessage.Add(errorMessage); transaction.ReturnStatus = false; } finally { _productDataService.CloseSession(); } return(product); }