MVC:模型和实体对象是分开的概念吗? [英] MVC: Are Models and Entity objects separate concepts?

查看:101
本文介绍了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:确定需要完成什么才能满足请求,并根据需要利用它需要收集/修改的任何模型.基本上,它是给定流程的经理.

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."

模型:应用程序的行为.当控制器要求它提取或修改某些内容时,它就会知道该怎么做.它也知道触发其他模型来执行相关任务(据我所知,当一个模型试图在StackOverflow上投票"时,那个模型知道是否要因为这个原因而授予徽章.需要在意).

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.

说实话,我对MVC的了解越多,我就越困惑.我准备好采用核心概念(将表示与逻辑分开)并以任何合适的方式使用它,而不必太担心"MVC"标签.

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.

推荐答案

是!

我的钱在后者上,因为这将允许模型独立运行

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.

更新

以下是对MVC实际使用的解释...

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.

这篇关于MVC:模型和实体对象是分开的概念吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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