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

MongoD Essentials
MongoD EssentialsMongoD Essentials
MongoD Essentialszahid-mian
 
MongoDB & Mongomapper 4 real
MongoDB & Mongomapper 4 realMongoDB & Mongomapper 4 real
MongoDB & Mongomapper 4 realjan_mindmatters
 
MongoDB GeoSpatial Feature
MongoDB GeoSpatial FeatureMongoDB GeoSpatial Feature
MongoDB GeoSpatial FeatureHüseyin BABAL
 
Embedding a language into string interpolator
Embedding a language into string interpolatorEmbedding a language into string interpolator
Embedding a language into string interpolatorMichael Limansky
 
Mongo db문서의생성,갱신,삭제
Mongo db문서의생성,갱신,삭제Mongo db문서의생성,갱신,삭제
Mongo db문서의생성,갱신,삭제홍준 김
 
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 RangeMongoDB
 

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

CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 

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