問題描述
我不久前問了這個(gè)問題但現(xiàn)在我希望在我的數(shù)據(jù)庫(kù)訪問層和域?qū)又g實(shí)現(xiàn)實(shí)際分離.我還將努力將業(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ù)訪問層實(shí)現(xiàn)了表數(shù)據(jù)網(wǎng)關(guān)和行數(shù)據(jù)網(wǎng)關(guān)模式,但它顯然沒有真正定義如何構(gòu)建與數(shù)據(jù)訪問層分離的域?qū)?我考慮過使用 Active Record 模式,其中域邏輯與數(shù)據(jù)訪問邏輯共存,但我認(rèn)為 Active Record 至少會(huì)處理以下情況:
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:
我有一個(gè)包含 person_id 和 userType 字段的Person"表.
I have a single table "Person" which contains person_id and userType fields.
每個(gè)用戶類型(管理員、采購(gòu)員、助理、主管)都有與之關(guān)聯(lián)的特定業(yè)務(wù)邏輯,并且所有類型都從 Person 對(duì)象繼承了一些基本功能.
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)對(duì)象,但我不確定如何構(gòu)建域?qū)觼肀硎静煌愋偷挠脩?例如,我是創(chuàng)建一個(gè)包含 PersonGateway 對(duì)象的 Person 對(duì)象,然后編寫將調(diào)用傳遞給網(wǎng)關(guān)對(duì)象的包裝函數(shù),還是編寫 Person 對(duì)象來擴(kuò)展 PersonGateway 對(duì)象,然后僅實(shí)現(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?
同樣,我通常認(rèn)為這是(部分)一個(gè)工廠問題,我需要一個(gè)工廠方法來實(shí)例化基于 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.
推薦答案
域模型不擴(kuò)展任何內(nèi)容.它們只是用于封裝業(yè)務(wù)邏輯的普通類.他們可能使用數(shù)據(jù)訪問對(duì)象,所以在類內(nèi)部可能有一個(gè)行數(shù)據(jù)網(wǎng)關(guān)對(duì)象的protected
實(shí)例.Row
對(duì)象通常比 Table
對(duì)象更接近地表示域的實(shí)例.此外,您始終可以使用Row
的getTable()
方法獲取Table
對(duì)象.
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 類具有一個(gè)接口,其中包含與您可以使用該類執(zhí)行的更高級(jí)別操作相對(duì)應(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)于這個(gè)主題的博客,并在 ZF 郵件列表上寫了它 最近.
I also blogged about this subject last spring, and wrote about it on the ZF mailing list recently.
就教程和資源而言,請(qǐng)嘗試 http://domaindrivendesign.org/
As far as tutorials and resources, try http://domaindrivendesign.org/
這篇關(guān)于如何使用 Zend Framework 正確創(chuàng)建域?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!