gpt4 book ai didi

model-view-controller - MVC 模式。 Model、View、Controller的关系

转载 作者:行者123 更新时间:2023-12-04 03:38:33 28 4
gpt4 key购买 nike

模型、 View 和 Controller 之间的关系让我感到困惑。

本主题显示了从 View 到 Controller 的箭头、从 Controller 到模型的箭头以及从模型到 View 的箭头:
http://www.codeproject.com/Tips/31292/MVC-v-s-MVP-How-Common-and-How-Different

但是,本主题显示了模型和 View 之间的双箭头; View 和 Controller 之间的双箭头;以及从 Controller 到模型的箭头:
http://www.codeproject.com/Articles/288928/Differences-between-MVC-and-MVP-for-Beginners

最后,本主题显示了从 View 到模型的箭头、从 Controller 到模型的箭头以及从 Controller 到 View 的箭头:
http://www.w3schools.com/aspnet/mvc_intro.asp

我有一些问题:

  • 哪些关系是正确的?
  • 业务逻辑应该在 Controller 还是模型中处理?我在某处读到业务逻辑不应该放在 Controller (ASP.Net MVC)中
  • 如果 Controller 将对象传递给 View ,该对象是否属于模型?
  • View 如何直接从模型中检索数据?它是直接引用模型还是与来自 Controller 的模型交互?
  • 最佳答案

    我发现您链接到的所有图像都令人困惑。这张图片 ( taken from Wikipedia ) 解释得最好。

    MVC diagram

    How It Works

    MVC considers three roles. The model is an object that represents some information about the domain. It's a nonvisual object containing all the data and behavior other than used for the UI.

    The view respresents the display of the model in the UI. Thus, if our model is a customer object our view might be a frame full of UI widget or an HTML page rendered with information from the model. The view is only about display of information; any changes to the information are handled by the third member of the MVC trinity: the controller. The controller takes user input, manipulates the model, and causes the view to update appropriately. In this way UI is a combination of the view and the controller.



    -- 引自 Patterns of Enterprise Application Architecture通过马丁福勒

    你的问题
  • MVC 是关于关注点分离,而不是关系。
  • 业务逻辑应该在模型中。 Controller 仅用于与用户交互。
  • 是(最有可能)
  • 通常, View 从模型中获取必要的信息。使用被动 View 时,对象(来自模型)从 Controller 传递。重要的是, View 只从模型读取,从不写入/更新它。

    View 观察并响应模型的变化。型号是Domain Model而不是单个记录集或实体。

  • 勘误表

    当前 MVC 的常用方式与 Martin Fowler 创造的原始 MVC 模式不同。他基于此模式在 Smalltalk .

    At the heart of MVC, and the idea that was the most influential to later frameworks, is what I call Separated Presentation.



    MVC 的另一部分是模型、 View 和 Controller 如何交互。

    In this case all the views and controllers observe the model. When the model changes, the views react.



    这与 流行的 MVC 非常不同。 Ruby on Rails , Controller 负责准备和加载 View 。
    class ArticlesController < ApplicationController
    def create
    @article = Article.new(article_params)

    if @article.save
    redirect_to @article
    else
    render 'new'
    end
    end

    Martin Fowler 将 MVC 浓缩为这个

    • Make a strong separation between presentation (view & controller) and domain (model) - Separated Presentation.
    • Divide GUI widgets into a controller (for reacting to user stimulus) and view (for displaying the state of the model). Controller and view should (mostly) not communicate directly but through the model.
    • Have views (and controllers) observe the model to allow multiple widgets to update without needed to communicate directly - Observer Synchronization.


    -- 引自 GUI Architectures通过马丁福勒

    关于model-view-controller - MVC 模式。 Model、View、Controller的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29005264/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com