Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            IProductCommunicatorAbstractFactory productCommunicatorAbstractFactory = new InMemoryProductCommunicatorAbstractFactory();
            ProductService productService = new ProductService(productCommunicatorAbstractFactory);

            // Get product.
            try
            {
                Product product = productService.GetById(new Guid("34cb0113-2b54-4f86-98bc-a1a70d5817fd"));
                Console.WriteLine(product);
            }
            catch (Exception)
            {
                Console.WriteLine("Product not found.");
            }

            // Save new product.
            try
            {
                Product savedProduct = productService.Save(new Product()
                {
                    Name  = "Desk",
                    Price = 49.99
                });
                Console.WriteLine(savedProduct);
            }
            catch (Exception)
            {
                Console.WriteLine("Product saved failed.");
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            // Models:
            // Product, with Id, Name, Price

            // Service
            // ProductService

            // Utility
            // FileProductWriter
            // FileProductReader

            // AbstractFactory


            IProductCommunicatorAbstractFactory productCommunicatorAbstractFactory = new InMemoryProductCommunicatorAbstractFactory();
            IProductService productService = new ProductService(productCommunicatorAbstractFactory);


            // add product to json file
            Product product = new Product
            {
                Name  = "Long",
                Price = 123M
            };

            try
            {
                productService.AddProduct(product);
                Console.WriteLine($"Product added {product}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception happen {ex.Message}");
            }

            // get product info from json file
            try
            {
                product = productService.GetById(product.Id);
                Console.WriteLine($"Product queried {product}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception happen {ex.Message}");
            }
        }