Example #1
0
        /// <summary>
        /// Save product to DB.
        /// </summary>
        /// <param name="productDto">Product DTO to save to DB.</param>
        public void AddProduct(ProductDataContract product)
        {
            if (product == null)
            {
                throw new ArgumentNullException(nameof(product));
            }

            using (var service = ProductsServiceFactory.CreateWithDbRepository())
            {
                service.SaveProduct(ToDto(product));
            }
        }
Example #2
0
        /// <summary>
        /// Save several products to DB.
        /// </summary>
        /// <param name="productDtos">List of product DTOs to save to DB.</param>
        public void AddSeveralProducts(IEnumerable <ProductDataContract> products)
        {
            if (products == null)
            {
                throw new ArgumentNullException(nameof(products));
            }

            using (var service = ProductsServiceFactory.CreateWithDbRepository())
            {
                foreach (var product in products)
                {
                    service.SaveProduct(ToDto(product));
                }
            }
        }
 private static IEnumerable <ProductViewModel> GetProductsFromFile()
 {
     using (var service =
                ProductsServiceFactory.CreateWithFileRepository(ConfigurationManager.AppSettings["FileStoragePath"]))
     {
         var productsFromFile = service.GetAllProducts();
         return(productsFromFile.Select(p => new ProductViewModel
         {
             Id = p.Id,
             Name = p.Name,
             Price = p.Price,
             Amount = p.Amount
         }));
     }
 }
Example #4
0
        private void AddProduct(object sender, RoutedEventArgs e)
        {
            var button = ((Button)sender);

            button.IsEnabled = false;

            tblResult.Text = string.Empty;

            bool   isSuccess    = false;
            string errorMessage = string.Empty;

            // First try to call WCF-service to add product to the DB.
            // If it will fail, then at least we try to save product to the file.
            try
            {
                _wcfClient.AddProduct(_product.ToDataContract());
                isSuccess = true;
            }
            catch (Exception ex)
            {
                errorMessage += $"An error has occured while calling the WCF service: {ex.Message}{Environment.NewLine}";
            }

            try
            {
                // Save product to file.
                using (var service =
                           ProductsServiceFactory.CreateWithFileRepository(ConfigurationManager.AppSettings["FileStoragePath"]))
                {
                    service.SaveProduct(_product.ToDto());
                }
                isSuccess = true;
            }
            catch (Exception ex)
            {
                errorMessage += $"An error has occured while saving to file: {ex.Message}{Environment.NewLine}";
            }

            if (isSuccess)
            {
                var message = "Product successfully added.";

                // If product was succesfully saved (either to DB or to file), but we get some error,
                // then add errorMessag to output.
                if (!string.IsNullOrEmpty(errorMessage))
                {
                    message += $" BUT{Environment.NewLine}{errorMessage}";
                }

                tblResult.Text       = message;
                tblResult.Foreground = new SolidColorBrush(Colors.Green);
            }
            else
            {
                tblResult.Text       = errorMessage;
                tblResult.Foreground = new SolidColorBrush(Colors.Red);
            }

            // Clear form.
            UpdateBinding();

            button.IsEnabled = true;
        }