久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

DDD 和 MVC:“模型"和“實體"之間的區別

DDD and MVC: Difference between #39;Model#39; and #39;Entity#39;(DDD 和 MVC:“模型和“實體之間的區別)
本文介紹了DDD 和 MVC:“模型"和“實體"之間的區別的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我對 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

Entity 表示作為業務邏輯使用的單個項目的對象,更具體地說是那些具有某種身份的對象.
因此,許多人將 ORM 映射的對象稱為實體.

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 與應用程序的 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 類可能是模型的一部分,但它顯然不是一個實體.
  • 如果有一個類在你的業務邏輯層中間使用但不暴露給應用程序的其余部分,它可能是一個實體,但從 MVC 的角度來看,它顯然不是一個模型應用程序.
  • 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.

你的例子

至于您的代碼示例,我更喜歡第一個.
模型是用作應用程序數據抽象手段的類,而不是名稱后綴為模型"的類.許多人認為后者是膨脹軟件.

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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Action View Helper in Zend - Work around?(Zend 中的動作視圖助手 - 解決方法?)
Is this a good way to match URI to class/method in PHP for MVC(這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方法嗎)
Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(視圖),以便我的應用程序中的所有視圖都可以訪問?) - IT屋-程序員軟件開發技術
Having a single entry point to a website. Bad? Good? Non-issue?(有一個網站的單一入口點.壞的?好的?沒問題?)
Is MVC + Service Layer common in zend or PHP?(MVC + 服務層在 Zend 或 PHP 中常見嗎?)
Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)
主站蜘蛛池模板: 国产伊人精品 | 伊人伊人 | 精品乱码一区二区三四区视频 | 久久蜜桃资源一区二区老牛 | 1级黄色大片 | 久久久av | 视频一二三区 | 欧美成视频 | 亚洲精品一区二区三区在线 | 亚洲一区二区在线视频 | 亚洲一区二区三区免费视频 | 又爽又黄axxx片免费观看 | 亚洲狠狠爱 | 亚洲成色777777在线观看影院 | 亚洲天堂av网 | 日韩黄色av | 91精品国产综合久久久亚洲 | 午夜精品久久久久久久99黑人 | 国产超碰人人爽人人做人人爱 | 毛片a| 欧美亚洲第一区 | 成人亚洲精品 | 中文字幕日韩一区 | 国产在线色| 交专区videossex农村 | 狠狠亚洲 | 日韩中文在线视频 | 999免费视频| 国产高清免费 | 国产精品一区二区在线免费观看 | 精品一区二区在线观看 | 欧美精品久久久 | 日韩欧美一区二区三区 | 男女深夜网站 | 91久久国产综合久久 | www.4虎影院| 国精产品一区二区三区 | 三级黄色大片网站 | 久久成人午夜 | aa级毛片毛片免费观看久 | 一区二区在线免费观看 |