简体   繁体   English

MVC架构控制流程

[英]MVC architecture control flow

I am a bit confused how the three components - Model, View, Controller - interact with each other in MVC architecture. 我有点困惑,这三个组件-模型,视图,控制器-在MVC体系结构中如何相互影响。 Sometimes I feel I know how MVC works and sometimes I feel I don't know its inner working properly. 有时我感觉我知道MVC的工作原理,有时我感觉我不知道它的内部工作正常。

What I know about MVC architecture is - 我对MVC架构的了解是-

  1. User requests are intercepted by Controller whether it is a URL 控制器是否拦截用户请求,是否为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. 在我看来,View从不直接与模型对话。 The interaction between the View and Model will be handled by the Controller only . 视图和模型之间的交互将仅由Controller处理。

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. 在下图中,左箭头表示View组件也可以与Model组件进行交互。

在此处输入图片说明

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. 我使用ASP.NET MVC Framework,在这里任何交互都是通过控制器对象完成的。 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. 有MVC(该模式)和MVC(由Microsoft开发并以该模式混乱地命名)的Web应用程序框架,因此最好将两者分开讨论。

In MVC, the pattern, you'll find the following: 在MVC模式中,您将找到以下内容:

  • 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 视图-为UI提供模型的表示形式
  • Controller - wires the model to the view 控制器-将模型连接到视图

In MVC, the web application framework: 在MVC中,Web应用程序框架:

  • 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. 更适当地,该模型可能是一个或多个实体类,一个DAL层和一个或多个视图模型的某种组合。
  • View - Pretty much the same as MVC, the pattern 视图-与MVC差不多,模式
  • 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. 尽管名称如此,MVC框架仅松散地实现了MVC模式。 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. 但是,对于MVC框架,您可以说视图只能访问视图模型。 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. 由于MVC框架中的“模型”不仅仅是一件事,因此视图不应访问某些组成部分,即:DAL层和潜在的实体,除非实体作为视图模型承担双重责任,也一样

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 控制器=>模型=>视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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