ASP.NET MVC4 - 基本上我曾经在我的控制器中拥有我所有的业务逻辑(我试图将其放入域模型中)。但是我不太清楚我的所有业务逻辑是否应该放入域模型中,还是应该保留在控制器中?
例如,我得到了一个控制器动作,如下所示:
[HttpPost]
public ActionResult Payout(PayoutViewModel model)
{
if (ModelState.IsValid)
{
UserProfile user = PublicUtility.GetAccount(User.Identity.Name);
if (model.WithdrawAmount <= user.Balance)
{
user.Balance -= model.WithdrawAmount;
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
ViewBag.Message = "Successfully withdrew " + model.WithdrawAmount;
model.Balance = user.Balance;
model.WithdrawAmount = 0;
return View(model);
}
else
{
ViewBag.Message = "Not enough funds on your account";
return View(model);
}
}
else
{
return View(model);
}
}
现在是否应该将所有逻辑放入域模型中的方法中,使操作方法看起来像这样?
[HttpPost]
public ActionResult Payout(PayoutViewModel model)
{
var model = GetModel(model);
return View(model);
}
或者你会怎么做呢?