What's the difference between a ViewModel and

2019-02-05 15:39发布

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

7条回答
混吃等死
2楼-- · 2019-02-05 16:19

Model represents your data and how it's manipulated. Thus, model touches the DB.

View is your UI.

Controler is the glue between them.

查看更多
Luminary・发光体
3楼-- · 2019-02-05 16:27

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.
查看更多
叛逆
4楼-- · 2019-02-05 16:29

I think there's some value to learning received doctrine. But there is also value in understanding how the doctrine came to be the way it is.

Trygve Reenskaug is widely credited with inventing MVC. N. Alex Rupp's article Beyond MVC: A new look at the servelet architecture includes a History of MVC. In a section on Reenskaug's 1978 work at Xerox Palo Alto Research Center, there's a link to his paper Thing-Model-View-Editor: an Example from a planningsystem. There the pieces are described like this.

Thing

Something that is of interest to the user. It could be concrete, like a house or an integrated circuit. It could be abstract, like a new idea or opinions about a paper. It could be a whole, like a computer, or a part, like a circuit element.

Model

A Model is an active representation of an abstraction in the form of data in a computing system

View

To any given Model there is attached one or more Views, each View being capable of showing one or more pictorial representations of the Model on the screen and on hardcopy. A View is also able to perform such operations upon the Model that is reasonabely associated with that View.

Editor

An Editor is an interface between a user and one or more views. It provides the user with a suitable command system, for example in the form of menus that may change dynamically according to the current context. It provides the Views with the necessary coordination and command messages.

Rupp identifies Reenskaug's Editor as a Controller or Tool.

MVC Triads emerged in SmallTalk-80. The model was an abstraction of the real-world concept, the view was its visual representation, and the controller was the buttons and slider bars that allowed the user to interact with it (thereby "controlling" the view). All pieces in the triad were interconnected and could communicate with the other two pieces, so there was no layering or abstraction involved. Since then, Reenskaug has "preferred to use the term Tool rather then Controller." According to his notes, these are the terms he has used in later implementations

查看更多
【Aperson】
5楼-- · 2019-02-05 16:31

Some logic and model should be invoked to generate some data (structured or semi-structured). From this data the returned page/JSON/etc. is created, typically with only rudimentary outlining logic.

The first part (creating the data) is done by the controller (via the model usually). The second part - by the View. The ViewModel is the data structure being passed between controller and view, and usually contains only accessors.

查看更多
放荡不羁爱自由
6楼-- · 2019-02-05 16:33

enter image description here

  • The ViewModel can be on the client side as well as server side. Wherever it may be, the sole purpose of viewmodel is to play the presentation data.
  • In MVC architecture Viewmodel is not mandatory but with out controller the request from the client cannot be processed.
  • Controller can be visualised as the main interface between client and server to get any response from the server. It processes the client request, fetches data from repository and then prepares the view data. Viewmodel can be visualised as view data processor/presenter thus an interface to manage the view more eloquently.
  • In the overall context of a web application we can say the controller is the application request handler whereas viewmodel is only the UI handler.
查看更多
Juvenile、少年°
7楼-- · 2019-02-05 16:33

MVC stands for Model, View, Controller.

Model = Data (Database tables)

View = HTML, CSS, JavaScript, etc

Controller = Main logic, a contract between Model & View.

In simple and graspable terms,

MVC allows you to develop your applications in a way that your business data and presentation data are separated. With this, a developer and designer can work independently on a MVC app without their work clashing. MVC makes your app avail OOP too.

查看更多
登录 后发表回答