The controller must not have domain logic. How faithful should adhere to this principle?

Quote from page 49 of the Pro ASP.NET MVC book

Of course, you can place the logic domain in the controller, although you should not, simply because it seems like everything will work. Its easy to avoid this if you imagine that you have multiple user interfaces (for example, an ASP.NET MVC application plus a native iPhone application) in the same underlying business domain layer (and maybe one day you will be!) . With this in mind, it is clear that you are not trying to put domain logic in any of the layers of the user interface.

Why does he seem 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: How to avoid placing domain logic in the controller?

+3
1

if (cart.Lines.Count == 0) . :

[HttpPost]
public ActionResult CheckOut(CheckOutViewModel checkOut)
{
    if (ModelState.IsValid)
    {
        orderSubmitter.SubmitOrder(checkOut);
        return View("Completed");
    }
    return View(checkOut);
}
+1

Source: https://habr.com/ru/post/1785920/


All Articles