开发者

logic of button to be disabled or not in mvc

开发者 https://www.devze.com 2022-12-27 15:37 出处:网络
Here\'s an excerpt from a book I\'m reading about application design with MVC: Ideally, the view is so simple and

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.


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.


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..."

0

精彩评论

暂无评论...
验证码 换一张
取 消