예제 #1
0
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                // 1-Force a exception to create a log using singleton instance
                //throw new Exception("Test log singleton");

                // 2-Cople code without factory pattern
                //if (product.CategoryId == 1)
                //{
                //	product.Bonus = 5;
                //	product.Tax = 2;
                //}
                //else if (product.CategoryId == 2)
                //{
                //	product.Bonus = 2;
                //	product.Tax = 5;
                //}
                //else if (product.CategoryId == 3)
                //{
                //	product.Bonus = 1;
                //	product.Tax = 7;
                //}

                // 3-Simple factory
                //ProductManagerFactory prodFactory = new ProductManagerFactory().CreateFactory;
                //IProductManager prodManager = prodFactory.GetProductManager(product.CategoryId);
                //product.Bonus = prodManager.GetBonus();
                //product.Tax = prodManager.GetTax();

                // 4-Factory Method
                BaseProductFactory prodFactory = new ProductManagerFactory().CreateFactory(product);
                prodFactory.ApplyBonusTax();


                db.Products.Add(product);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            var viewModel = new ProductViewModel
            {
                Categories = db.Categories.ToList()
            };

            return(View(viewModel));
        }