DDD和MVC:“模型"和“实体"之间的区别 [英] DDD and MVC: Difference between 'Model' and 'Entity'

查看:1071
本文介绍了DDD和MVC:“模型"和“实体"之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MVC中模型"的概念感到非常困惑.当今存在的大多数框架都将模型放置在Controller和数据库之间,并且该模型几乎充当数据库抽象层的角色.随着控制器开始执行越来越多的逻辑,胖模型瘦控制器"的概念消失了.

在DDD中,还有一个域实体的概念,它具有唯一的标识.据我了解,用户是实体的一个很好的例子(例如,唯一的用户ID).实体具有生命周期-它的值可以在操作的整个过程中改变-然后被保存或丢弃.

我在上面描述的实体就是我认为Model应该在MVC中的实体?我的境况如何?

要使事情更加混乱,您需要引入其他模式,例如存储库模式(也许在其中放置服务).很明显,存储库将如何与实体进行交互-如何与模型进行交互?

控制器可以具有多个模型,这使得模型似乎比唯一的实体少了一个数据库表".

更新:在此帖子该模型被描述为具有知识的事物,它可以是单个的或对象的集合.因此,听起来更像是实体和模型大致相同.模型是一个笼统的术语,其中实体是更具体的.值对象也将是模型.至少就MVC而言.也许吗?

那么,以粗略的说法,哪个更好?

真的没有模特" ...

class MyController {
    public function index() {
        $repo = new PostRepository();
        $posts = $repo->findAllByDateRange('within 30 days');
        foreach($posts as $post) {
            echo $post->Author;
        }
    }
}

还是这个,有一个模型作为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;
        }
    }
}

这两个例子甚至都没有达到我上面所描述的.我显然迷路了.有输入吗?

解决方案

实体

Entity表示一个对象,该对象是与业务逻辑一起使用的单个项目,更具体地说,是具有某种标识的对象.
因此,许多人将ORM映射的对象称为实体.

有些人将此类称为"实体",该类的实例表示数据库中的单行.

另一些​​人更喜欢仅将这些类中的那些称为实体",其中还包含业务规则,验证和一般行为,而其他人则将其称为"数据传输对象". /p>

型号

A Model与应用程序的UI(= View)和控制流(= Controller)不直接相关,而是与数据访问方式和主要数据抽象有关.该应用程序有效.

基本上,任何事物都可以是符合上述条件的模型.

MVC

您可以在MVC中将实体用作模型.它们表示两种不同的含义,但是相同的类可以同时称为两者.

示例

  • Customer类通常是一个实体(通常),并且您还可以在应用程序中将其用作数据访问的一部分.在这种情况下,它既是实体又是模型.
  • Repository类可能是模型的一部分,但显然不是实体.
  • 如果在业务逻辑层的中间使用了一个类,但没有公开给应用程序的其余部分,则它可能是一个实体,但是从MVC的角度来看,显然不是模型.应用程序.

您的示例

对于您的代码示例,我希望使用第一个.
Model是用于作为应用程序的数据抽象手段的类,而不是名称后缀为"Model"的类.许多人认为后一种过时的软件.

您几乎可以将您的Repository类视为模型的一部分,即使其名称不带"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.

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.

The Entity I describe above is what I thought Model was supposed to be in MVC? How off-base am I?

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.

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;
        }
    }
}

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

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

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.

Examples

  • A 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.
  • A Repository class may be part of the Model, but it is clearly not an entity.
  • If there is a class that you use in the middle of your business logic layer but don't expose to the rest of the application, it may be an entity, but it is clearly not a Model from the perspective of the MVC app.

Your example

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.

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:“模型"和“实体"之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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