Playdoh Modelling Your Objects

Embed Size (px)

Citation preview

  • 7/31/2019 Playdoh Modelling Your Objects

    1/42

    All rights reserved. Zend Technologies, Inc.

    Architecting Your ModelsMatthew Weier O'PhinneyProject LeadZend Framework

  • 7/31/2019 Playdoh Modelling Your Objects

    2/42

    2 All rights reserved. Zend Technologies, Inc.

    Goals

  • 7/31/2019 Playdoh Modelling Your Objects

    3/42

    All rights reserved. Zend Technologies, Inc.

    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 existingbehavior without rewriting existing code

  • 7/31/2019 Playdoh Modelling Your Objects

    4/424 All rights reserved. Zend Technologies, Inc.

    What We've Learned and What We Do

  • 7/31/2019 Playdoh Modelling Your Objects

    5/425 All rights reserved. Zend Technologies, Inc.

    Oooh! Let's create the schema!

  • 7/31/2019 Playdoh Modelling Your Objects

    6/426 All rights reserved. Zend Technologies, Inc.

    Write code that uses the DB

  • 7/31/2019 Playdoh Modelling Your Objects

    7/42 All rights reserved. Zend Technologies, Inc.

    Plain Old Mysql (POM) ActiveRecord

    Table/Row Data Gateway

  • 7/31/2019 Playdoh Modelling Your Objects

    8/428 All rights reserved. Zend Technologies, Inc.

    And then

  • 7/31/2019 Playdoh Modelling Your Objects

    9/42

    All rights reserved. Zend Technologies, Inc.

    We start lamenting about performance. We end up refactoring for every new feature

    (e.g. caching, logging).

    We need to go to a Service OrientedArchitecture, and effectively refactor twice.

  • 7/31/2019 Playdoh Modelling Your Objects

    10/42

    10 All rights reserved. Zend Technologies, Inc.

    STOPTHE

    MADNESS!

  • 7/31/2019 Playdoh Modelling Your Objects

    11/42

    11 All rights reserved. Zend Technologies, Inc.

    Step One

  • 7/31/2019 Playdoh Modelling Your Objects

    12/42

    12 All rights reserved. Zend Technologies, Inc.

    Models are just classes.

    Create classes.

  • 7/31/2019 Playdoh Modelling Your Objects

    13/42

    13 All rights reserved. Zend Technologies, Inc.

    Models have metadata and behavior;

    createproperties and methods.

  • 7/31/2019 Playdoh Modelling Your Objects

    14/42

    14 All rights reserved. Zend Technologies, Inc.

    class Person implements PersonInterface{

    // Metadataprotected $_email;protected $_password;protected $_username;

    // Cheat: use overloading to

    // provide setters/getterspublic function __get($name) { }public function __set($name, $value) { }

    // Behaviors

    public function authenticate() { }public function logout() { }public function ban() { }

    }

    classPerson implements PersonInterface{ // Metadata

    protected$_email;protected$_password;protected$_username;

    // Cheat: use overloading to

    // provide setters/getterspublicfunction__get($name) { }publicfunction__set($name,$value) { }

    // Behaviors

    publicfunctionauthenticate() { }publicfunctionlogout() { }publicfunctionban() { }

    }

  • 7/31/2019 Playdoh Modelling Your Objects

    15/42

    15 All rights reserved. Zend Technologies, Inc.

    Step Two

  • 7/31/2019 Playdoh Modelling Your Objects

    16/42

    16 All rights reserved. Zend Technologies, Inc.

    Nowstart thinking

    about datapersistence.

  • 7/31/2019 Playdoh Modelling Your Objects

    17/42

    All rights reserved. Zend Technologies, Inc.

    Identify what data you need to persist Identify how you'll persist the data

    Write code for persisting data

    (Data Access Layer)

  • 7/31/2019 Playdoh Modelling Your Objects

    18/42

    18 All rights reserved. Zend Technologies, Inc.

    CREATE TABLE person(usernameVARCHARPRIMARY KEY,passwordVARCHAR,emailVARCHAR,

    );

    CREATE TABLEperson(usernameVARCHARPRIMARY KEY,passwordVARCHAR,emailVARCHAR,

    );

    class PersonTableextends Zend_Db_Table_Abstract

    {

    protected $_name = 'person';protected $_primary = 'username';}

    classPersonTableextendsZend_Db_Table_Abstract

    {protected$_name='person';protected$_primary='username';

    }

  • 7/31/2019 Playdoh Modelling Your Objects

    19/42

    19 All rights reserved. Zend Technologies, Inc.

    Step Three

  • 7/31/2019 Playdoh Modelling Your Objects

    20/42

    20 All rights reserved. Zend Technologies, Inc.

    Map your model

    to your data.

  • 7/31/2019 Playdoh Modelling Your Objects

    21/42

    All rights reserved. Zend Technologies, Inc.

    Common approaches

    Transaction Script (can even use POM) Table Module (often with ActiveRecord or Table

    Data Gateway)

    Data Mapper / ORM

  • 7/31/2019 Playdoh Modelling Your Objects

    22/42

    22 All rights reserved. Zend Technologies, Inc.

    class PersonMapperimplements 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);

    }

    classPersonMapperimplements PersonMapperInterface

    {

    publicfunctionsave(PersonInterface$person) { $data=array( 'username'=>$person->username, 'password'=>$person->password,

    'email' =>$person->email, ); $this->getTable()->save($data); }

    publicfunctionfetch($username);publicfunctiongetTable();publicfunctionsetTable($table);

    }

  • 7/31/2019 Playdoh Modelling Your Objects

    23/42

    All rights reserved. Zend Technologies, Inc.

    Some notes:

    Data !== Relational Database. Data could come from a document database,

    filesystem, cache, or web service.

    Choose an ORM that allows you to generateyour schema from your entities; allows you toeasily model first, and persistence comes forfree.

  • 7/31/2019 Playdoh Modelling Your Objects

    24/42

    24 All rights reserved. Zend Technologies, Inc.

    Step Four

  • 7/31/2019 Playdoh Modelling Your Objects

    25/42

    25 All rights reserved. Zend Technologies, Inc.

    Move business andapplication logic to

    a Service Layer.

  • 7/31/2019 Playdoh Modelling Your Objects

    26/42

    26 All rights reserved. Zend Technologies, Inc.

    Applicationsare like onions;

    they have layers.

    Photo 2008, MikeChaput-Branson

  • 7/31/2019 Playdoh Modelling Your Objects

    27/42

    27 All rights reserved. Zend Technologies, Inc.

    The Service Layerprovides application logicon top of your models

  • 7/31/2019 Playdoh Modelling Your Objects

    28/42

    28 All rights reserved. Zend Technologies, Inc.

    Service Layerin perspective

    Data Access Objectsand Data store(s)

    Data Mappers

    Domain Models

    ServiceLayer

  • 7/31/2019 Playdoh Modelling Your Objects

    29/42

    All rights reserved. Zend Technologies, Inc.

    Benefits to a Service Layer

    Allows easy consumption of the applicationvia your MVC layer

    Allows easy re-use of your applicationvia services

    Write CLI scripts that consume theService Layer

  • 7/31/2019 Playdoh Modelling Your Objects

    30/42

    All rights reserved. Zend Technologies, Inc.

    What kind of application logic?

    Validation and filtering Authentication and Authorization

    Transactions and interactions between model

    entities

  • 7/31/2019 Playdoh Modelling Your Objects

    31/42

    31 All rights reserved. Zend Technologies, Inc.

    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;

    }}

    classPersonService{

    publicfunctioncreate(array$data)

    { $person=newPerson(); if(!$data=$this->getValidator()

    ->isValid($data)) {

    throw newInvalidArgumentException(); } $person->username=$data['username']; $person->password=$data['password']; $person->email =$data['email'];

    $this->getMapper()->save($person); return$person; }}

  • 7/31/2019 Playdoh Modelling Your Objects

    32/42

    32 All rights reserved. Zend Technologies, Inc.

    Decorating forfun and profit

  • 7/31/2019 Playdoh Modelling Your Objects

    33/42

    33 All rights reserved. Zend Technologies, Inc.

    Decorators allow youto add or alter functionalityof an existing class.

  • 7/31/2019 Playdoh Modelling Your Objects

    34/42

    All rights reserved. Zend Technologies, Inc.

    Typical decorators

    Implement the same interface(s) of the classbeing decorated

    Often use overloading to proxy to thedecorated class

    Override specific behavior(s) you wish tomodify or enhance

    Add new behaviors that use existing behaviorsin the decorated class

  • 7/31/2019 Playdoh Modelling Your Objects

    35/42

    35 All rights reserved. Zend Technologies, Inc.

    Refactor to add caching?No!Decorate!

    class CachingPersonMapper

    classCachingPersonMapper

  • 7/31/2019 Playdoh Modelling Your Objects

    36/42

    36 All rights reserved. Zend Technologies, Inc.

    class CachingPersonMapperimplements 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;

    }}

    g ppimplements PersonMapperInterface

    {publicfunction__construct(

    PersonMapperInterface$mapper){ $this->_mapper=$mapper; }

    publicfunctionfetch($username){

    $cache=$this->getCache(); if(!$person=$cache->load($username)) { $person=$this->_mapper

    ->fetch($username); $cache->save($person,$username);

    } return$person; }}

  • 7/31/2019 Playdoh Modelling Your Objects

    37/42

    37 All rights reserved. Zend Technologies, Inc.

    Refactor to provide alternatereturn formats? (e.g., JSON, XML, etc.)No!Decorate!

    l i l f

  • 7/31/2019 Playdoh Modelling Your Objects

    38/42

    38 All rights reserved. Zend Technologies, Inc.

    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);}

    }

    classJsonPerson implements PersonInterface{

    publicfunction__construct(

    PersonInterface$person ){ $this->_person=$person; }

    publicfunction__toString() { $data=array( 'username'=>$this->_person->username, 'email' =>$this->_person->email, ); returnjson_encode($data); }}

  • 7/31/2019 Playdoh Modelling Your Objects

    39/42

    39 All rights reserved. Zend Technologies, Inc.

    Nicely FormedObjects

  • 7/31/2019 Playdoh Modelling Your Objects

    40/42

    All rights reserved. Zend Technologies, Inc.

    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.)

  • 7/31/2019 Playdoh Modelling Your Objects

    41/42

    41 All rights reserved. Zend Technologies, Inc.

    Think beyondthe DB!

  • 7/31/2019 Playdoh Modelling Your Objects

    42/42

    42 All rights reserved. Zend Technologies, Inc.

    Thank you! Feedback: http://joind.in/918

    Twitter: @weierophinney

    Blog: http://weierophinney.net/matthew/