Controller should not have domain logic. How faithful should one adhere to this tenet?
- by Hao
Quoting from page 49 of Pro ASP.NET MVC book
It is certainly possible to put domain
logic into a controller, even though
you shouldn’t, just because it seems
like it will work anyway. It’s easy to
avoid this if you imagine that you
have multiple UI technologies (e.g.,
an ASP.NET MVC application plus a
native iPhone application) operating
on the same underlying business domain
layer (and maybe one day you will!).
With this in mind, it’s clear that you
don’t want to put domain logic into
any of the UI layers.
Why he seems to contradict himself on page 172?
[HttpPost]
public ActionResult CheckOut(Cart cart, ShippingDetails shippingDetails)
{
// Empty carts can't be checked out
if (cart.Lines.Count == 0)
ModelState.AddModelError("Cart", "Sorry, your cart is empty!");
if (ModelState.IsValid)
{
orderSubmitter.SubmitOrder(cart, shippingDetails);
cart.Clear();
return View("Completed");
}
else // Something was invalid
return View(shippingDetails);
}
Related to: How to avoid placing domain logic in controller?