Architecting Your Models

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

1 comments

Comments 1 - 1 of 1 previous next Post a comment

  • + benjamindulau benjamindulau 1 month ago
    Hi,

    Interesting. I’ve spent time recently to implement different layers within Zend MVC.
    I have the following architecture: Controller > Facade (Service manager) > Service Layer > DAO > ORM with dependencies injection (and some experimental AOP :p).

    But i felt so alone around the forums with that stuff. The PHP developers were not ready yet to go so far.

    I’m happy that you’re starting to talk about the subject and i hope that kind of architecture will be democratized and proposals will emerge soon !

    In the mean time, this tutorial should generate some interesting discussions but i don’t really know where the discussion could take place (nabble, your blog ?).


    Cya,

    Benjamin.

    (I’m sorry if my english is sometime not so good !)
Post a comment
Embed Video
Edit your comment Cancel

16 Favorites

Architecting Your Models - Presentation Transcript

  1. Architecting Your Models Matthew Weier O'Phinney Project Lead Zend Framework
  2. Goals
    • Learn to recognize why old habits may be bad
    • Learn several design patterns that can help you write testable, maintainable code
    • Learn some techniques for altering existing behavior without rewriting existing code
  3. What We've Learned and What We Do
  4. Oooh! Let's create the schema!
  5. Write code that uses the DB
    • Plain Old Mysql (POM)
    • ActiveRecord
    • Table/Row Data Gateway
  6. And then …
    • We start lamenting about performance.
    • We end up refactoring for every new feature (e.g. caching, logging).
    • We need to go to a Service Oriented Architecture, and effectively refactor twice.
  7. STOP THE MADNESS!
  8. Step One
  9. Models are just classes. Create classes.
  10. Models have metadata and behavior ; create properties and methods .
  11. class Person implements PersonInterface { // Metadata protected $_email ; protected $_password ; protected $_username ; // Cheat: use overloading to // provide setters/getters public function __get( $name ) { } public function __set( $name , $value ) { } // Behaviors public function authenticate() { } public function logout() { } public function ban() { } }
  12. Step Two
  13. Now start thinking about data persistence .
    • Identify what data you need to persist
    • Identify how you'll persist the data
    • Write code for persisting data (Data Access Layer)
  14. CREATE TABLE person( username VARCHAR PRIMARY KEY , password VARCHAR , email VARCHAR , ); class PersonTable extends Zend_Db_Table_Abstract { protected $_name = 'person' ; protected $_primary = 'username' ; }
  15. Step Three
  16. Map your model to your data.
  17. Common approaches
    • Transaction Script (can even use POM)
    • Table Module (often with ActiveRecord or Table Data Gateway)
    • Data Mapper / ORM
  18. class PersonMapper implements PersonMapperInterface { public function save(PersonInterface $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 ); }
  19. Some notes:
    • Data !== Relational Database .
    • Data could come from a document database, filesystem, cache, or web service.
    • Choose an ORM that allows you to generate your schema from your entities; allows you to easily model first, and persistence comes for free.
  20. Step Four
  21. Move business and application logic to a Service Layer .
  22. Applications are like onions; they have layers. Photo © 2008, Mike Chaput-Branson
  23. The Service Layer provides application logic on top of your models
  24. Service Layer in perspective Data Access Objects and Data store(s) Data Mappers Domain Models Service Layer
  25. Benefits to a Service Layer
    • 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
  26. What kind of application logic?
    • Validation and filtering
    • Authentication and Authorization
    • Transactions and interactions between model entities
  27. 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 ; } }
  28. Decorating for fun and profit
  29. Decorators allow you to add or alter functionality of an existing class.
  30. Typical decorators …
    • Implement the same interface(s) of the class being decorated
    • Often use overloading to proxy to the decorated class
    • Override specific behavior(s) you wish to modify or enhance
    • Add new behaviors that use existing behaviors in the decorated class
  31. Refactor to add caching? No! Decorate!
  32. class CachingPersonMapper implements PersonMapperInterface { public function __construct( PersonMapperInterface $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 ; } }
  33. Refactor to provide alternate return formats? (e.g., JSON, XML, etc.) No! Decorate!
  34. class JsonPerson implements PersonInterface { public function __construct( PersonInterface $person ) { $this ->_person = $person ; } public function __toString() { $data = array ( 'username' => $this ->_person->username, 'email' => $this ->_person->email, ); return json_encode( $data ); } }
  35. Nicely Formed Objects
    • Rebuilding and refactoring is costly and painful
    • Good OOP and encapsulation CAN make your life easier
    • Testing is easier than debugging
    • Choose a good ORM to expedite development. (Doctrine, Object Freezer, Zend_Entity, etc.)
  36. Think beyond the DB!
  37. Thank you!
    • Feedback: http://joind.in/918
    • Twitter: @weierophinney
    • Blog: http://weierophinney.net/matthew/

+ Matthew Weier O'PhinneyMatthew Weier O'Phinney, 1 month ago

custom

2016 views, 16 favs, 1 embeds more stats

Provides some approaches to the "M" in MVC -- Model more

More info about this document

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Go to text version

  • Total Views 2016
    • 2015 on SlideShare
    • 1 from embeds
  • Comments 1
  • Favorites 16
  • Downloads 149
Most viewed embeds
  • 1 views on http://bleroutoolkit.wordpress.com

more

All embeds
  • 1 views on http://bleroutoolkit.wordpress.com

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories