域对象应该包含其映射器吗?

发布于 2024-07-12 10:06:23 字数 516 浏览 6 评论 0原文

给定一个域对象(例如,Person),该对象是否应该包含其数据映射器(Person_Mapper)?

例如,我可以通过这两种不同的方式进行停用操作:

$mapper = new Person_Mapper();

$person = $mapper->load(1);

$person->active = false;
$mapper->save($person);

或者像这样:

$mapper = new Person_Mapper();

$person = $mapper->load(1);

$person->inactivate();


class Person
{
    public function inactivate()
    {
            $this->active = false;
            $this->_mapper->save($this);
    }
}

Given a domain object (say, for example, Person), should that object contain its Data Mapper (Person_Mapper)?

For example, I could have an inactivate action work in these two different ways:

$mapper = new Person_Mapper();

$person = $mapper->load(1);

$person->active = false;
$mapper->save($person);

Or like this:

$mapper = new Person_Mapper();

$person = $mapper->load(1);

$person->inactivate();


class Person
{
    public function inactivate()
    {
            $this->active = false;
            $this->_mapper->save($this);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有心的人 2024-07-19 10:06:24

Person 类应该只知道 Person 的东西,因此不应该包含与数据映射有关的任何内容。

请参阅http://en.wikipedia.org/wiki/Single_responsibility_principle

The Person class should only know Person stuff, therefore shouldn't contain anything to do with data mapping.

See http://en.wikipedia.org/wiki/Single_responsibility_principle

浅暮の光 2024-07-19 10:06:24

我有点不清楚 DAO 模式和数据映射器模式之间的关系,但是使用 DAO,Person 对象将返回一个传输对象,其非活动字段设置为 true,并将其交还给 Person DAO 来处理的。 person 对象根本不应该知道持久性。

I'm a little unclear as to the relationship between the DAO pattern and the Data Mapper pattern, but with DAO the Person object would return a transfer object with the inactive field set to true, and hand that back to the Person DAO to take care of. The person object should not know from persistence at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文