Your SlideShare is downloading. ×
0
Play-Doh:  Modelling your Objects Matthew Weier O'Phinney Project Lead Zend Framework
What we've learned and what we do
Oooh!  Let's create the schema!
Write code that uses the DB
<ul><li>Object Relational Mapping (ORM)
ActiveRecord
Table/Row Data Gateway
Plain Old Mysql (POM) </li></ul>
And then …
<ul><li>we start lamenting about performance
we end up refactoring for every new feature  (esp. caching, logging)
we need to go to a Service Oriented Architecture,  and refactor twice </li></ul>
STOP THE MADNESS!
Step One
Models are just classes. Create classes.
< ?php class   Person { protected   $_email ; protected   $_password ; protected   $_username ; public   function   __get ...
Step Two
<ul><li>Identify what data you need to persist
Identify how you'll persist the data
Write code for persisting data  (Data Access Layer) </li></ul>
CREATE TABLE   person ( username   VARCHAR   PRIMARY KEY , password   VARCHAR , email   VARCHAR , ); < ?php class   Person...
Step Three
<ul><li>Map your model to your data: </li><ul><li>Data Mapper
Transaction Script </li></ul></ul>
< ?php class   PersonMapper { public   function   save ( Person   $person ) { $data   =   array ( 'username'   =>   $perso...
Step Four
<ul><li>Move application logic to a Service Layer </li><ul><li>Allows easy consumption of the application via your MVC layer
Allows easy re-use of your application via services
Write CLI scripts that consume the Service Layer </li></ul></ul>
<ul><li>What kind of application logic? </li><ul><li>Validation and filtering
Upcoming SlideShare
Loading in...5
×

Play-Doh: Modelling Your Objects

7,460

Published on

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

Published in: Technology
3 Comments
9 Likes
Statistics
Notes
  • Thanks for your quick answer.
    I got your point about the save() method.
    I think i was wrong while thinking about this, and as you say, the object which saves, also does the instrospection to know what to do, but i thought differently while asking the question.
    I read some of your article on your blog covering a similar approach and it was quite interesting.
    The introducing of a Mapper & a Service which could be decorated is a quite interesting point.
    I would be happy if you have some documents/links about relational mapping.
    Thanks again for your works
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Relational mapping is very difficult, and was way beyond the scope of this presentation. The primary purpose of the presentation was to get PHP developers to start thinking beyond the RDBMS and instead focus on the objects they're manipulating. I mention ORMs briefly within this presentation as they can be used to solve some of the problems of persisting your models (though since ORMs are typically RDBMS-specific, they do not answer the question of persisting through document databases or web services) -- but there was no time to go into detail on that subject.

    As for save() vs. insert()/update(), save() is used in many ORMs, ActiveRecord, and Table Data Gateway implementations because it simplifies the interface. You do not need to know how the object was instantiated -- i.e., was it loaded from the persistence store, or is it a newly created object -- you simply save() it. save() then does the introspection of the object for you and decides if it is an update() or insert() operation -- and when done, updates the object from the persistence store.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Insteresting slide, but it covers only simple cases.
    What would have been insteresting is to cover classic problems like relational tables mapping.
    For example, how do you handle a Post domain object and a User domain object.
    Do you write a new PostUser object ? do you include userId, or userObject within the Post object ?

    Another point is, if i save i need to know if i update, or i insert, which is often simple to know for me because, if the userId is present in the object, i know the object has been retrieved from the DB, and then need to be updated, not inserted.
    Then, it is insteresting to return the new User object with its userId, but it could break the pattern.
    What do you think about this ?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
7,460
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
108
Comments
3
Likes
9
Embeds 0
No embeds

No notes for slide

Transcript of "Play-Doh: Modelling Your Objects"

  1. 1. Play-Doh: Modelling your Objects Matthew Weier O'Phinney Project Lead Zend Framework
  2. 2. What we've learned and what we do
  3. 3. Oooh! Let's create the schema!
  4. 4. Write code that uses the DB
  5. 5. <ul><li>Object Relational Mapping (ORM)
  6. 6. ActiveRecord
  7. 7. Table/Row Data Gateway
  8. 8. Plain Old Mysql (POM) </li></ul>
  9. 9. And then …
  10. 10. <ul><li>we start lamenting about performance
  11. 11. we end up refactoring for every new feature (esp. caching, logging)
  12. 12. we need to go to a Service Oriented Architecture, and refactor twice </li></ul>
  13. 13. STOP THE MADNESS!
  14. 14. Step One
  15. 15. Models are just classes. Create classes.
  16. 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. 17. Step Two
  18. 18. <ul><li>Identify what data you need to persist
  19. 19. Identify how you'll persist the data
  20. 20. Write code for persisting data (Data Access Layer) </li></ul>
  21. 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. 22. Step Three
  23. 23. <ul><li>Map your model to your data: </li><ul><li>Data Mapper
  24. 24. Transaction Script </li></ul></ul>
  25. 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. 26. Step Four
  27. 27. <ul><li>Move application logic to a Service Layer </li><ul><li>Allows easy consumption of the application via your MVC layer
  28. 28. Allows easy re-use of your application via services
  29. 29. Write CLI scripts that consume the Service Layer </li></ul></ul>
  30. 30. <ul><li>What kind of application logic? </li><ul><li>Validation and filtering
  31. 31. Authentication and Authorization
  32. 32. Transactions and interactions between models </li></ul></ul>
  33. 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. 34. Decorating for Fun and Profit
  35. 35. Refactor to add caching? No! Decorate!
  36. 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. 37. Refactor or extend to change the return value (e.g., JSON vs XML vs ...)? No! Decorate!
  38. 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. 39. Congratulations!
  40. 40. <ul><li>Rebuilding and refactoring is costly and painful
  41. 41. Good OOP and encapsulation CAN make your life easier
  42. 42. Testing is easier than debugging </li></ul>
  43. 43. Think beyond the DB!
  44. 44. Thank you.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×