且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是视图模型和控制器之间的区别?

更新时间:2022-11-11 21:21:11

视图模型是用来处理视图的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.