Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC: should view talk with model directly?

earlier, many developer hold the opinion that view should not communicate with model directly, like most framework do.

and then, this opinion seems to be wrong, I find some articles, these articles say that view can communicate with model directly.

http://r.je/views-are-not-templates.html
http://www.tonymarston.net/php-mysql/model-view-controller.html
Model, View, Controller confusion
and
How should a model be structured in MVC?

most of these articles quotes a block from wikipedia, Model–view–controller, the quotes is:

A view queries the model in order to generate an appropriate user interface (for example the view lists the shopping cart's contents). The view gets its own data from the model. In some implementations, the controller may issue a general instruction to the view to render itself. In others, the view is automatically notified by the model of changes in state (Observer) that require a screen update.

ah, it's from wikipedia, such a authoritative site, it must be right!

but now, when I open the wiki link of MVC http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller, the page has be edited on September 14 this year(the year 2013), and the sentence above has gone.

the new definition for view is:

A view requests from the model through the controller the information that it needs to generate an output representation to the user.

now I'm confused again, the new definition says the view should request data from the model through the controller...

should the view access model directly on earth?

like image 608
cifer Avatar asked Sep 18 '13 13:09

cifer


People also ask

Should the view talk to the model?

Controllers can also talk directly to their View. The Model and View should never speak to each other.

Can view directly interact with model in MVC?

The important thing is that the View and the Model never interact with each other. The only interaction that takes place between them is through the Controller. This means the logic of the application and the interface never interacts with each other, which makes writing complex applications easier.

Can the view access the model directly?

The view gets its own data from the model. In some implementations, the controller may issue a general instruction to the view to render itself. In others, the view is automatically notified by the model of changes in state (Observer) that require a screen update.

How do the model and view communicate with each other?

Model to view communication is often accomplished via the Observer pattern. The code in views tends to change more than the code in models, so Model-View separation means the model elements don't depend directly on the view elements.


1 Answers

The following is a representation of dependencies in classical MVC architecture. You will notice that there is no arrow pointing from controller to view, because it is newer addition:

enter image description here
Source: GUI architectures

And then there is dependency map that is closer to what you will usually see in "MVC frameworks":

Passive View
Source: Passive view

The "passive view" configuration is not part of MVC architecture. While it uses the same names, it actually is a variation on MVP pattern (you can find a longer and more detailed description in this publication)

Bottom line: yes, if you are implementing MVC or MVC-like architecture, then your views should be requesting information from the model layer.

Also, you should note that this is NOT what so-called "mvc frameworks" are pushing for. In the Rails-like frameworks there is no view. Instead (since the original structure was made for prototyping) the views are replaced with dumb templates and all the responsibilities of a view are pushed into something they call a "controller".

Basically, IMHO, the best way to name Rails-like patterns would be OLT: ORM-Logic-Template.

like image 175
tereško Avatar answered Sep 28 '22 03:09

tereško