Facebook
Twitter
Google+
Kommentare
0

Objekte abrufen und mehrere Instanzen vermeiden

Auf Englisch würde das etwa als „Retrieve objects avoiding multiple instances” geschrieben werden. Den Artikel habe ich auf Englisch verfasst, da ich von phpclasses.org gebeten wurde paar Zeilen Dokumentation zum Projekt „Building an Identity Map in PHP” zu schreiben. Das Projekt zeigt auf wie eine Identity-Map im PHP Projekt implementiert wird und was für Vorteile das Ganze mit sich bringt. So, ab heir ist der Artikel auf Englisch verfasst: The „Building an Identity Map in PHP” package can store and retrieve objects in persistent storage containers avoiding to have multiple instances of the same object in memory. It can use a mapper class to store objects of a class in a container like for instance a database table. It can also retrieve the objects from the container assuring that only one instance of the same object is retrieved into memory.

This package requires

– PDO a lightweight, consistent interface for accessing databases in PHP.
– PHPUnit a unit testing framework for PHP projects.

This package implements

Data-Mapper Pattern
Identity-Map Pattern

Why identity mapping?

By using Data-Mapper pattern without an identity map, you can easily run into problems because you may have more than one object that references the same domain entity.

Data-Mapper without identity map

      $userMapper = new UserMapper($pdo);

      $user1 = $userMapper->find(1); // creates new object
      $user2 = $userMapper->find(1); // creates new object

      echo $user1->getNickname(); // joe123
      echo $user2->getNickname(); // joe123

      $user1->setNickname('bob78');

      echo $user1->getNickname(); // bob78
      echo $user2->getNickname(); // joe123 -> ?!?

Data-Mapper with identity map

The identity map solves this problem by acting as a registry for all loaded domain instances.

      $userMapper = new UserMapper($pdo);

      $user1 = $userMapper->find(1); // creates new object
      $user2 = $userMapper->find(1); // returns same object

      echo $user1->getNickname(); // joe123
      echo $user2->getNickname(); // joe123

      $user1->setNickname('bob78');

      echo $user1->getNickname(); // bob78
      echo $user2->getNickname(); // bob78 -> yes, much better

By using an identity map you can be confident that your domain entity is shared throughout your application for the duration of the request. Note that using an identity map is not the same as adding a cache layer to your mappers. Although caching is useful and encouraged, it can still produce duplicate objects for the same domain entity.

Source & UML

Take a look at the php-identity-map repository on GitHub
php-identity-map-with-one-2-many-uml

Process Workflow

Martin Fowler says: “If you load the same data more than once you’re incurring an expensive cost in remote calls. Thus, not loading the same data twice doesn’t just help correctness, but can also speed up your application. An Identity Map keeps a record of all objects that have been read from the database in a single business transaction. Whenever you want an object, you check the Identity Map first to see if you already have it.”

identity map workflow

Presentation

Über den Autor

Gjero Krsteski

Link erfolgreich vorgeschlagen.

Vielen Dank, dass du einen Link vorgeschlagen hast. Wir werden ihn sobald wie möglich prüfen. Schließen