Proved PHP Design Patterns for Data Persistence
Upcoming SlideShare
Loading in...5
×
 

Proved PHP Design Patterns for Data Persistence

on

  • 6,097 views

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

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

Statistics

Views

Total Views
6,097
Views on SlideShare
5,317
Embed Views
780

Actions

Likes
1
Downloads
16
Comments
1

6 Embeds 780

http://krsteski.de 767
http://paper.li 4
http://www.thewebhatesme.com 4
http://translate.googleusercontent.com 3
http://131.253.14.66 1
https://twitter.com 1

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Please read this blog post to learn more about http://krsteski.de/php-tricks-und-tipps/objekten-abrufen-und-mehrere-instanzen-vermeiden.html
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Proved PHP Design Patterns for Data Persistence Proved PHP Design Patterns for Data Persistence Presentation Transcript

  • Proved PHP Design Patterns for Data Persistence and the evolution to Identity-Map
  • Topics & Cowboys Active-Record Pattern Data-Mapper Pattern Identity-Map & implementation20.04.2012 Gjero Krsteski 2
  • Active-Record Pattern + insert() + update() + delete() + Table properties - No separation of concerns. - Difficult testing without a database. ------------------------------- = For systems with simpler domain logic.20.04.2012 Gjero Krsteski 3
  • Data-Mapper Pattern + 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.20.04.2012 Gjero Krsteski 4
  • Data-Mapper Pattern problem$personMapper = new PersonMapper($pdo);$person1 = $personMapper->find(1); // creates new object$person2 = $personMapper->find(1); // creates new objectecho $person1->getLastName(); // Joeecho $person2->getLastName(); // Joe$person1->setLastName(Bob);echo $person1->getLastName(); // Bobecho $person2->getLastName(); // Joe -> ?!?20.04.2012 Gjero Krsteski 5
  • Identity-Map Pattern + 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.20.04.2012 Gjero Krsteski 6
  • Identity-Map workflow for method find()20.04.2012 Gjero Krsteski 7
  • Data-Mapper Pattern with Identity-Map$personMapper = new PersonMapper($pdo);$person1 = $personMapper->find(1); // creates new object$person2 = $personMapper->find(1); // returns same objectecho $person1->getLastName(); // Joeecho $person2->getLastName(); // Joe$person1->setLastName(Bob);echo $person1->getLastName(); // Bobecho $person2->getLastName(); // Bob -> yes, much better20.04.2012 Gjero Krsteski 8
  • 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.}20.04.2012 Gjero Krsteski 9
  • 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.}20.04.2012 Gjero Krsteski 10
  • 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.}20.04.2012 Gjero Krsteski 11
  • 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.}20.04.2012 Gjero Krsteski 12
  • Thank you for your attention! Gjero Krsteski Programmer, Consultant, Trainer Homepage: krsteski.de E-Mail: gjero@krsteski.de