Zend Framework ORM样式表数据网关与扩展Zend_Db_Table_Abstract [英] Zend Framework ORM-style table data gateway vs. extending Zend_Db_Table_Abstract
问题描述
在 Zend Framework快速入门中,从扩展Zend_Db_Table_Abstract
到Table Data Gateway模式的模型发生了变化.
In the Zend Framework Quickstart, there has been a change from models that extend Zend_Db_Table_Abstract
to the Table Data Gateway pattern.
我个人对这种模式没有太多的经验,我一直在听,这很可能应该被用来代替旧的方式.
Personally, I have not had much experience with this pattern and I keep hearing this should most likely be used instead of the old way.
快速入门的简短示例:
旧方法:
class Default_Model_Guestbook extends Zend_Db_Table_Abstract
{
protected $_name = 'tablename';
// do stuff
}
新方法:
// The actual model
class Default_Model_Guestbook
{
protected $_comment;
protected $_created;
protected $_poster;
// list continues with all columns
}
// Dbtable for this model
class Default_Model_DbTable_Guestbook extends Zend_Db_Table_Abstract
{
/** Table name */
protected $_name = 'guestbook';
}
// Mapper
class Default_Model_GuestbookMapper
{
public function save($model);
public function find($id, $model);
public function fetchAll();
}
由于我缺乏这种编程风格的经验,因此我很难掌握后一种方法的实际好处;我知道这种方法尽可能将数据库与实际逻辑分开,从理论上讲,这应该使向另一个数据库平台的转换更加容易.但是,我真的没有在我正在从事的任何项目中看到这种情况.
From my lacking experience with this style of programming, I find it hard to grasp the actual benefits from this latter way; I understand that this method seperates the database from the actual logic as much as possible, which should in theory make a transition to another database platform easier. However, I really don't see this happening on any project I am working.
几乎毫无疑问,我正在忽略某些东西,因此,我很想听听您的建议.
There is almost no doubt that I am overlooking something, so I'd love to hear your advice.
问题:
-
有人可以向我解释为什么(或是否)后者是更好的做法?
Could someone please explain to me why (or if) the latter is better practice?
我应该从旧方法切换到新方法,还是仍然有适当的理由坚持使用代表数据库表的模型?
Should I switch from the old way to the new way or are there still proper reasons for sticking with models that represent database tables?
谢谢.
推荐答案
以下是我对这是一种更好做法的解释:
Here's my explanation at why this is a better practice:
我认为,真正的好处是可以无缝更改数据源.通过在应用程序中添加额外的抽象层,您的模型不再代表数据库表(我认为它永远都不会拥有),因为模型应该代表数据(而不是数据的网关).数据库访问层应由模型封装,以提供更大的灵活性.
I think the real benefit to this is the ability to seamlessly change your data sources. By adding an additional layer of abstraction into your application, your models no longer represent a database table (it never should have, in my opinion) as a model should be a representation of the data (not a gateway to it). The database access layer should be encapsulated by the model, allowing you more flexibility.
例如,说您的应用程序需要开始使用SOAP服务或XML-RPC作为数据源/存储.通过使用数据映射器方法,您将具有明显的优势,因为您已经具备添加这些特定数据层接口的必要结构,而又不会对现有模型产生太多(如果有)干扰.
Say, for instance, your application needed to start using a SOAP service or XML-RPC as it's data source/storage. By using the data mapper approach, you are at a distinct advantage, as you already have the necessary structure in place to add these specific data layer interfaces without much (if any) interference with your existing models.
但是,您应该这样做吗?这是一个务实的问题.就个人而言,我喜欢放心,我正在开发一些灵活的方法,并遵循(遵循)最佳实践.但是,只有您知道创建更灵活的应用程序是否会使您的项目现在或将来更容易构建和维护.
Should you do it, though? That's a pragmatic question. Personally, I like to have the peace of mind that I'm developing something that is flexible and follows (agreed on) best practices. However, only you know whether creating a more flexible application will make your projects easier, either now or some time in the future, to build and maintain.
对于我来说,我喜欢自己正在建造某种东西的感觉,我认为这是最佳实践,并且通常会带来回报.
For me, I just like the feeling that I'm building something, I consider to be best-practice and it often pays dividends.
这篇关于Zend Framework ORM样式表数据网关与扩展Zend_Db_Table_Abstract的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!