SlideShare a Scribd company logo
1 of 13
Proved PHP Design Patterns for
       Data Persistence
   and the evolution to Identity-Map
Topics & Cowboys




             Active-Record Pattern
             Data-Mapper Pattern
             Identity-Map & implementation


20.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 object

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

$person1->setLastName('Bob');

echo $person1->getLastName(); // Bob
echo $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 object

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

$person1->setLastName('Bob');

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




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

More Related Content

What's hot (10)

MongoD Essentials
MongoD EssentialsMongoD Essentials
MongoD Essentials
 
MongoDB & Mongomapper 4 real
MongoDB & Mongomapper 4 realMongoDB & Mongomapper 4 real
MongoDB & Mongomapper 4 real
 
MongoDB GeoSpatial Feature
MongoDB GeoSpatial FeatureMongoDB GeoSpatial Feature
MongoDB GeoSpatial Feature
 
Mongodb railscamphh
Mongodb railscamphhMongodb railscamphh
Mongodb railscamphh
 
Embedding a language into string interpolator
Embedding a language into string interpolatorEmbedding a language into string interpolator
Embedding a language into string interpolator
 
Mongo db문서의생성,갱신,삭제
Mongo db문서의생성,갱신,삭제Mongo db문서의생성,갱신,삭제
Mongo db문서의생성,갱신,삭제
 
Using Dojo
Using DojoUsing Dojo
Using Dojo
 
Scaling MongoDB; Sharding Into and Beyond the Multi-Terabyte Range
Scaling MongoDB; Sharding Into and Beyond the Multi-Terabyte RangeScaling MongoDB; Sharding Into and Beyond the Multi-Terabyte Range
Scaling MongoDB; Sharding Into and Beyond the Multi-Terabyte Range
 
Green dao
Green daoGreen dao
Green dao
 
Jongo mongo sv
Jongo mongo svJongo mongo sv
Jongo mongo sv
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

Proved PHP Design Patterns for Data Persistence

  • 1. Proved PHP Design Patterns for Data Persistence and the evolution to Identity-Map
  • 2. Topics & Cowboys Active-Record Pattern Data-Mapper Pattern Identity-Map & implementation 20.04.2012 Gjero Krsteski 2
  • 3. 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
  • 4. 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
  • 5. 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(); // Joe echo $person2->getLastName(); // Joe $person1->setLastName('Bob'); echo $person1->getLastName(); // Bob echo $person2->getLastName(); // Joe -> ?!? 20.04.2012 Gjero Krsteski 5
  • 6. 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
  • 7. Identity-Map workflow for method find() 20.04.2012 Gjero Krsteski 7
  • 8. 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(); // Joe echo $person2->getLastName(); // Joe $person1->setLastName('Bob'); echo $person1->getLastName(); // Bob echo $person2->getLastName(); // Bob -> yes, much better 20.04.2012 Gjero Krsteski 8
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. Thank you for your attention! Gjero Krsteski Programmer, Consultant, Trainer Homepage: krsteski.de E-Mail: gjero@krsteski.de