MVC:模型和实体对象是不同的概念吗?

发布于 2024-09-25 08:12:17 字数 858 浏览 6 评论 0原文

我不久前在这里问过寻求一些理解 MVC 的帮助,因为我对这个主题很陌生。我认为我对它有很好的理解,这记录在 博客文章我最近写了关于这个主题的文章。我的理解基本上可以归结为:

控制器:确定需要做什么来满足请求,并根据需要利用需要收集/修改的任何模型。它基本上是给定流程的管理者。

视图:仅演示。一旦控制器收集到它需要的内容,它就会创建特定类型的视图,将信息交给它,并说“无论您如何操作,都将其显示给用户”。

模型:应用程序的行为。当控制器要求它提取或修改某些内容时,它知道如何去做。它还知道触发其他模型执行相关任务(根据我的理解,当一个模型尝试在 StackOverflow 上“为某事投票”时,该模型知道询问是否也应该因此而授予徽章。控制器不需要关心这一点)。

假设所有这些或多或少准确,我的问题是实体对象从哪里来?模型和实体是同一件事吗?每个对象都知道如何保存自己的数据,或者实体是一个独立的概念,独立存在并在整个应用程序中使用?

我的钱花在后者上,因为这将允许模型独立运行,而所有三层(模型、视图和控制器)都可以利用实体​​根据需要传递数据。此外,对象和数据库持久性似乎应该分开。

老实说,我对 MVC 的了解越多,就越感到困惑。我准备好采用核心概念(将表示与逻辑分开)并以任何感觉正确的方式运行它,而不必太担心“MVC”标签。

I asked here a while ago for some help in understanding MVC, since I'm very new to the topic. I thought I had a decent understanding of it, and this is documented in a blog post I wrote recently on the subject. My understanding basically boils down to this:

Controller: Determines what needs to be done to fulfill a request, and utilizes whatever models it needs to collect/modify as needed. It's basically a manager for a given process.

Views: Presentation only. Once a controller collects what it needs, it creates a specific type of view, hands it the information, and says "show this to the user however you do it."

Models: Behavior of the application. When the controller asks it to extract or modify something, it knows how to do it. It also knows to trigger other models to do related tasks (in my understanding, when a model tries to "vote for something" on StackOverflow, that model knows to ask if a badge should also be granted because of it. The controller doesn't need to care about that).

My question, assuming all of that is more or less accurate, is where do entity objects come in? Are models and entities the same thing, with each object knowing how to persist its own data, or are entities a separate concept that exist on their own and are used throughout the application?

My money is on the latter, since this would allow models to act independently, while all three layers (model, view and controller) could utilize the entities to pass data around as needed. Also, objects and database persistence seem like concerns that should be separated.

To be honest, the more I read about MVC the more confused I get. I'm about ready to just take the core concept (separate presentation from logic) and run with it in whatever way feels right, and not worry too much about the "MVC" label.

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

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

发布评论

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

评论(2

讽刺将军 2024-10-02 08:12:17

是的!

我的钱是后者,因为这将允许模型独立行动

您不想将视图绑定到实体,因为如果视图还需要其他一些数据,则必须将其绑定到实体。该模型完全支持该观点,并且只关心支持该观点而不是其他。

例如,您显示实体列表,您可能还需要哪些其他数据?当前页码?总页数?要显示的自定义消息?

这就是为什么您应该绑定到模型,您可以根据需要自由添加数据项。

更新

这是对 MVC 实际操作的解释...

控制器获取请求所需的所有数据并将其放入模型中。然后它将模型传递给视图。

然后视图处理模型中数据的布局。

Yes!

My money is on the latter, since this would allow models to act independently

You don't want to bind your view to an Entity, because if the view also needs some other piece of data, you would have to it to your Entity. The model is entirely supportive of the view, and is concerned with supporting that view and nothing else.

For example, you show a list of your entities, what other data might you need? Current page number? Total number of pages? A custom message to be displayed?

This is why you should bind to a model, which you can freely add data items to as you need to.

Update

Here is an explanation of MVC in action...

The controller gets all of the data required for the request and puts it into the model. It then passes the model to the view.

The view then deals with the layout of the data in the model.

还如梦归 2024-10-02 08:12:17

每个模型可以是一个实体,其中包含一些控制和使用其数据的方法。
够了吗?

Each Model can be one entity that contains some methods to control and use its data.
Is it enough?

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