【发布时间】:2011-11-18 05:27:31
【问题描述】:
我对 DataMapper 模式的逻辑有点困惑。
到目前为止我所拥有的(伪代码,不是在这里寻找特定语言的答案):
class Car implements DomainObjectAbstract {
... properties ...
... accessors / mutators ...
... behaviors ... // drive, park, whatever.
}
class House implements DomainObjectAbstract {
... properties ...
... accessors / mutators ...
// may not have behaviors, a house doesn't 'do anything, it just is'
}
class DaoCar implements DaoAbstract {
// some code to handle reading and writing car object to database table
}
class DaoHouse implements DaoAbstract {
// some code to handle reading and/or writing house object to an xml file
}
class DataMapper {
protected _dao;
protected _model;
function getDao() return this->_dao;
function setDao( DomainObjectAbstract dao) this->_dao = dao;
function getModel() return this->_model;
function setModel( DaoAbstract model ) this->_model = model;
function create() { // create a new instance of the model }
function save() { // create or update the model into persistent storage }
function fetch( id ) { // fetch record from storage and return a property populated model }
... etc ...
}
问题:多个 DataMapper 的目的是什么?
我的意思是,为什么我会有 HouseDataMapper 和 CarDataMapper,考虑到 DataMapper 不应该关心模型或 dao,除非它遵循实现规则。我唯一的想法是针对不同模型和数据访问对象的不同抽象(规则)的不同 DataMapper。我问是因为我看到的大多数示例都将 HouseDataMapper 和 CarDataMapper 视为独立的实体,这不会破坏 DataMapper PoEEA 的目的吗?
【问题讨论】:
标签: design-patterns datamapper