Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ideal MVC should the view know the model?

My question is about the ideal or the original MVC interpretation http://heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html

As MVC goal is to reduce dependencies, should the View knows the Model ? Then what would prevent it to become fat and call directly Model Methods without asking Controller ?

Update: as I read answer below, I'll take a concrete example:

Let's say you create a complex calculator (not just some simple one let's say an option pricer for stock market). It only needs input like stock price, interest rate, volatility. So why would I create a reference to the whole model which contains the methods from the view since I ONLY need these input variables ?

Why the controller would not just be notified when something change in the view and then callback a method in the view with the input only ?

For example here I see the View has a reference to the whole model:

http://leepoint.net/notes-java/GUI/structure/40mvc.html

private CalcModel m_model;
like image 616
user310291 Avatar asked Sep 18 '10 14:09

user310291


People also ask

Should the view talk to the model?

Divide objects in your program into 3 “camps.” Controllers can always talk directly to their Model. Controllers can also talk directly to their View. The Model and View should never speak to each other.

Should controller know about view?

The job of the Controller is to co-ordinate between the view and the model. From that point of view, it makes sense for the controller to be the one that controls references to the view and model.

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. You can add or change view code and the model code is not affected.

Does the model or the controller update the view?

The client only interacts with the view. The controller interacts with the view and indirectly with the client to update the model. The view does not directly cause modifications in the model, but all modification requests go through the controller.


1 Answers

Then what would prevent it to become fat and call directly Model Methods without asking Controller?without asking Controller?

I found this to be a bit humorous. Views don't have minds of their own, but programmers do. They're the ones that make bad decisions and give permission for View to do what it does.

View has to know enough about Model to be able to display. If your programmers can't control themselves, one answer might be to make their Model objects immutable.

Another might be to use AOP. Write an aspect that prevents calls to the service layer that don't come from either other services or controllers.

There's one other point of view: AJAX is all about Views taking things into their own hands and making calls to services, without permission from anyone, to do things asynchronously and improve responsiveness for a better user experience. That's a good thing.

Don't be too hung up on architectural purity. MVC is a fine pattern, and very useful where it applies. Know the rules; know when it's appropriate to break the rules. Don't be so dogmatic - in programming or in life.

like image 147
duffymo Avatar answered Oct 07 '22 00:10

duffymo