简体   繁体   中英

MVC architecture control flow

I am a bit confused how the three components - Model, View, Controller - interact with each other in MVC architecture. Sometimes I feel I know how MVC works and sometimes I feel I don't know its inner working properly.

What I know about MVC architecture is -

  1. User requests are intercepted by Controller whether it is a URL
    request or any event raised by the user on the page.
  2. Then controller processes the user input and talks to the Model.
  3. Model prepares data and sends back to the Controller.
  4. Finally, controller hands over data back to the view and is displayed to the user.

Here in my opinion, View never talks to the Model directly. The interaction between the View and Model will be handled by the Controller only .

So far so good!

But when I see diagrams like below then I get confused. In the below diagram, the left arrow shows that View component can also interact with the Model component.

在此处输入图片说明

Also in this code-project article, it is written that "A model is accessible by both controller and view" and "a view can use model to display data".

So kindly clarify whether a model is accessible only by the controller or by both the controller and the view. And if second one is also correct then in what situation this is possible. I work on ASP.NET MVC Framework and here any interaction is done through a controller object. I have never encountered a situation where a view directly talks to the model object.

There's MVC, the pattern, and MVC, the web application framework developed by Microsoft and confusingly named after the pattern, so it's best to break those two out and discuss separately.

In MVC, the pattern, you'll find the following:

  • Model - Contains all the business logic for the application, including logic for how to "build" itself, whether via database queries or some other method.
  • View - provides a representation of the model for the UI
  • Controller - wires the model to the view

In MVC, the web application framework:

  • Model - There's not one. More appropriately, the model is probably some combination of one or more entity classes, a DAL layer and one or more view models.
  • View - Pretty much the same as MVC, the pattern
  • Controller - Wires the model to the view, but takes on much more responsibility as well. Could be responsible for querying entities from the database, mapping to view models, creating components for the view, etc.

Despite the name, the MVC framework only loosely implements the MVC pattern. However, in both, the Model is accessible by both the controller and the view, so your fundamental understanding there is flawed. In the case of the MVC framework, though, you could say that only the view model is accessible to the view. Since the "Model" in the MVC framework is not just one thing, there are constituent parts that should not be accessible by the view, namely: the DAL layer and potentially the entity, unless the entity is doing double duty as the view model, as well.

In Case If the view is Strongly typed with any models then we can say

View=> models

In Case if view is not strongly typed with models then we can say

controller=>Models=> Views

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM