0

I'm just getting into mvc 4 (and mvc in general) and am just wondering is this action code ok or should it be stripped down again?

[HttpPost]
        public ActionResult Index(DashboardViewModel dbModel)
        {
            //retrieve latest resident order
            var residentOrder = db.ResidentOrders.GetById(dbModel.ResidentOrderID);

            if (residentOrder == null)
            {
                var order = db.Orders.GetById(dbModel.OrderID);

                var user = db.Users.GetUserByUsername(User.Identity.Name);

                residentOrder = new ResidentOrder()
                {
                    CreatedDate=DateTime.Now,
                    LastUpdateDate = DateTime.Now,
                    Litres=0,
                    Customer = user
                };

                order.ResidentOrders.Add(residentOrder);

                db.Commit();
            }

            //check to see if value has changed
            if (!dbModel.ResidentLitresOrdered.Equals(residentOrder.Litres))
            {
                //get new ordered value
                residentOrder.Litres = dbModel.ResidentLitresOrdered;

                db.Commit();

                //send an email just to notify in writing of the change.
                SendOwnOrderQtyUpdateNotification();
            }

            return View(dbModel);
        }

Basically if a resident order doesnt exist then we create one, this is the only place in the system where this would need to happen.

Should I still be stripping that code out into my repositories?

db is my IUnitOfWork btw

4

1 回答 1

1

我建议您创建一个“存储库”来隐藏控制器操作的详细信息。“Upsert”方法可以清晰而优雅地实现,对控制器隐藏细节。

于 2013-06-17T09:03:46.050 回答