問題描述
我不久前問了這個問題但現(xiàn)在我希望在我的數(shù)據(jù)庫訪問層和域?qū)又g實現(xiàn)實際分離.我還將努力將業(yè)務(wù)邏輯移入它所屬的域中并移出控制器腳本.
I asked this question a while back but now I'm looking to implement an actual separation between my database access layer and the domain layer. I am also going to be working to move business logic into the domain where it belongs and out of the controller scripts.
我正在使用 Zend Framework,它為數(shù)據(jù)訪問層實現(xiàn)了表數(shù)據(jù)網(wǎng)關(guān)和行數(shù)據(jù)網(wǎng)關(guān)模式,但它顯然沒有真正定義如何構(gòu)建與數(shù)據(jù)訪問層分離的域?qū)?我考慮過使用 Active Record 模式,其中域邏輯與數(shù)據(jù)訪問邏輯共存,但我認為 Active Record 至少會處理以下情況:
I'm using Zend Framework which implements the Table Data Gateway and Row Data Gateway patterns for the data access layer, but it apparently fails to really define how to build a domain layer that is separate from the data access layer. I've considered using an Active Record pattern where the domain logic coexists with the data access logic, but I have the following situation that occurs at least once that I don't think Active Record will handle:
我有一個包含 person_id 和 userType 字段的Person"表.
I have a single table "Person" which contains person_id and userType fields.
每個用戶類型(管理員、采購員、助理、主管)都有與之關(guān)聯(lián)的特定業(yè)務(wù)邏輯,并且所有類型都從 Person 對象繼承了一些基本功能.
Each userType (admin, buyer, associate, supervisor) has specific business logic associated with it and all types inherit some basic functionality from a Person object.
我不想使用專門屬于一種類型用戶的業(yè)務(wù)邏輯來膨脹行數(shù)據(jù)網(wǎng)關(guān)對象,但我不確定如何構(gòu)建域?qū)觼肀硎静煌愋偷挠脩?例如,我是創(chuàng)建一個包含 PersonGateway 對象的 Person 對象,然后編寫將調(diào)用傳遞給網(wǎng)關(guān)對象的包裝函數(shù),還是編寫 Person 對象來擴展 PersonGateway 對象,然后僅實現(xiàn)我需要的特定功能?
I don't want to bloat the Row Data Gateway object with business logic that belongs specifically to just one type of user but I'm not certain how to construct the domain layer to represent the different types of users. For example, do I make a Person object that contains the PersonGateway object and then write wrapper functions that pass calls to the gateway object, or do I write the Person object to extend the PersonGateway object and then only implement the specific functions that I need?
同樣,我通常認為這是(部分)一個工廠問題,我需要一個工廠方法來實例化基于 userType 的正確子類.這仍然是 Zend Framework 的 Zend_Db 類的最佳方法嗎?
Likewise, I would typically think that this is (in part) a factory problem where I need a factory method that will instantiate the correct sub-class based on userType. Is that still the best method here with Zend Framework's Zend_Db class?
任何關(guān)于如何在 Zend_Db 之上正確創(chuàng)建域模型的教程的建議或鏈接將不勝感激.
Any suggestions or links to tutorials that talk about how to properly create a domain model on top of Zend_Db would be greatly appreciated.
推薦答案
域模型不擴展任何內(nèi)容.它們只是用于封裝業(yè)務(wù)邏輯的普通類.他們可能使用數(shù)據(jù)訪問對象,所以在類內(nèi)部可能有一個行數(shù)據(jù)網(wǎng)關(guān)對象的protected
實例.Row
對象通常比 Table
對象更接近地表示域的實例.此外,您始終可以使用Row
的getTable()
方法獲取Table
對象.
Domain Models extend nothing. They're just plain classes you use to encapsulate business logic. They may use data access objects, so there may be a protected
instance of a row data gateway object inside the class. A Row
object usually represents an instance of the domain more closely than a Table
object. Besides, you can always get the Table
object with the Row
's getTable()
method.
通常,DM 類具有一個接口,其中包含與您可以使用該類執(zhí)行的更高級別操作相對應(yīng)的方法.但您不一定要顯示所有數(shù)據(jù)訪問操作.
Typically DM classes have an interface with methods corresponding to higher-level operations you can do with that class. But you don't necessarily want to surface all data access operations.
class Person {
// Zend_Db_Table_Row object
protected $data;
public function subscribeToService(Service $service) { ... }
public function sendMailTo(Person $recipient) { ... }
public function changePassword($newPassword) { ... }
}
我去年 春季也寫了關(guān)于這個主題的博客,并在 ZF 郵件列表上寫了它 最近.
I also blogged about this subject last spring, and wrote about it on the ZF mailing list recently.
就教程和資源而言,請嘗試 http://domaindrivendesign.org/
As far as tutorials and resources, try http://domaindrivendesign.org/
這篇關(guān)于如何使用 Zend Framework 正確創(chuàng)建域?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!