Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Active Record + biblioteka funkcji a połączenia, z bazą danych (trochę filozoficzne)
Aztech
post
Post #1





Grupa: Zarejestrowani
Postów: 276
Pomógł: 3
Dołączył: 22.10.2003
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


Zagadnienie, które mnie nurtuje to jak zorganiwazować połączenia z bazą danych w bibliotekach (zbiorach funkcji) a mianowicie dokładnie
(*) klasy reprezentujące rekordy poszczególnych tabel zgodnie z filozofią Active Record posiadają klasę bazową baseClass, która w konstruktorze wywołuje połączenie z bazą danych (to jest dobre (IMG:http://forum.php.pl/style_emoticons/default/biggrin.gif) )
(*) teraz przystępuję do pisania zbioru funkcji, łączonych w biblioteki, będą to funkcje zwracające z bazy MySQL różne tablice z danymi np:
  1. <?php
  2.  
  3. function getNotActivePrivateGroups() {/* kod */}
  4. function getActivePrivateGroups() {/* kod */}
  5. function getPrivateGroupUsersList() { /* kod */}
  6.  
  7. ?>

itp

Pytanie brzmi: o ile dla klas połączenie było realizowne w kontruktorze, to jak lepiej zrobić to dla zbioruy takich funkcji bibliotecznych.

1) czy lepiej w ciele każdej z tej funkcji łączyć się i rozłączać z bazą danych
2) czy może lepsze jest zastosowanie parametru przekazującego obiekt PDO
  1. <?php
  2.  
  3.  function getNotActivePrivateGroups(dbPDOConn){/* kod */}
  4.  
  5. ?>

3) a może nieładnie skorzystać z globalnego obiektu odpowiedzialnego za połączenie?

Zastanawiam się jak się będzie to miało do wydajności (szybkości działania) aplikacji w momencie gdy będą wykonywane duże ilości takich zapytań w jednym skrypcie? Jak państwo rozwiązujecie takie problemy?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
matid
post
Post #2





Grupa: Zarejestrowani
Postów: 362
Pomógł: 0
Dołączył: 18.02.2004
Skąd: Knurów

Ostrzeżenie: (0%)
-----


Co do ActiveRecord, to ja chcę u siebie zaimplementować bardziej dynamiczne rozwiązanie, odwzorowujące w czasie rzeczywistym stan bazy danych, więc np. utworzenie obiektu Post, który będzie odwzorowywał tablicę posts w bazie danych wyglądałoby tak:
  1. <?php
  2. class Post extends ActiveRecord {}
  3. ?>

Podobnie wygląda to w Ruby on Rails.

A co do sprawdzonych źródeł wzorców, m.in. Registry: PoEAA (Patterns of Enterprise Application Architecture). W polskim wydaniu opis wzorca Registry znajduje się na s. 438.

A co do mojej implementacji tego wzorca - u mnie jest to po prostu kontener, nie zawiera on metod tworzenia obiektów, jeśli one nie istnieją, a jedynie udostępnia globalny intefejs dostępu do przechowywanych obiektów. Brakuje mi jeszcze w moim rozwiązaniu implementacji wzorca IdentityMap, lub czegoś podobnego, co zapewni unikalność obiektów - chwilowo są to tylko nazwy wpisów w rejestrze.

Wygląda to mniej więcej tak:
  1. <?php
  2.  
  3. final class Registry extends Container {
  4. private static $Instance;
  5. private static function getInstance(){
  6. if( self::$Instance instanceof Registry ){
  7. return self::$Instance;
  8. }
  9. else {
  10. return self::$Instance = new Registry;
  11. self::$Instance->DataType = new String( 'Entry' );
  12. }
  13. }
  14. public static function getEntry( String $Name ){
  15. $Registry = self::getInstance();
  16. if( self::hasEntry( $Name )->isFalse() ){
  17. throw new ContainerException( 'Element ' . $Name->toString() . ' does not
  18. exist in ' . get_class( $Registry ) );
  19. }
  20. return $Registry->Elements[$Name->toString()];
  21.  
  22. }
  23.  
  24. public static function hasEntry( String $Name ){
  25. $Registry = self::getInstance();
  26. return new Boolean( isset( $Registry->Elements[$Name->toString()] ) );
  27. }
  28.  
  29. public static function addEntry( String $Name, $Element ){
  30. $Registry = self::getInstance();
  31. return $Registry->Elements[$Name->toString()] = $Element;
  32. }
  33. }
  34.  
  35. ?>


A tak wygląda klasa kontenera, którą Registry rozszerza.
  1. <?php
  2.  
  3. abstract class Container{
  4. protected $Elements = array();
  5. protected $ClassName;
  6. protected $DataType;
  7.  
  8. function __construct(){
  9. $ClassName = new String( get_class( $this ) );
  10. $ClassName->replace( new String( '/Container$/s' ), new String() );
  11. $this->DataType = Inflector::singularize( $ClassName );
  12. }
  13.  
  14. protected function getDataType(){
  15. return $this->DataType;
  16. }
  17.  
  18. protected function hasElement( String $Name ){
  19. $DataType = $this->getDataType()->toString();
  20. return new Boolean( isset( $this->Elements[ $Name->toString() ] ) &&
  21. $this->Elements[ $Name->toString() ] instanceof
  22. $DataType );
  23. }
  24.  
  25. protected function getElement( String $Name ){
  26. if( $this->hasElement($Name)->isFalse() ){
  27. throw new Exception( 'Element ' . $Name->toString() . ' does not
  28. exist in ' . get_class( $this ) );
  29. }
  30. return $this->Elements[$Name->toString()];
  31. }
  32.  
  33. protected function addElement( String $Name, $Element ){
  34. $DataType = $this->getDataType()->toString();
  35. if( !$Element instanceof $DataType ){
  36. throw new Exception( 'Argument 1 passed to ' . get_class( $this
  37. ) . '::add' . $this->getDataType()->toString() . '() must be an object of class ' .
  38. $DataType );
  39. }
  40. return $this->Elements[$Name->toString()] = $Element;
  41. }
  42.  
  43. protected function removeElement( String $Name ){
  44. if( $this->hasElement( $Name )->isTrue() ){
  45. unset( $this->Elements[ $Name->toString() ] );
  46. }
  47. }
  48.  
  49. public function getAll(){
  50. return $this->Elements;
  51. }
  52.  
  53. public function removeAll(){
  54. $this->Elements = array();
  55. }
  56.  
  57. function __call( $MethodName, $MethodParams ){
  58. $MethodName = new String( $MethodName );
  59.  
  60. if( $MethodName->startsWith( new String('get') ) ||
  61. $MethodName->startsWith( new String('has') ) ||
  62. $MethodName->startsWith( new String('add') ) ||
  63. $MethodName->startsWith( new String('remove') ) ){
  64. $DataType = clone $MethodName;
  65. $DataType->replace( new String( '/^(get|has|add|remove)/s' ), new
  66. String() );
  67. $MethodName->replace( new String( '/' . $DataType->toString() .
  68. '$/' ), new String() );
  69. $MethodName->concat( new String( 'Element' ) );
  70. if( $DataType->equals( $this->getDataType() ) === true ){
  71. return call_user_func_array( array( &$this,
  72. $MethodName->toString() ), $MethodParams );
  73. }
  74. else {
  75. throw new Exception( 'Container type invalid' );
  76. }
  77. }
  78. }
  79. }
  80.  
  81. ?>
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 5.10.2025 - 06:31