dongxun8189 2010-06-12 20:12
浏览 281
已采纳

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

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?

  • 写回答

5条回答 默认 最新

  • doucuo4413 2010-06-12 20:45
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角