什么是视图模型和控制器之间的区别? [英] What's the difference between a ViewModel and Controller?

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

问题描述

什么是一体VS其他的职责是什么?
应该在一个VS其他走什么样的逻辑?
哪一个打服务和数据库?
我该如何决定,如果我的code应在视图模型或控制器去?

有关记录,我使用ASP的MVC,但由于问题的建筑,我不相信它很重要,我使用的是什么语言或框架。我邀请所有MVC回应


解决方案

视图模型是用来处理视图的presentation逻辑和状态的模式,控制器是所有MVC框架的基本组成部分之一,它响应任何HTTP请求和orchest所有的后续操作,直到HTTP响应。

视图模型模式:更多信息


  

在视图模型模式,用户界面​​和
  任何UI逻辑封装在一个
  视图。视图观察一个ViewModel
  它封装了presentation逻辑
  和状态。该视图模型反过来
  与型号,并作为互动
  它和之间的中介
  查看。


 查看<  - >视图模型<  - >模型

该控制器(从前端控制器模式附带):更多信息


  

据提供了一个集中的切入点
  处理的请求。


  HTTP请求 - >控制器 - > (模型,视图)

- 平原的差异: -


  • 当视图模型是一个可选
    模式控制器是必须的,如果
    您准备了MVC的方式。

  • 视图模型封装
    presentation逻辑和状态时,
    控制器orchest所有
    应用程序流。

What are the responsibilities of one vs the other? What kind of logic should go in one vs the other? Which one hits services and databases? How do I decide if my code should go in the viewmodel or the controller?

For the record, I am using ASP MVC, but since the question is architectural, I do not believe it matters what language or framework I am using. I'm inviting all MVC to respond

解决方案

The ViewModel is a Pattern used to handle the presentation logic and state of the View and the controller is one of the fundamentals parts of any MVC framework, it response to any http request and orchest all the subsequent actions until the http response.

The ViewModel Pattern: More info

In the ViewModel pattern, the UI and any UI logic are encapsulated in a View. The View observes a ViewModel which encapsulates presentation logic and state. The ViewModel in turn interacts with the Model and acts as an intermediary between it and the View.

View <-> ViewModel <-> Model

The Controllers (Comes from the Front Controller Pattern): More Info

It "provides a centralized entry point for handling requests."

HTTP Request -> Controller -> (Model,View)

--Plain Differences:--

  • While the ViewModel is an optional pattern the Controller is a must, if you are going the MVC way.
  • The ViewModel encapsulates presentation logic and state, The Controller orchest all the Application Flow.

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

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