I am reading the book "Pro ASP.NET MVC 2 by Steven Sanderson", and I am currently on Chapter 5. Great book up to this point! I have followed along the book in constructing my first MVC web app: the SportsStore application.
In the introduction the Author states this about the View:
Views are simple templates for converting the view model into a finished piece of HTML. They are allowed to contain basic, presentation-only logic, such as the ability to iterate over a list of objects to produce an HTML table row for each object, or the ability to hide or show a section of the page according to a flag on some object in the view model, but nothing more complicated than that.
However, having constructued this simple web app, on page 161 I see this statement in a \Views\Cart\Index.aspx:
<td align="right"> <%: Model.Cart.ComputeTotalValue().ToString("c") %> </td> The View uses Model Binding, and this Model is of type Store.WebUI.Models.CartIndexViewModel
public class CartIndexViewModel { public Cart Cart { get; set; } public string ReturnUrl { get; set; } } the Cart class is:
namespace Store.Domain.Entities { public class Cart { private List<CartLine> lines = new List<CartLine>(); public IList<CartLine> Lines { get { return lines.AsReadOnly(); } } public decimal ComputeTotalValue() { return lines.Sum(l => l.Product.Price * l.Quantity); } } } Ok, so the View statement "simply" output the total Cart value. But wait a minute...
When in the code I see a statement like Model.Cart.ComputeXXX_YYY() how can I determine "a priori" (i.e. without inspecting the code) whether this method performs a simple calculation that does not change the state of the model?
And if instead it were a complex procedure, involving the modification and updating of various entities of the Model?
After all, being able to directly call up the model, a "lazy programmer" could simply bypass the controller logic. What are the constraints that MVC put up against this coundut of work?
Is there any point that I did not understand or is this true?