Persistence Mechanism Generates Identity

Usually, the simplest way of generating the Identity is to delegate it to the persistence mechanism, because the vast majority of persistence mechanisms support some kind of Identity generation — like MySQL's AUTO_INCREMENT attribute or Postgres and Oracle sequences. This, although simple, has a major drawback: we won't have the Identity of the Entity until we persist it. So to some degree, if we're going with persistence mechanism-generated Identities, we'll couple the Identity operation with the underlying persistence store:

CREATE TABLE `orders` (
`id` int(11) NOT NULL auto_increment,
`amount` decimal (10,5) NOT NULL,
`first_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

And then we might consider this code:

namespace DddIdentityDomainModel;

class Person
{
private $identificationNumber;
private $firstName;
private $lastName;

public function __construct(
$anIdentificationNumber, $aFirstName, $aLastName
) {
$this->identificationNumber = $anIdentificationNumber;
$this->firstName = $aFirstName;
$this->lastName = $aLastName;
}

public function identificationNumber()
{
return $this->identificationNumber;
}

public function firstName()
{
return $this->firstName;
}

public function lastName()
{
return $this->lastName;
}
}

If you've ever tried to build your own ORM, you've already experienced this situation. What's the approach for creating a new Person? If the database is going to generate the Identity, do we have to pass it in the constructor? When and where is the magic that will update the Person with its Identity? What happens if we end up not persisting the Entity?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset