gpt4 book ai didi

php - 在 Zend 中,为什么我们将 DB Model 类和 Mapper 类分开使用?

转载 作者:可可西里 更新时间:2023-11-01 12:44:38 25 4
gpt4 key购买 nike

我正在做zend项目,我引用了其他zend项目来创建新的Zend项目。但我不喜欢在不了解的情况下盲目跟随那个项目。在 Zend Directory 结构中,在 Model 类中,我看到主要有两种类型的类,如

- models
- DbTables
- Blog.php //Extends Zend_Db_Table_Abstract
- Blog.php // Contains methods like validate() and save()
- BlogMapper.php // Also Contains methods like validate(Blog b) & save(Blog b)

为什么要遵循这个特定的结构?这是为了分离Object类和Database模型类吗?

请解释。

最佳答案

DataMapper is a design pattern from Patterns of Enterprise Application Architecture .

The Data Mapper is a layer of software that separates the in-memory objects from the database. Its responsibility is to transfer data between the two and also to isolate them from each other. With Data Mapper the in-memory objects needn't know even that there's a database present; they need no SQL interface code, and certainly no knowledge of the database schema.

在关系数据库中存储数据的方式通常不同于在内存中构造对象的方式。例如,一个对象将有一个包含其他对象的数组,而在数据库中,您的表将有一个指向另一个表的外键。因为 object-relational impedance mismatch ,您在域对象和数据库之间使用了一个中介层。这样,您可以在不影响对方的情况下同时发展两者。

在自己的层中分离 Mapping 职责也更紧密地遵循 Single Responsibility Principle .您的对象不需要了解数据库逻辑,反之亦然。这使您在编写代码时具有更大的灵 active 。

当您不想使用域模型时,通常不需要 DataMapper。如果您的数据库表很简单,您最好使用 TableModule 和 TableDataGateway,甚至只使用 ActiveRecord。

对于其他各种模式,请参阅我的回答

关于php - 在 Zend 中,为什么我们将 DB Model 类和 Mapper 类分开使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9329881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com