域对象应该包含其映射器吗?
给定一个域对象(例如,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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
我有点不清楚 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.