DDD 和 MVC:“模型”之间的区别和“实体”

发布于 2024-09-05 19:17:24 字数 1358 浏览 4 评论 0原文

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

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

我上面描述的实体是我认为 MVC 中应该存在的模型吗?我离基地有多远?

为了使事情变得更加混乱,您可以添加其他模式,例如存储库模式(可能在其中放置一个服务)。存储库如何与实体交互非常清楚——它如何与模型交互?

控制器可以有多个模型,这使得模型看起来更像是一个唯一的实体,而不是一个“数据库表”。

更新: 在这篇文章中< /a> 模型被描述为具有知识的东西,它可以是单个的,也可以是对象的集合。所以听起来更像是实体和模型或多或少是相同的。模型是一个包罗万象的术语,而实体则更为具体。值对象也可以是模型。至少在 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;
        }
    }
}

这两个例子甚至都没有达到我上面描述的目的。我显然迷路了。有什么意见吗?

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?

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

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

发布评论

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

评论(5

错々过的事 2024-09-12 19:17:24

实体

实体表示一个对象,它是业务逻辑所使用的单个项目,更具体地说是那些具有某种身份的对象。
因此,许多人将 ORM 映射的对象称为实体。

有些人将一个类称为“实体”,该类的实例代表数据库中的一行。

其他一些人更喜欢仅将这些类中的那些还包含业务规则、验证和一般行为的类称为“实体”,并将其他类称为“数据传输对象”。

模型

Model 与应用程序的 UI (=View) 和控制流 (=Controller) 不直接相关,但是而是关于应用程序的数据访问和主要数据抽象的工作方式。

基本上,任何符合上述条件的东西都可以成为模型。

MVC

您可以使用实体作为 MVC 中的模型。它们意味着两种不同的事物,但相同的类可以同时被称为两者。

示例

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

您的示例

至于您的代码示例,我更喜欢第一个。
模型是用作应用程序数据抽象手段的类,而不是名称以“Model”为后缀的类。许多人认为后者是过时的软件。

您几乎可以将 Repository 类视为模型的一部分,即使其名称不带有“Model”后缀。

我想补充一点,使用第一个代码也更容易,并且对于后来可能需要理解您的代码的其他人来说,它更容易理解。

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.

胡大本事 2024-09-12 19:17:24

所有答案都是不同事物的严重混搭,而且完全是错误的。

DDD 中的模型非常类似于现实世界中的模型:
事物的简化和抽象。
不多也不少。
它与数据、对象或其他任何东西无关。
这只是域部分的概念。在每一个复杂的领域
总是有不止一种模型,例如贸易、发票、物流。

实体不是“具有身份的模型”,而只是具有身份的对象。

存储库不仅仅是一级缓存,而且也是域的一部分。
它给人一种内存中对象的错觉并负责获取
来自任何地方的聚合(不是实体!)并保存它们
即维护对象的生命周期。

All answers are a heavy mashup of different things and simply wrong.

A model in DDD is much like a model in the real world:
A simplification and abstraction of something.
No less and no more.
It has nothing to do with data nor objects or anything else.
It's simply the concept of a domain part. And in also every complex domain
there is always more than one model, e.g. Trading, Invoicing, Logistics.

An entity is not a "model with identity" but simply an object with identity.

A repository is not just a 1st level cache but a part of the domain too.
It is giving an illusion of in-memory objects and responsible for fetching
Aggregates (not entities!) from anywhere and saving them
i.e. maintaining the life cycle of objects.

花开半夏魅人心 2024-09-12 19:17:24

应用程序中的“模型”是保存数据的位。如果我没记错的话,领域驱动设计中的“实体”是一个具有身份的模型。也就是说,实体是通常直接对应于数据库或文件中的“物理”元素的模型。我认为DDD定义了两种类型的模型,一种是实体,另一种是价值,这只是一个没有身份的模型。

存储库模式只是一种模型/实体的索引集合。因此,例如,如果您的代码想要订单#13,它将首先向存储库询问它,如果它无法从那里获取它,它将去从任何地方获取它。如果你愿意的话,它基本上是一个 1 级缓存。它与模型的交互方式以及与实体的交互方式没有区别,但由于存储库的想法是能够使用模型的 ID 来获取模型,因此就 DDD 而言,只有实体才被允许进入存储库。存储库。

The "model" in your application is the bit which holds your data. The "entity" in domain-driven design is, if I remember correctly, a model with an identity. That is to say, an entity is a model which usually corresponds directly to a "physical" element in a database or file. I believe DDD defines two types of models, one being the entity, the other being the value, which is just a model without and identity.

The Repository pattern is just a type of indexed collection of models/entities. So for instance if your code wants order #13, it will first ask the repository for it, and if it can't get it from there, it will go and fetch it from wherever. It's basically a level 1 cache if you will. There is no difference in how it acts with a model, and how it acts with an entity, but since the idea of a repository is to be able to fetch models using their IDs, in terms of DDD, only entities would be allowed into the repository.

葬シ愛 2024-09-12 19:17:24

使用服务和收集的简单解决方案:

<?php
class MyController {
    public function index() {
        $postService = ServiceContainer::get('Post');
        $postCollection = $postService->findAllByDateRange('within 30 days');
        while($postCollection->getNext()) {
            echo $postCollection->current()->getAuthor();
        }
    }
}

编辑:
模型(类)是实体方案的简单表示。模型(对象)是单个实体。该服务在模型上运行并向控制器提供具体数据。。没有控制器有任何型号。这些模型是独立的。
另一方面,映射器将模型映射到持久层(例如:数据库、第三方后端等)。

A simple solution using service and collection:

<?php
class MyController {
    public function index() {
        $postService = ServiceContainer::get('Post');
        $postCollection = $postService->findAllByDateRange('within 30 days');
        while($postCollection->getNext()) {
            echo $postCollection->current()->getAuthor();
        }
    }
}

EDIT:
The model(class) is the simple representation of the entity scheme. The model(object) is a single entity. The service operates on models and provides concrete data to the controllers. No controller has any model. The models stand alone.
On the other "side", mappers map the models into persistance layers (e.g: databases, 3rd party backends, etc).

明天过后 2024-09-12 19:17:24

虽然这是专门针对 Ruby on Rails 的,但由于讨论是围绕 MVC 和 DDD 进行的,因此相同的原则和信息仍然适用。

http://blog.scottbellware.com/ 2010/06/no-domain-driven-design-in-rails.html

while this is specifically about Ruby on Rails, the same principles and information still apply since the discussion is around MVC and DDD.

http://blog.scottbellware.com/2010/06/no-domain-driven-design-in-rails.html

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