如何使用Zend Framework正确创建域?

时间:2021-09-14 11:11:13

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.

我前一段时间问过这个问题,但现在我正在寻求实现数据库访问层和域层之间的实际分离。我也将努力将业务逻辑移动到它所属的域和控制器脚本之外。

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:

我正在使用Zend Framework为数据访问层实现表数据网关和行数据网关模式,但显然无法真正定义如何构建与数据访问层分离的域层。我考虑过使用Active Record模式,其中域逻辑与数据访问逻辑共存,但我有以下情况至少发生一次,我认为Active Record不会处理:

I have a single table "Person" which contains person_id and userType fields.

我有一个表“Person”,其中包含person_id和userType字段。

Each userType (admin, buyer, associate, supervisor) has specific business logic associated with it and all types inherit some basic functionality from a Person object.

每个userType(admin,buyer,associate,supervisor)都具有与之关联的特定业务逻辑,所有类型都从Person对象继承一些基本功能。

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?

我不想使用专门属于一种类型用户的业务逻辑来膨胀Row Data Gateway对象,但我不确定如何构造域层来表示不同类型的用户。例如,我是否创建一个包含PersonGateway对象的Person对象,然后编写将调用传递给网关对象的包装函数,或者编写Person对象以扩展PersonGateway对象,然后只实现我需要的特定函数?

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?

同样,我通常认为这是(部分)工厂问题,我需要一个工厂方法,它将基于userType实例化正确的子类。这仍然是Zend Framework的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.

任何有关如何在Zend_Db上正确创建域模型的教程的建议或链接将不胜感激。

1 个解决方案

#1


16  

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.

域模型没有任何扩展。它们只是用于封装业务逻辑的普通类。它们可能使用数据访问对象,因此类中可能存在行数据网关对象的受保护实例。 Row对象通常比Table对象更接近地表示域的实例。此外,您始终可以使用Row的getTable()方法获取Table对象。

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.

通常,DM类具有一个接口,其方法对应于您可以对该类执行的更高级别的操作。但是,您不一定要展示所有数据访问操作。

class Person {
  // Zend_Db_Table_Row object
  protected $data; 

  public function subscribeToService(Service $service) { ... }

  public function sendMailTo(Person $recipient) { ... }

  public function changePassword($newPassword) { ... }
}

I also blogged about this subject last spring, and wrote about it on the ZF mailing list recently.

去年春天我也在博客上写过这个主题,最近在ZF邮件列表上写了这篇文章。

As far as tutorials and resources, try http://domaindrivendesign.org/

至于教程和资源,请尝试http://domaindrivendesign.org/

#1


16  

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.

域模型没有任何扩展。它们只是用于封装业务逻辑的普通类。它们可能使用数据访问对象,因此类中可能存在行数据网关对象的受保护实例。 Row对象通常比Table对象更接近地表示域的实例。此外,您始终可以使用Row的getTable()方法获取Table对象。

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.

通常,DM类具有一个接口,其方法对应于您可以对该类执行的更高级别的操作。但是,您不一定要展示所有数据访问操作。

class Person {
  // Zend_Db_Table_Row object
  protected $data; 

  public function subscribeToService(Service $service) { ... }

  public function sendMailTo(Person $recipient) { ... }

  public function changePassword($newPassword) { ... }
}

I also blogged about this subject last spring, and wrote about it on the ZF mailing list recently.

去年春天我也在博客上写过这个主题,最近在ZF邮件列表上写了这篇文章。

As far as tutorials and resources, try http://domaindrivendesign.org/

至于教程和资源,请尝试http://domaindrivendesign.org/