views:

134

answers:

2

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);
    }
}
+5  A: 

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

James L
+1  A: 

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.

blockhead