Esempio n. 1
0
        public static void Main()
        {
            Signature.Sign("Command Pattern", "Author: Piotr Stefaniak", "Based on Pluralsight course");
            Console.WriteLine();

            var shoppingCartRepository = new ShoppingCartRepository();
            var productsRepository     = new ProductsRepository();

            var product = productsRepository.FindBy("SM7B");

            var addToCartCommand = new AddToCartCommand(shoppingCartRepository,
                                                        productsRepository,
                                                        product);

            var increaseQuantityCommand = new ChangeQuantityCommand(
                Operation.Increase,
                shoppingCartRepository,
                productsRepository,
                product);

            var manager = new CommandManager();

            manager.Invoke(addToCartCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);

            PrintCart(shoppingCartRepository);

            manager.Undo();

            PrintCart(shoppingCartRepository);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var shoppingCartRepository = new ShoppingCartRepository();
            var productsRepository     = new ProductsRepository();

            var product          = productsRepository.FindBy("SM7B");
            var addToCartCommand = new AddToCartCommand(shoppingCartRepository, productRepository, product);

            var increaseQuantityCommand = new ChangeQuantityCommand(
                ChangeQuantityCommand.Operation.Increase,
                shoppingCartRepository,
                product);

            var manager = new CommandManager();

            manager.Invoke(addToCartCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);
            manager.Invoke(increaseQuantityCommand);


            shoppingCartRepository.Add(product);
            shoppingCartRepository.IncreaseQuantity(product.ArticleId);
            shoppingCartRepository.IncreaseQuantity(product.ArticleId);
            shoppingCartRepository.IncreaseQuantity(product.ArticleId);
            shoppingCartRepository.IncreaseQuantity(product.ArticleId);

            PrintCart(shoppingCartRepositiry);
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Command Pattern Example");

            var shoppingCartRepository = new ShoppingCartRepository();
            var productRepository      = new ProductRepository();

            var product = productRepository.Find(1);

            var addToCartCommand = new AddToCartCommand(shoppingCartRepository,
                                                        productRepository,
                                                        product);

            var increaseQuantityCommand = new ChangeQuantityCommand(ChangeQuantityCommand.Operation.Increase,
                                                                    product,
                                                                    shoppingCartRepository,
                                                                    productRepository);

            var command = new Command();

            Console.WriteLine("Adding item to shopping cart...");
            command.Invoke(addToCartCommand);
            var addedLineItem = shoppingCartRepository.Get(product.Id);

            Console.WriteLine($"Item added to shopping cart : Name : {addedLineItem.Product.Name} Quantity : {addedLineItem.Quantity}");

            Console.WriteLine("Increasing quantity...");
            command.Invoke(increaseQuantityCommand);
            addedLineItem = shoppingCartRepository.Get(product.Id);
            Console.WriteLine($"Item added to shopping cart : Name : {addedLineItem.Product.Name} Quantity : {addedLineItem.Quantity}");

            Console.WriteLine("Increasing quantity...");
            command.Invoke(increaseQuantityCommand);
            addedLineItem = shoppingCartRepository.Get(product.Id);
            Console.WriteLine($"Item added to shopping cart : Name : {addedLineItem.Product.Name} Quantity : {addedLineItem.Quantity}");

            Console.WriteLine("Increasing quantity...");
            command.Invoke(increaseQuantityCommand);
            addedLineItem = shoppingCartRepository.Get(product.Id);
            Console.WriteLine($"Item added to shopping cart : Name : {addedLineItem.Product.Name} Quantity : {addedLineItem.Quantity}");

            PrintShoppingCart(shoppingCartRepository);

            Console.WriteLine("Undo all actions");
            command.Undo();

            PrintShoppingCart(shoppingCartRepository);
        }