In ideal MVC should the view know the model?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-02 03:34:28

问题


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;

回答1:


The view should not know about the business model, that's up to the controller. The view should however know about the data model. How else must it present it?

See also:

  • Design Patterns web based applications



回答2:


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.




回答3:


Yes, in MVC the view knows about the model. In fact, the view's job is to display the model so it has to know about the model. The models should be fairly simply and shouldn't pretty much be containers of state (i.e., properties that are strings, ints, etc.) - it shouldn't really have methods.

The controller knows about both the view and the model - the controller's job is to get the appropriate model and pass it to the appropriate view.

The model should be blissfuly unaware of either the controller or the view.

This is the typical Separation of Concerns in MVC (SoC) where each component has its well-defined "job".




回答4:


In MVC the point is not that you should not comunicate between M-V-C. The point is to keep the model seperated from the view (and the controller) so you can change/substitute the components easily.

http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller




回答5:


The goal of the view is to render data derived from the model. So the answer is it should not. On the other hand the model should not care how those data will be presented.

For instance, the business logic might have been:

  view->report->annual_revenue =
    this->first_quarter_rev +
    this->second_quarter_rev +
    this->third_quarter_rev +
    this->fourth_quarter_rev;

While the presentation logic:

  if (!this->report->annual_revenue) {
    print('No information')
  }
  else {
    print(format('# ###', this->report->annual_revenue) + '$');
  }


来源:https://stackoverflow.com/questions/3742018/in-ideal-mvc-should-the-view-know-the-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!