Thursday, March 24, 2011

Should a Domain Object Contain Its Mapper?

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);
    }
}
From stackoverflow
  • 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

  • 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.

0 comments:

Post a Comment