Play-Doh: Modelling Your Objects

Embed Size (px)

DESCRIPTION

Short un-conference tutorial showing how to create your models in a way that is data layer independent, testable, and extensible.

Citation preview

  • 1. Play-Doh:Modelling your Objects Matthew Weier O'Phinney Project Lead Zend Framework

2. What we've learned and what we do 3. Oooh!Let's create the schema! 4. Write code that uses the DB 5.

  • Object Relational Mapping (ORM)

6. ActiveRecord 7. Table/Row Data Gateway 8. Plain Old Mysql (POM) 9. And then 10.

  • we start lamenting about performance

11. we end up refactoring for every new feature(esp. caching, logging) 12. we need to go to a Service Oriented Architecture,and refactor twice 13. STOP THE MADNESS! 14. Step One 15. Models are just classes. Create classes. 16. < ?php class Person { protected $_email ; protected $_password ; protected $_username ; public function __get ( $name ) { $local = '_'. $name ; if ( property_exists ( $this , $local )) { return $this -> $local ; } return null ; } public function __set ( $name , $value ) { $local = '_'. $name ; if (! property_exists ( $this , $local )) { throw new InvalidArgumentException (); } $this -> $local = $value ; } } 17. Step Two 18.

  • Identify what data you need to persist

19. Identify how you'll persist the data 20. Write code for persisting data(Data Access Layer) 21. CREATE TABLE person ( username VARCHAR PRIMARY KEY , password VARCHAR , email VARCHAR , ); < ?php class PersonTable extends Zend_Db_Table_Abstract { protected $_name = 'person' ; protected $_primary = 'username' ; } 22. Step Three 23.

  • Map your model to your data:
  • Data Mapper

24. Transaction Script 25. < ?php class PersonMapper { public function save ( Person $person ) { $data = array ( 'username' => $person -> username , 'password' => $person -> password , 'email' => $person -> email , ); $this -> getTable ()-> save ( $data ); } public function fetch ( $username ); public function getTable (); public function setTable ( $table ); } 26. Step Four 27.

  • Move application logic to a Service Layer
  • Allows easy consumption of the application via your MVC layer

28. Allows easy re-use of your application via services 29. Write CLI scripts that consume the Service Layer 30.

  • What kind of application logic?
  • Validation and filtering

31. Authentication and Authorization 32. Transactions and interactions between models 33. < ?php class PersonService { public function create ( array $data ) { $person = new Person (); if (! $data = $this -> getValidator ()-> isValid ( $data )) { throw new InvalidArgumentException (); } $person -> username = $data [ 'username' ]; $person -> password = $data [ 'password' ]; $person -> email = $data [ 'email' ]; $this -> getMapper ()-> save ( $person ); return $person ; } } 34. Decorating for Fun and Profit 35. Refactor to add caching? No! Decorate! 36. < ?php class CachingPersonMapper { protected $_mapper ; public function __construct ( PersonMapper $mapper ) { $this -> _mapper = $mapper ; } public function fetch ( $username ) { $cache = $this -> getCache (); if (! $person = $cache -> load ( $username )) { $person = $this -> _mapper -> fetch ( $username ); $cache -> save ( $person , $username ); } return $person ; } } 37. Refactor or extendto change the return value (e.g., JSON vs XML vs ...)? No! Decorate! 38. < ?php class JsonPerson { protected $_person ; public function __construct ( Person $person ) { $this -> _person = $person ; } public function __toString () { $data = array ( 'username' => $this -> _person -> username , 'email' => $this -> _person -> email , ); return json_encode ( $data ); } } 39. Congratulations! 40.

  • Rebuilding and refactoring is costly and painful

41. Good OOP and encapsulation CAN make your life easier 42. Testing is easier than debugging 43. Think beyond the DB! 44. Thank you.