問題描述
我不久前問了這個問題但現在我希望在我的數據庫訪問層和域層之間實現實際分離.我還將努力將業務邏輯移入它所屬的域中并移出控制器腳本.
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,它為數據訪問層實現了表數據網關和行數據網關模式,但它顯然沒有真正定義如何構建與數據訪問層分離的域層.我考慮過使用 Active Record 模式,其中域邏輯與數據訪問邏輯共存,但我認為 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.
每個用戶類型(管理員、采購員、助理、主管)都有與之關聯的特定業務邏輯,并且所有類型都從 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.
我不想使用專門屬于一種類型用戶的業務邏輯來膨脹行數據網關對象,但我不確定如何構建域層來表示不同類型的用戶.例如,我是創建一個包含 PersonGateway 對象的 Person 對象,然后編寫將調用傳遞給網關對象的包裝函數,還是編寫 Person 對象來擴展 PersonGateway 對象,然后僅實現我需要的特定功能?
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?
任何關于如何在 Zend_Db 之上正確創建域模型的教程的建議或鏈接將不勝感激.
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.
推薦答案
域模型不擴展任何內容.它們只是用于封裝業務邏輯的普通類.他們可能使用數據訪問對象,所以在類內部可能有一個行數據網關對象的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 類具有一個接口,其中包含與您可以使用該類執行的更高級別操作相對應的方法.但您不一定要顯示所有數據訪問操作.
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) { ... }
}
我去年 春季也寫了關于這個主題的博客,并在 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/
這篇關于如何使用 Zend Framework 正確創建域?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!