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

SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
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
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 

Recently uploaded (20)

SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
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
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 

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