In 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 appropriate at some point in pressure.
Just a far-fetched example, if the application does not allow a negative order, where to put the change in quantity to 1? If you follow the principle that the domain logic should not be placed in the controller, it is, of course, impractical to use:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult PlaceOrder(Order order)
{
if (ModelState.IsValid)
{
order.Submit();
return View("Thanks", order);
}
else
{
if (order.Quantity <= 0)
{
ModelState.Remove("Quantity");
order.Quantity = 1;
}
return View(order);
}
}
Thus, the following code is the correct code that adheres to the principle of MVC, i.e. it follows the separation of problems, if in the domain logic you should not see your code in the controllers. So this is how I tried to put the domain logic in Model:
public class Order : IDataErrorInfo
{
public int OrderId { set; get; }
public int ProductId { set; get; }
public int Quantity { set; get; }
public string Error { get { return null; } }
public string this[string propName]
{
get
{
if (propName == "Quantity" && Quantity <= 0)
{
Quantity = 1;
return "0 or negative quantity not allowed, changed it to 1";
}
else
return null;
}
}
}
Controller (without domain logic):
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult PlaceOrder(Order order)
{
if (ModelState.IsValid)
{
order.Submit();
return View("Thanks", order);
}
else
{
return View(order);
}
}
- () ModelState, , .
, , ?