MVC Design – How Dumb Should the View Be?

mvc

creating an MVC structure and pondering this…

My view has an Add button. Click it and we send off a request to the controller (using pub/sub so view doesn't really know who its talking to). Having done this the view disables its Add button until it receives a "data added" event from the controller (again pub/sub so doesn't really know who from). At this points it updates its list and re-enables the add button.

My question is this. Should the view contain this logic about enabling/disabling the add button or should the controller? My thoughts so far are perhaps the controller because it is up to the controller whether multiple adds can be handled at any one time or whether they need to be serialised in the method described. however, it is easier to implement the current scheme in the view as don't have to expose so many methods to control the view.

Is there a preferred way that is the most MVC compliant? Thanks 🙂

Best Answer

The Controller should be just as dumb as the View.

So in this case, we need to ask about responsibility.

Views are responsible for UI elements and displaying things. Controllers are responsible for connecting Views with Models and providing data for the View to display.

A Controller should be dumb about what the View has within it, buttons included. So the View should be responsible for enabling / disabling the button.

Keeping the Controller oblivious of the View allows you to more easily decouple the View from the Controller and have multiple types of Views reuse the same Controller.

Related Topic