我对 MVC 中模型"的概念感到非常困惑.当今存在的大多数框架都将 Model 放在 Controller 和数据库之间,而 Model 几乎就像一个数据库抽象层.随着控制器开始做越来越多的逻辑,胖模型瘦控制器"的概念消失了.
I'm seriously confused about the concept of the 'Model' in MVC. Most frameworks that exist today put the Model between the Controller and the database, and the Model almost acts like a database abstraction layer. The concept of 'Fat Model Skinny Controller' is lost as the Controller starts doing more and more logic.
在 DDD 中,还有域实体的概念,它具有唯一的身份.据我了解,用户是实体的一个很好的例子(例如,唯一的用户 ID).实体有一个生命周期——它的值可以在整个操作过程中改变——然后它被保存或丢弃.
In DDD, there is also the concept of a Domain Entity, which has a unique identity to it. As I understand it, a user is a good example of an Entity (unique userid, for instance). The Entity has a life-cycle -- it's values can change throughout the course of the action -- and then it's saved or discarded.
我上面描述的实体是我认为模型应该在MVC中的东西?我离基地有多远?
The Entity I describe above is what I thought Model was supposed to be in MVC? How off-base am I?
为了使事情更加混乱,您可以使用其他模式,例如 Repository 模式(可能在其中放置一个 Service).存储库如何与实体交互非常清楚——它如何与模型交互?
To clutter things more, you throw in other patterns, such as the Repository pattern (maybe putting a Service in there). It's pretty clear how the Repository would interact with an Entity -- how does it with a Model?
控制器可以有多个模型,这使得模型看起来不像是一个数据库表",而是一个唯一的实体.
Controllers can have multiple Models, which makes it seem like a Model is less a "database table" than it is a unique Entity.
更新: 在这篇文章 模型被描述为具有知识的东西,它可以是单一的或对象的集合.所以这听起来更像是一个实体和一个模型或多或少是一样的.模型是一个包罗万象的术语,其中实体更具体.值对象也将是模型.至少在 MVC 方面.也许???
UPDATE: In this post the Model is described as something with knowledge, and it can be singular or a collection of objects. So it's sound more like an Entity and a Model are more or less the same. The Model is an all encompassing term, where an Entity is more specific. A Value Object would be a Model as well. At least in terms of MVC. Maybe???
那么,粗略地说,哪个更好?
So, in very rough terms, which is better?
真的没有模特"......
No "Model" really ...
class MyController {
public function index() {
$repo = new PostRepository();
$posts = $repo->findAllByDateRange('within 30 days');
foreach($posts as $post) {
echo $post->Author;
}
}
}
或者这个,它有一个模型作为 DAO?
Or this, which has a Model as the DAO?
class MyController {
public function index() {
$model = new PostModel();
// maybe this returns a PostRepository?
$posts = $model->findAllByDateRange('within 30 days');
while($posts->getNext()) {
echo $posts->Post->Author;
}
}
}
这两个例子甚至都没有做我上面描述的事情.我显然迷路了.任何输入?
Both those examples didn't even do what I was describing above. I'm clearly lost. Any input?
Entity
表示作为业务逻辑使用的单个项目的对象,更具体地说是那些具有某种身份的对象.
因此,许多人将 ORM 映射的对象称为实体.
Entity
means an object that is a single item that the business logic works with, more specifically those which have an identity of some sort.
Thus, many people refer to ORM-mapped objects as entities.
有些人将一个类称为实体",该类的实例代表数据库中的一行.
Some refer to as "entity" to a class an instance of which represents a single row in a database.
其他一些人更喜欢仅将这些类中的那些称为实体",其中还包含业务规则、验证和一般行为,而将其他类称为数据传输对象".
Some other people prefer to call only those of these classes as "entity" which also contain business rules, validation, and general behaviour, and they call the others as "data transfer objects".
Model
与应用程序的 UI(=View
)和控制流(=Controller
)没有直接关系,而是关于数据访问方式和应用程序的主要数据抽象的工作方式.
A Model
is something that is not directly related to the UI (=View
) and control flow (=Controller
) of an application, but rather about the way how data access and the main data abstraction of the application works.
基本上,任何东西都可以是符合上述条件的模型.
Basically, anything can be a model that fits the above.
您可以在 MVC 中使用实体作为模型.它们表示两种不同的含义,但相同的类可以同时调用.
You can use entities as your models in MVC. They mean two different things, but the same classes can be called both.
示例
Customer
类在很大程度上是一个实体(通常),您还可以将它用作应用程序中数据访问的一部分.在这种情况下,它既是实体又是模型.Repository
类可能是模型的一部分,但它显然不是一个实体.Customer
class is very much an entity (usually), and you also use it as part of data access in your app. It is both an entity and a model in this case.Repository
class may be part of the Model, but it is clearly not an entity.你的例子
至于您的代码示例,我更喜欢第一个.
模型是用作应用程序数据抽象手段的类,而不是名称后缀为模型"的类.许多人认为后者是膨胀软件.
As for your code examples, I would prefer the first one.
A Model is a class that is used as a means of data abstaction of an application, not a class which has a name suffixed with "Model". Many people consider the latter bloatware.
您几乎可以将 Repository 类视为模型的一部分,即使其名称没有以Model"为后缀.
You can pretty much consider your Repository class as part of your model, even if its name isn't suffixed with "Model".
我还要补充一点,使用第一个代码也更容易,对于以后可能需要理解您的代码的其他人来说,它更容易理解.
I would add to that the fact that it is also easier to work with the first one, and for other people who later may have to understand your code, it is easier to understand.
这篇关于DDD 和 MVC:“模型"和“实体"之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!