【问题标题】:PoEEA logic of DataMapper / Domain Driven DesignDataMapper/领域驱动设计的PoEEA逻辑
【发布时间】: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


    【解决方案1】:

    进一步阅读,我发现我的错误:

    “数据映射器是将内存中的对象与数据库分开的软件层。它的职责是在两者之间传输数据,并将它们彼此隔离。” - Martin Fowler, P of EAA (Data Mapper)

    所以我的困惑是,数据映射器(具体)应该知道内存中的对象(实例化的模型对象),它只是将它们与数据库(数据访问对象)隔离开来。

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      • 2011-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多