Esempio n. 1
0
        static void RunCQRS()
        {
            var serviceProvider = BuildServiceProvider();

            try
            {
                var commandDispatcher = new CommandDispatcher(serviceProvider);
                var queryDispatcher   = new QueryDispatcher(serviceProvider);

                //Add new Product
                var product = new AddNewProductCommand {
                    Id = Guid.NewGuid(), Name = "iPhone 11", Description = "Apple iphone 11"
                };
                commandDispatcher.Send(product);

                //Update Product Unit Price
                commandDispatcher.Send(new UpdateProductUnitPriceCommand {
                    Id = product.Id, UnitPrice = 800
                });

                //Update Product Current Stock
                commandDispatcher.Send(new UpdateProductCurrentStockCommand {
                    Id = product.Id, CurrentStock = 500
                });


                //Fine Products By Name
                var productsByName = queryDispatcher.Send(new GetProductsByNameQuery {
                    Name = "iPhone"
                });
                foreach (var item in productsByName)
                {
                    Console.WriteLine(item.ToString());
                }

                //Fine Products By Name
                var outOfStockProducts = queryDispatcher.Send(new FindOutOfStockProductsQuery());
                foreach (var item in outOfStockProducts)
                {
                    Console.WriteLine(item.ToString());
                }

                //Delete Product
                commandDispatcher.Send(new DeleteProductCommand {
                    Id = product.Id
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }