views:

38

answers:

2

Hi All,

Here's an excerpt from a book I'm reading about application design with MVC:

Ideally, the view is so simple and logic-free as to need virtually no testing. Users (and developers before users) can reasonably test the view by simply looking at the pixels on the screen. Anything else beyond pure graphical rendering should ideally be taken out of the view and placed in the controller and model. This includes, for example, the logic that determines whether a certain button should be enabled or grayed out at some point.

what does the bold statement mean to you? what would this look like?

thanks, rod.

A: 

This is what that bold statement means to me:

  • The controller is going to be full of nested if statements
  • The model (or viewmodel) is going to be full of properties to help render the page specific ways, making the object graphs difficult to maintain.

While I think the analysis should not be made in the view, the condition should be set so the button only has to think - show or not show.

eg. only show the examinee details button if the examinee is male.

You either create a viewmodel property ShowExamineeDetails. The view will check if this is ture or not.

the ShowExamineeDetails = is examinee Male?

code should be in the controller.

As for testing, I am yet to find an app that "...needs virtually no testing..."

Russell
+1  A: 

The logic that decides when to enable or disable the button should be residing in the controller and simply calls a method e.g view.EnableContinueButton() to enable/disable the button on the page.

The actual code to enable/disable the button on the page itself should be implemented in the view e.g a EnableContinueButton() method then which calls something like btnContinue.Enable().

Simply put, the view should concern itself with the UI details (show/hide/enable/disable UI elements) and leave all business logic processing to the controller. In this way, the controller does not need to concern itself with the UI elements and the view works independently of the actual business logic.

e.g in the Controller,

public void ProcessOrder()
{
   if (!controller.ValidateOrder(model.OrderNo))
       view.EnableContinueButton(false);
   else
       // Process the order
       ...
}

and in the View

public void EnableContinueButton(bool enabled)
{
    btnContinueButton.Enabled = enabled;
}

Frankly I haven't got much experience in MVC (implemented in one project a while back) but I hope the logic separation between controller and view is clear enough.

Mr Roys
Could you please illustrate the different code snippets a little more...I apologize for not getting it right away. rod. I do get the "why" though, thanks.
rod
I have added a code sample, not exactly ASP.NET-specific but I hope that it's still useful.
Mr Roys
thanks for the help and patience
rod