了解域对象+数据映射器模式吗? [英] Understanding the Domain object + Data mapper pattern?

查看:111
本文介绍了了解域对象+数据映射器模式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去,我一直在与各种ORM一起工作,并将所有逻辑放在模型中,无论其性质如何-SQL,MongoDB查询&甚至获取远程JSON对象. 但是,当需要确保松散的联轴器以实现高水平的可测试性时,这种方法的问题很快就会出现.

I've always worked with various ORM's in the past and placed all my logic inside my models regardless of it's nature - SQL, MongoDB queries & even fetching of remote JSON objects. But when it's necessary to ensure loose couplings to allow a high level of testability, the issues of this methodology quickly appears.

今天,我已经读到了将模型分为两部分的内容,分别为Domain objects& Data mappers.
如果我完全理解的话,Domain objects完全不知道所使用的存储,而是可以处理业务逻辑.另一方面,Data mappers负责将Domain objects中设置的数据存储到设置的数据存储中.

Today I've read about separating models into two parts, Domain objects & Data mappers.
If I understood it completely, Domain objects are completely unaware of the storage used, and instead exists to handle business logic. Data mappers on the other hand takes care of storing the data set in the Domain objects to a set data storage.

但是,我确实很难在网上找到一个很好的,易于理解的示例,以了解如何与DomainObjects&真实示例中的DataMappers.

I do however find it a bit hard to find a good, easy-to-understand example online on how to work with the DomainObjects & DataMappers in a real world example.

这(下面显示的代码)是使用DomainObjects&的合适方法吗?在我的代码中存储用户的DataMappers还是我脑子里弄错了?

Would this (below shown code) be the appropriate way to work with DomainObjects & DataMappers in my code to store Users or have I gotten it all wrong in my head?

$user = new User_DO;
$userSave = new User_DM;
$userSave->store( $user->add(array('name' => 'John Doe')) );

class User_DO {

    function add($array) {
        if(!isset($array['name'])) {
            throw new Exception("Name must be set");
        }

        return $array;

    }

}

class User_DM {

    function store($array) {
        MyDatabase::execute("INSERT INTO...");
    }

}

推荐答案

其背后的想法是拥有一个标准对象,该对象代表现实生活中的当前状态,或者换句话说,领域.此域模型通常是没有逻辑的数据集合.

The idea behind this is to have a standard object, that represents the current state in real life or in other words, in the domain. This domain model is usually a collection of data without logic.

class person_DO {
    public $id;
    public $firstname;
    public $lastname;
    public $addresses;
}

此域模型实例(域对象)的加载和持久性通过数据映射器处理-例如上述人员的地址可能通过1:n关系位于另一个表中,例如:

The loading of instances of this domain model (domain objects) and the persistence is handled through data mappers - e.g. the address of the above person might be located in another table via a 1:n relationship, as such:

TABLE person {
    id        INTEGER PRIMARY KEY,
    firstname VARCHAR(32),
    lastname  VARCHAR(32)
}

TABLE addresses {
    id INTEGER PRIMARY KEY,
    person_id  INTEGER FOREIGN KEY ON person.id, --Reference on person-row
    street     VARCHAR(64),
    ...
}

person_DO不需要知道这一点,但是datamapper知道,因为它必须在加载期间聚合数据并在持久期间分离数据:

The person_DO does not need to know about that, but the datamapper does, as it has to aggregate the data during loading and separate during persisting:

class person_DM {
    /**
     * @param  [integer] $id
     * @return [person_DO] an instance of a person or null, if no person
     *                     with that id was found.
     */
    public function findById ($id) {...}

    /**
     * @return [array of person_DO]
     */
    public function fetchAll() {...}

    /**
     * persists a person object
     * @param [person_DO] an instance of a person
     */
    public function saveOrUpdate(person_DO $person) {...}
}

为了进一步分离不同的部分,DataMappers通常使用 DbTable Gateway 或类似的模式来允许使用不同的数据库或类似的动作.这样,我可以拥有多个具有相同架构的数据库,例如在不同的组织中使用相同的代码构建数据仓库,只是使用不同的数据库对象.

In order to decouple the different parts even more, the DataMappers usually use the DbTable Gateway or a similar pattern to allow the use of different databases or similar actions. That way, I can have several databases with the same schemas, but e.g. in different organizations to build a data warehouse with the same code, only different database objects.

作为一个实际示例,我建议您查看快速入门教程,它正是我刚刚简要解释过的.

As a practical example, I would suggest looking at the Quickstart Tutorial of the Zend Framework, which does exactly what I just explained briefly.

这篇关于了解域对象+数据映射器模式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆