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

Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
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
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
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
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 

Recently uploaded (20)

Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
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
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
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
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 

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