示例#1
0
        public ActionResult Add(string app)
        {
            Applience        newApp;
            ApplienceFactory appCreate;

            switch (app)
            {
            default:
                appCreate = new LampCreator();
                newApp    = appCreate.CreateApplience();
                db.Lamps.Add((Lamp)newApp);
                break;

            case "conditioner":
                appCreate = new ConditionerCreator();
                newApp    = appCreate.CreateApplience();
                db.Conditioneres.Add((Conditioner)newApp);
                break;

            case "microwave":
                appCreate = new MicrowaveCreator();
                newApp    = appCreate.CreateApplience();
                db.Microwaves.Add((Microwave)newApp);
                break;

            case "tv":
                appCreate = new TVCreator();
                newApp    = appCreate.CreateApplience();
                db.TVs.Add((TV)newApp);
                break;
            }
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        protected override void Seed(ApplienceContext context)
        {
            ApplienceFactory app = new LampCreator();

            context.Lamps.Add((Lamp)app.CreateApplience());

            app = new ConditionerCreator();

            context.Conditioneres.Add((Conditioner)app.CreateApplience());
            app = new MicrowaveCreator();

            context.Microwaves.Add((Microwave)app.CreateApplience());
            app = new TVCreator();
            context.TVs.Add((TV)app.CreateApplience());

            context.SaveChanges();
        }
示例#3
0
        public ActionResult Index()
        {
            IDictionary <int, Applience> applienceDictionary;

            if (Session["Apps"] == null)
            {
                ApplienceFactory app = new LampCreator();
                applienceDictionary = new SortedDictionary <int, Applience>();
                applienceDictionary.Add(1, app.CreateApplience());
                app = new ConditionerCreator();
                applienceDictionary.Add(2, app.CreateApplience());
                app = new MicrowaveCreator();
                applienceDictionary.Add(3, app.CreateApplience());
                app = new TVCreator();
                applienceDictionary.Add(4, app.CreateApplience());

                Session["Apps"]   = applienceDictionary;
                Session["NextId"] = 5;
            }
            else
            {
                applienceDictionary = (SortedDictionary <int, Applience>)Session["Apps"];
            }

            SelectListItem[] appList = new SelectListItem[4];
            appList[0] = new SelectListItem {
                Text = "Lamp", Value = "lamp", Selected = true
            };
            appList[1] = new SelectListItem {
                Text = "Conditioner", Value = "conditioner"
            };
            appList[2] = new SelectListItem {
                Text = "Microwave", Value = "microwave"
            };
            appList[3] = new SelectListItem {
                Text = "TV", Value = "tv"
            };
            ViewBag.AppList = appList;

            return(View(applienceDictionary));
        }
示例#4
0
        public ActionResult Add(string app)
        {
            Applience        newApp;
            ApplienceFactory appCreate;

            switch (app)
            {
            default:
                appCreate = new LampCreator();
                newApp    = appCreate.CreateApplience();
                break;

            case "conditioner":
                appCreate = new ConditionerCreator();
                newApp    = appCreate.CreateApplience();
                break;

            case "microwave":
                appCreate = new MicrowaveCreator();
                newApp    = appCreate.CreateApplience();
                break;

            case "tv":
                appCreate = new TVCreator();
                newApp    = appCreate.CreateApplience();
                break;
            }

            int id = (int)Session["NextId"];
            IDictionary <int, Applience> applienceDictionary = (SortedDictionary <int, Applience>)Session["Apps"];

            applienceDictionary.Add(id, newApp);
            id++;
            Session["NextId"] = id;

            return(RedirectToAction("Index"));
        }