0

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?

3
  • I suggest looking up ViewModels. They will hopefully get rid of lot of your gripes with the MVC framework. They are an integral part of it but probably a later topic in the book I'd assume. Commented Oct 9, 2013 at 9:13
  • 1
    Ok, no I understand that. In fact CartIndexViewModel is a ViewModel. Simplyfing, my point is that the View "shouldn't be allowed" to access directly the Model (or ViewModel), because you can't "a priori" know what the ComputeXXX function does! Commented Oct 9, 2013 at 10:46
  • 1
    Sorry I overlooked that.I would NOT have Cart.ComputeXXX on my Cart Model, I would move this to CartService.GetCartXXXValue(Cart cart); as an example. You can then use this CartService in your controller to build your ViewModels. With this method your View should never be calling functions which change the Model as the data on it will be purely for display. Commented Oct 9, 2013 at 11:10

1 Answer 1

1

I don't like methods on models at all. In this case I would write a readonly property TotalValue which does that same calculation. Properties should not have side-effects so it should be safe to use in a view.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.