zend framework 03 - singleton factory data mapper caching logging

36
Zend Framework 3. Singleton & Factory, Dependency Injection, Data Mapper, Caching, Logging Tricode Professional Services www.tricode.nl Date: 27-02-2009 Authors: Marcel Blok Patrick van Dissel

Upload: tricode

Post on 15-Jan-2015

3.903 views

Category:

Technology


4 download

DESCRIPTION

Singleton & Factory, Dependency Injection, Data Mapper, Caching and Logging.

TRANSCRIPT

Page 1: Zend framework 03 - singleton factory data mapper caching logging

Zend Framework

3. Singleton & Factory,Dependency Injection, Data Mapper,

Caching, Logging

Tricode Professional Serviceswww.tricode.nl

Date: 27-02-2009Authors: Marcel Blok

Patrick van Dissel

Page 2: Zend framework 03 - singleton factory data mapper caching logging

2

Singleton

“Ensure a class only has one instance, and provide a global point of access to it.”

– GoF

Page 3: Zend framework 03 - singleton factory data mapper caching logging

3

Singletonclass Logger{ /** * @var Logger */ private static $instance = null; private function __construct () {} private function __clone() {}

/** * @return Logger */ public static function getInstance() { if (!self::$instance instanceof self) { self::$instance = new self(); } return self::$instance; }}

<?php

$a = Logger::getInstance();$a->methodX(..);

$b = Logger::getInstance();$a->methodX(..);

$a === $b

Page 4: Zend framework 03 - singleton factory data mapper caching logging

4

Singleton

• Pros:– There's always only one instance of the class– Easy to use

• Cons:– Hides dependencies– Hard to test– Hard to subclass– A singleton today is a multiple tomorrow– It's a global variable! Globals are bad! So use with caution

Page 5: Zend framework 03 - singleton factory data mapper caching logging

5

Dependency Injection

“Dependency Injection refers to the process of supplying an external dependency to a software component.”

Page 6: Zend framework 03 - singleton factory data mapper caching logging

6

Dependency InjectionHow can Dependency Injection be applied to the following code?

<?phpclass Book { public function __construct() { $this->_databaseConnection = new DatabaseConnection();

// or

global $databaseConnection; $this->_databaseConnection = $databaseConnection; }

}

Page 7: Zend framework 03 - singleton factory data mapper caching logging

7

Dependency Injection

• What does this mean for your code?– Enforce that an external dependency is

provided to a class. Not instanciated and configured within the class itself

– More reusable code– More testable code

Page 8: Zend framework 03 - singleton factory data mapper caching logging

8

Dependency InjectionHow is this?

<?phpclass Book {

public function __construct() { }

public function setDatabaseConnection($databaseConnection) { $this->_databaseConnection = $databaseConnection; }}

$book = new Book();$book->setDatabase($databaseConnection);

Page 9: Zend framework 03 - singleton factory data mapper caching logging

9

Dependency InjectionHow is this?

<?phpclass Book {

public function __construct($databaseConnection) { … }

protected function setDatabaseConnection($databaseConnection) { $this->_databaseConnection = $databaseConnection; }}

$book = new Book($databaseConnection);

Page 10: Zend framework 03 - singleton factory data mapper caching logging

10

Dependency InjectionHow is this?

<?phpclass Factory {

private static $_database;

public static function makeBook() {

$book = new Book();$book->setDatabase(self::$_database);// more injection...

return $book;}public static function setup($database) { …. }

}

$book = Factory::makeBook();

Page 11: Zend framework 03 - singleton factory data mapper caching logging

11

Dependency Injection

• Dependency Injection makes sure classes have:– High coherence – Low coupling

• What results in:– Reduced Dependencies– Reduced Dependency Carrying– More Reusable Code– More Testable Code– More Readable Code

Page 12: Zend framework 03 - singleton factory data mapper caching logging

12

Dependency Injection

• Where should objects be instantiating and configured?– On the highest layer, eg. in the controller or in a factory

• Doesn't that make the controller huge?– Yes, it makes the controller bigger. But also more in

control and without surprises

Page 13: Zend framework 03 - singleton factory data mapper caching logging

13

Factory

“Provide an interface for creating families of related or dependent objects without specifying their concrete classes.”

– GoF

Page 14: Zend framework 03 - singleton factory data mapper caching logging

14

Factory

• Benefits:– A good alternative for the singleton pattern– Takes care of initiating objects– Can take care of configuring objects

Page 15: Zend framework 03 - singleton factory data mapper caching logging

15

Factory

<?phpclass Factory {

private static $_database;

public static function makeBook() {

$book = new Book();$book->setDatabase(self::$_database);// more injection...

return $book;}public static function setup($database) { …. }

}

$book = Factory::makeBook();

Page 16: Zend framework 03 - singleton factory data mapper caching logging

16

Persistency

Very good ways to persist objects are nowadays:

• Object-relational DBMS (ORDBMS)• Object-orientated DBMS (OODBMS)• Space based architectures (SBA)

Page 17: Zend framework 03 - singleton factory data mapper caching logging

17

Persistency

Unfortunately often we have legacy data that is stored and structured in:

• RDMS• XML• Text files• Etc.

Page 18: Zend framework 03 - singleton factory data mapper caching logging

18

Object-relational impedance mismatch

The biggest challenge in persisting objects to relational database management systems is the mismatch between objects and tables.

Page 19: Zend framework 03 - singleton factory data mapper caching logging

19

Object-relational impedance mismatch

Database model

Object model

Page 20: Zend framework 03 - singleton factory data mapper caching logging

20

Data mapper Pattern

In order to stick to good OO design rules the code for the object itself and for the mapping should

be separated (high cohesion).

The Mapper should know of the object, the object not of the Mapper (low coupling)

Page 21: Zend framework 03 - singleton factory data mapper caching logging

21

Data mapper Pattern

Page 22: Zend framework 03 - singleton factory data mapper caching logging

22

Database connection

• It is good practice to separate the code for creating the database connection from the Data Mapper (high cohesion)

Page 23: Zend framework 03 - singleton factory data mapper caching logging

23

Database connection

Data Mapper

ObjectDatabase

Connection Database

Like a singleton

Page 24: Zend framework 03 - singleton factory data mapper caching logging

24

Database connection

With a factory

Database ConnectionDatabase

ConnectionData

MapperObject

Database Connection Database

ConnectionFactory

Page 25: Zend framework 03 - singleton factory data mapper caching logging

25

Lazy loading

Load the data as late as possible

• Lazy initializationSet initially to null and every request checks this

• Virtual proxyA proxy object with the same interface that loads the requested object

• GhostInitialize with only ID, load the remainder when needed

• Value holderUse a generic object that handles the lazy loading

Page 26: Zend framework 03 - singleton factory data mapper caching logging

26

Aggressive loading

Load all data as soon as the object is instantiated.

In some cases aggressive loading is moreappropriate:

• All (or most) data is always needed• Resource costs are low (time, memory)

Page 27: Zend framework 03 - singleton factory data mapper caching logging

27

Zend_Cache

•Algorithm:

No

$data = false;

$data = $cache->get(‘myKey’);

if (false === $data) {

$data = $db->fetchOne(...);

$cache->save($data, ‘myKey’);

}

echo $data;

Page 28: Zend framework 03 - singleton factory data mapper caching logging

28

Zend_Cache

•• Frontend: How to cache• Backend: Where to cache it

• Used by several other components in the framework

• Use caching where possible to limit the number of requests to slow resources, eg. database queries

Page 29: Zend framework 03 - singleton factory data mapper caching logging

29

Zend_Cache

Theory:

• Lifetime: every entry in the cache expires at some point

• Commonly used methods:– get(): returns false when nothing found (a miss)– save()– remove()

Page 30: Zend framework 03 - singleton factory data mapper caching logging

30

Zend_Cache: Frontend

Different methods of caching:

• Zend_Cache_Core (abstract)• Zend_Cache_Frontend_Output• Zend_Cache_Frontend_Function• Zend_Cache_Frontend_Class• Zend_Cache_Frontend_File• Zend_Cache_Frontend_Page

Page 31: Zend framework 03 - singleton factory data mapper caching logging

31

Zend_Cache: Backend

Generic interface for different cache backends

– File (on disk)– Sqlite (database)– Memcached (in memory, distributed)– APC (opcode cache)– Xcache (opcode cache)– ZendPlatform (disk)– TwoLevels (combination of two above methods)

Page 32: Zend framework 03 - singleton factory data mapper caching logging

32

Zend_Log

$logger = new Zend_Log();

$logger->log(‘log this message’, Zend_Log::INFO);

$logger->info(‘log this message’);

Page 33: Zend framework 03 - singleton factory data mapper caching logging

33

Zend_Log

Name Value Usage

Zend_Log::EMERGE

0 Emergency: system is unusable

Zend_Log::ALERT 1 Alert: action must be taken immediately

Zend_Log::CRIT 2 Critical: critical conditions

Zend_Log::ERR 3 Error: error conditions

Zend_Log::WARN 4 Warning: warning conditions

Zend_Log::NOTICE 5 Notice: normal but significant conditions

Zend_Log::INFO 6 Informational messages

Zend_Log::DEBUG 7 Debug messages

Log priorities

Page 34: Zend framework 03 - singleton factory data mapper caching logging

34

Zend_Log

Name When to use

Zend_Log_writer_Stream

Stores logs to files or other streams. The ‘php://output’ stream can be used to display to the output buffer

Zend_Log_writer_Db Stores logs to database records. You need to map the level and messages to two fields within a table

Zend_Log_writer_Firebug

Sends log messages to the console in the Firebug extension of Firefox

Zend_Log_writer_Null Discards all log messages. This can be useful for turning off logging during testing or for disabling logging

Log writers

Page 35: Zend framework 03 - singleton factory data mapper caching logging

35

Zend_Log

$logger = new Zend_Log();

// Log Warnings, Notices and Informational messages$writerMessages = new Zend_Log_Writer_Stream($logsPath . 'messages.log');

$messageFilter1 = new Zend_Log_Filter_Priority(Zend_Log::WARN, '>=');$messageFilter2 = new Zend_Log_Filter_Priority(Zend_Log::INFO, '<=');$writerMessages->addFilter($messageFilter1);$writerMessages->addFilter($messageFilter2);

$logger->addWriter($writerMessages);

Page 36: Zend framework 03 - singleton factory data mapper caching logging

36

References

• Singleton and Dependency Injection– http://googletesting.blogspot.com

/2008/11/clean-code-talks-global-state-and.html

– http://en.wikipedia.org/wiki/Singleton_pattern– http://www.potstuck.com/2009/01/08/php-dependency-i

njection/

• Anti-Patterns– http://en.wikipedia.org/wiki/Anti_pattern