MVC模式.模型,视图和控制器之间的关系 [英] MVC pattern. The relationship between Model, View and Controller

查看:838
本文介绍了MVC模式.模型,视图和控制器之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模型,视图和控制器之间的关系使我感到困惑.

本主题显示从视图到控制器的箭头,从控制器到模型的箭头以及从模型到视图的箭头: http://www.codeproject.com /Tips/31292/MVC-vs-MVP-如何常见与如何区别

但是,本主题显示了模型"和视图"之间的双向箭头; View和Controller之间的双箭头;以及从Controller到Model的箭头: http://www.codeproject.com/Articles /288928/初学者的MVC和MVP之间的差异

最后,此主题显示从视图到模型的箭头,从控制器到模型的箭头,以及从控制器到视图的箭头: http://www.w3schools.com/aspnet/mvc_intro.asp

我有一些问题:

  1. 哪些关系是正确的?
  2. 业务逻辑应在控制器或模型中处理?我读过某处不应将业务逻辑放在控制器(ASP.Net MVC)中
  3. 如果控制器将对象传递给视图,那么该对象是否属于Model?
  4. 视图如何直接从模型中检索数据?它是否直接具有模型参考或与Controller的模型交互?

解决方案

我发现您链接的所有图像令人困惑.这张图片(取自Wikipedia )说明最好.

工作原理

MVC考虑三个角色. 模型是一个对象,代表有关该域的一些信息.这是非视觉的 包含除用于之外的所有数据和行为的对象 用户界面.

视图表示模型在UI中的显示.因此,如果 我们的模型是一个客户对象,我们的视图可能是一个充满UI的框架 用来自模型的信息呈现的窗口小部件或HTML页面.这 视图仅是信息的显示;对的任何更改 信息由MVC三位一体的第三个成员处理: 控制器. controller (控制器)接受用户输入,操纵模型,并适当地更新视图.这样,UI是 视图和控制器的组合.

-引自Martin Fowler的企业应用程序体系结构模式

您的问题

  1. MVC是关注点分离,而不是关系.
  2. 业务逻辑应该在模型中.控制器仅用于与用户进行交互.
  3. 是(很有可能)
  4. 通常,视图从模型中获取必要的信息.使用被动视图时,对象(来自模型)从控制器传递.重要的是,视图仅从模型读取,而从不写入/更新模型.

    视图观察并响应模型的变化.该模型是域模型,而不是单个记录集或实体.

勘误表

目前,MVC的常用方式与马丁·福勒(Martin Fowler)提出的原始MVC模式有所不同.他基于 Smalltalk .

我称之为MVC的核心,它是对以后的框架最有影响力的想法,称为单独的演示文稿.

MVC的另一部分是模型,视图和控制器之间的交互方式.

在这种情况下,所有视图和控制器均会观察模型.当模型更改时,视图会做出反应.

这与 Ruby on Rails 流行的MVC完全不同,后者由控制器负责准备和加载视图.

class ArticlesController < ApplicationController
  def create
    @article = Article.new(article_params)

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

马丁·福勒(Martin Fowler)将MVC浓缩到了这一点

  • 在演示文稿(视图和控制器)和域(模型)之间进行强烈分离-分离的演示文稿.
  • 将GUI小部件划分为控制器(用于对用户刺激做出反应)和视图(用于显示模型状态).控制器和视图 应该(主要)不直接沟通,而是通过模型进行沟通.
  • 让视图(和控制器)观察模型以允许更新多个小部件而无需直接通信-Observer 同步.

-引自Martin Fowler的 GUI体系结构

The relationship between Model, View and Controller make me confused.

This topic shows an arrow form View to Controller, an arrow from Controller to Model and an arrow from Model to View: http://www.codeproject.com/Tips/31292/MVC-v-s-MVP-How-Common-and-How-Different

However, this topic shows a dual-arrow between Model and View; a dual-arrow between View and Controller ; and an arrow from Controller to Model: http://www.codeproject.com/Articles/288928/Differences-between-MVC-and-MVP-for-Beginners

Finally, this topic shows an arrow from View to Model, an arrow from Controller to Model and an arrow from Controller to View: http://www.w3schools.com/aspnet/mvc_intro.asp

I have some questions:

  1. Which relationships are correct?
  2. Business Logic should be handled in Controller or Model? I've read somewhere that the business logic shouldn't be placed in Controller (ASP.Net MVC)
  3. In case the controller pass an object to a view, is this object belonged to Model?
  4. How does the view retrieve data directly from model? Does it have the reference directly to model or it interact with the model coming from Controller?

解决方案

I find all of the images you're linking to confusing. This image (taken from Wikipedia) explaines it best.

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.

-- Quoted from Patterns of Enterprise Application Architecture by Martin Fowler

Your questions

  1. MVC is about separation of concerns, not about relationships.
  2. Business Logic should be in the model. The controller is only for interacting with the user.
  3. Yes (most likely)
  4. Typically the view fetches the necessary information from the model. When using passive views, objects (from the model) are passed from the controller. Important is that the view only reads from the model and never writes/updates it.

    The View observes and responds to changes in model. The model is the Domain Model rather than an individual recordset or entity.

Errata

How MVC is commonly used in the present time differs from the original MVC pattern as it was coined by Martin Fowler. He based this pattern in Smalltalk.

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

Another part of MVC is how the model, view and controller interact.

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

This is very different from MVC as made popular by Ruby on Rails, where the controller is responsible for preparing and loading the view.

class ArticlesController < ApplicationController
  def create
    @article = Article.new(article_params)

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

Martin Fowler condenses MVC down to this

  • 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.

-- Quoted from GUI Architectures by Martin Fowler

这篇关于MVC模式.模型,视图和控制器之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆