如何使用 Zend Framework 正确创建域?
我不久前问了这个问题但现在我希望在我的数据库访问层和域层之间实现实际分离.我还将努力将业务逻辑移入它所属的域中并移出控制器脚本.
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/
相关文章