ViewModel 和 Controller 有什么区别? [英] What's the difference between a ViewModel and Controller?

查看:29
本文介绍了ViewModel 和 Controller 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个与另一个的责任是什么?一个和另一个应该采用什么样的逻辑?哪一个击中了服务和数据库?我如何决定我的代码应该放在视图模型还是控制器中?

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?

作为记录,我使用的是 ASP MVC,但由于问题是架构问题,我认为我使用的语言或框架并不重要.我正在邀请所有 MVC 响应

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

推荐答案

ViewModel 是一种 Pattern,用于处理 View 的表示逻辑和状态,控制器是任何 MVC 框架的基础部分之一,它响应任何 http 请求并协调所有后续操作,直到 http 响应.

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 responds to any http request and orchestrates all the subsequent actions until the http response.

ViewModel 模式:更多信息

The ViewModel Pattern: More info

在 ViewModel 模式中,UI 和任何 UI 逻辑都封装在一个看法.View 观察一个 ViewModel它封装了表示逻辑和状态.ViewModel 依次与模型交互并充当它和它之间的中介查看.

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)

--明显的区别:--

  • 虽然 ViewModel 是可选的模式控制器是必须的,如果您正在采用 MVC 方式.
  • ViewModel 封装了表示逻辑和状态,控制器协调所有申请流程.

这篇关于ViewModel 和 Controller 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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