什么是“模型”?在MVC模式中?

发布于 2024-12-15 09:32:10 字数 114 浏览 5 评论 0原文

所以我在 Google 上搜索了 MVC 模式,但我仍然不确定“模型”部分是什么。它具体涉及什么?我对编程相当陌生,所以我能找到的所有解释都在我的脑海中。如果您能给我简单的解释,我将非常感激。

谢谢

So I did some Google searching on the MVC pattern and I'm still not exactly sure what the "Model" part is. What exactly does it deal with? I'm rather new to programming so all the explanations I can find go right over my head. I'd really appreciate it if you could give me an explanation in simple terms.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

在风中等你 2024-12-22 09:32:10

我描述它的最简单的方法就是将其称为“数据”部分。如果它必须处理获取或保存数据,那么它就在模型中。如果您有一个 Web 应用程序,该模型通常是您与数据库或文件系统交互的地方。

The simplest way I can describe it is to call it the "Data" part. If it has to deal with getting or saving data, it's in the model. If you have a web application, the model is usually where you interact with a database or a filesystem.

红ご颜醉 2024-12-22 09:32:10

MVC中的模型是UI呈现的数据所在的地方。因此,不应将其与域模型混淆,域模型充当保存业务逻辑的骨架。

当然,对于作为数据库支持的 CRUD 操作服务的小型应用程序,这两个模型可以是相同的。在现实世界的应用程序中,它们应该完全分开。

控制器是与应用程序服务和域模型对话的人。它接收从应用程序服务返回的更新,更新模型,然后由视图呈现。

视图呈现模型保存的状态,解释用户的输入并将其重定向到控制器。然后,控制器决定是否立即更新模型,或者首先将信息转发到应用程序服务。

Model in MVC is a place where data presented by the UI is located. Therefore, it shouldn't be confused with Domain Model which serves as a skeleton that holds the business logic.

Certainly, for a small application that serves as a service for CRUD operations backed by a database these two models can be the same. In a real world application they should be cleanly separated.

Controller is the one who talks to the application services and the Domain Model. It receives updates back from application services updating the Model which is then rendered by the View.

View renders the state hold by the Model, interprets User's input and it redirects it to the Controller. Controller then decides if the Model is going to be updated immediately or first the information is forwarded to application services.

半﹌身腐败 2024-12-22 09:32:10

该模型可以代表较小项目中的“领域模型”。领域模型由代表您正在处理的问题的现实世界实体的类组成。

在较大的项目中,域模型应该从实际的 MVC 应用程序中分离出来,并给出它自己的项目/程序集。在这些较大的项目中,为 UI 表示对象(DTO - 数据传输对象)保留“模型”(即“MVC 项目中的模型文件夹”)

The Model can represent your "Domain Model" in smaller projects. The Domain Model consists of classes which represent the real-world entities of the problem you're dealing with.

In larger projects, the Domain Model should be separated out of the actual MVC application, and given it's own project/assembly. In these larger projects, reserve the "Model" (i.e. the "Models folder in the MVC project") for UI presentation objects (DTOs - Data Transfer Objects)

肥爪爪 2024-12-22 09:32:10

该模型负责管理应用程序中的数据。这可能包括数据库查询和文件 IO 等内容。

视图显然是模板,控制器是业务逻辑。

The model is respomnsible for managing the data in the application. This might include stuff like database queries and file IO.

the view is obviously the template, with controller being the business logic.

娇纵 2024-12-22 09:32:10

该模型用于表示您正在使用的数据。控制器控制数据流和可以对数据采取的操作。视图将数据和可以向控制器请求的操作可视化。

简单的例子:

汽车是一个模型,它具有代表汽车的属性(车轮、发动机等)。

控制器定义了可以对汽车执行的操作:查看、编辑、创建,甚至是购买和出售等操作。

控制器将数据传递到视图,该视图既显示数据,有时又允许用户对该数据执行操作。然而,请求的操作实际上是由控制器处理的。

The model is used to represent the data you are working with. The controller controls the data flow and actions that can be taken with the data. The view(s) visualize the data and the actions that can be requested of the controller.

Simple example:

A car is a model, it has properties that represent a car (wheels, engine, etc).

The controller defines the actions that can be taken on the car: view, edit, create, or even actions like buy and sell.

The controller passes the data to a view which both displays the data and sometimes lets the user take action on that data. However, the requested action is actually handled by the controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文