proved php design patterns for data persistence

13
Proved PHP Design Patterns for Data Persistence and the evolution to Identity-Map

Upload: gjero-krsteski

Post on 15-Jan-2015

6.781 views

Category:

Technology


4 download

DESCRIPTION

Proved PHP Design Patterns for Data Persistence, and the evolution to Identity-Map.

TRANSCRIPT

Page 1: Proved PHP Design Patterns for Data Persistence

Proved PHP Design Patterns for Data Persistence

and the evolution to Identity-Map

Page 2: Proved PHP Design Patterns for Data Persistence

Topics & Cowboys

Active-Record PatternData-Mapper PatternIdentity-Map & implementation

Gjero Krsteski10.04.2023 2

Page 3: Proved PHP Design Patterns for Data Persistence

Active-Record Pattern

+ insert()+ update()+ delete()+ Table properties- No separation of

concerns.- Difficult testing

without a database.-------------------------------= For systems with

simpler domain logic.

Gjero Krsteski 310.04.2023

Page 4: Proved PHP Design Patterns for Data Persistence

Data-Mapper Pattern

Gjero Krsteski 410.04.2023

+ Decouples domain model class from the persistence store. + For systems with complex domain logic where the shape of the domain model will diverge from the database model.

Page 5: Proved PHP Design Patterns for Data Persistence

Data-Mapper Pattern problem

$personMapper = new PersonMapper($pdo);

$person1 = $personMapper->find(1); // creates new object $person2 = $personMapper->find(1); // creates new object

echo $person1->getLastName(); // Joeecho $person2->getLastName(); // Joe

$person1->setLastName('Bob');

echo $person1->getLastName(); // Bob echo $person2->getLastName(); // Joe -> ?!?

Gjero Krsteski 510.04.2023

Page 6: Proved PHP Design Patterns for Data Persistence

Identity-Map Pattern

Gjero Krsteski 610.04.2023

+ Ensures that each object gets loaded only once by keeping every loaded object in a map.

+ Looks up objects using the map when referring to them.

Page 7: Proved PHP Design Patterns for Data Persistence

Identity-Map workflow for method find()

Gjero Krsteski 710.04.2023

Page 8: Proved PHP Design Patterns for Data Persistence

Data-Mapper Pattern with Identity-Map

$personMapper = new PersonMapper($pdo);

$person1 = $personMapper->find(1); // creates new object $person2 = $personMapper->find(1); // returns same object

echo $person1->getLastName(); // Joeecho $person2->getLastName(); // Joe

$person1->setLastName('Bob');

echo $person1->getLastName(); // Bob echo $person2->getLastName(); // Bob -> yes, much better

Gjero Krsteski 810.04.2023

Page 9: Proved PHP Design Patterns for Data Persistence

Implementation of Identity-Map into find()

public function find($id){ // If the ID is in the Identity-Map, // then return the object from the Identity-Map.

// If not, then trie to fetch the object from the database. // If not found in the database, // than throw an exception -> no object with id=x exists! // If found in the database, then register the object in to the

Identity-Map.

// Return the object.}

Gjero Krsteski 910.04.2023

Page 10: Proved PHP Design Patterns for Data Persistence

Implementation of Identity-Map into insert()

public function insert(Person $person){ // Check if the object is not in the Identity-Map, // otherwise throw an exception -> object has an id, cannot insert!

// Store the object in the database. // Then register the object in the Identity-Map.

// Return the new object.}

Gjero Krsteski 1010.04.2023

Page 11: Proved PHP Design Patterns for Data Persistence

Implementation of Identity-Map into update()

public function update(Person $person){ // Check whether the object is in the Identity-Map, // if not, then throw an exception -> object has no id, cannot update!

// Otherwise, update the object in the database.

// Return true.}

Gjero Krsteski 1110.04.2023

Page 12: Proved PHP Design Patterns for Data Persistence

Implementation of Identity-Map into delete()

public function delete(Person $person){ // Check whether the object is in the Identity-Map, // if not, then throw an exception -> object has no id, cannot delete!

// Otherwise, delete the object in the database.

// Return true.}

Gjero Krsteski 1210.04.2023

Page 13: Proved PHP Design Patterns for Data Persistence

Gjero Krsteski Programmer, Consultant, Trainer

Homepage: krsteski.deE-Mail: [email protected]

Thank you for your attention!