際際滷

際際滷Share a Scribd company logo
Dependency Injection(DI),
              Phemto
                  仂仄舒仆亳亠于 仍舒亟亳仄亳

VETMANAGER TEAM        26.4.12   弌. 1
VETMANAGER TEAM   26.4.12   弌. 2
仍舒仆
  1)舒从 仗仍仂仂 弍亠亰 Dependency Injection
  2)舒从 仂仂仂  Dependency Injection
  3)舒从 仗仍仂仂  Dependency Injection
  4)舒从 仂仂仂  Phemto
  5)舒从 仂仂仂 :)




VETMANAGER TEAM          26.4.12   弌. 3
舒从 仗仍仂仂
 class Stores {
  function add($data){
     $query = 'INSERT INTO stores SET title=?
        , assigned_user_id=?, type=?';
     NDatabase::query($query, array($data['title']
        , $data['assigned_user_id'], $data['type_id']));
  }


Stores::add

VETMANAGER TEAM                 26.4.12   弌. 4
舒从 仗仍仂仂

    舒从 亠亳仂于舒?(仆亳从舒从)

    舒从 舒弍仂舒  仆亠从仂仍从亳仄亳 从仂仆仆亠从舒仄亳? (仆亳从舒从)

    舒从 仗仂亟仄亠仆亳 弌丕?(从仂亠-从舒从)

    舒从 亳仗仂仍亰仂于舒 于 亟亞仂仄 仗仂亠从亠?(从仂亠-从舒从)

    舒从 仗亠亠亶亳 仆舒 NoSQL?(仆亳从舒从)




VETMANAGER TEAM             26.4.12   弌. 5
亠仍舒亠仄 亳仆亠从亳
class Stores{
   public function __construct($database) {
   }
   function add($data){
     $query = 'INSERT INTO stores SET title=?
             , assigned_user_id=?, type=?';
     $this->_database
             ->query($query, $data);
   }

VETMANAGER TEAM          26.4.12   弌. 6
亠

    舒从 亠亳仂于舒?(仍亠亞从仂)

    舒从 舒弍仂舒  仆亠从仂仍从亳仄亳 从仂仆仆亠从舒仄亳? (仍亠亞从仂)

    舒从 仗仂亟仄亠仆亳 弌丕?(仍亠亞亠)

    舒从 亳仗仂仍亰仂于舒 于 亟亞仂仄 仗仂亠从亠?(仍亠亞亠)

    舒从 仗亠亠亶亳 仆舒 NoSQL?(仆亳从舒从)




VETMANAGER TEAM             26.4.12   弌. 7
亳亠仄 亠...
              class StoresTest extends
                        PHPUnit_Framework_TestCase
              {
                 function setUp(){
                   $this->dbMock = $this
                              ->getMock('VMDatabase');
                   $this->testObject =
                                   new Stores($this->dbMock
                 }


VETMANAGER TEAM                  26.4.12   弌. 8
亳亠仄 亠...
            function testAdd(){
              $this->dbMock->expects($this->once())
                    ->method('query')
                    ->with(
                       $this->stringContains("INSERT")
                       , $this->equalTo(array('title', 1, 2))
                    );
              $this->testObject->add($testData);
            }



VETMANAGER TEAM                   26.4.12   弌. 9
舒从 仗仍仂仂  DI
             $stores = new Stores(new VMDatabase());
                  $stores = new Stores(
                      new VMDatabase()
                     , new VMRequest()
                     , new VMSession()
                     , new UserModel()
                     
                  );


VETMANAGER TEAM                  26.4.12   弌. 10
舒从 仂仂仂  Phemto
public function __construct(VMDatabase $database) {
  $this->_database = $database;
}


require_once 'phemto/phemto.php';
$injector = new Phemto();
$stores = $injector->create('Stores');




VETMANAGER TEAM              26.4.12     弌. 11
舒从 仂仂仂  Phemto2
      public function __construct($database) {
        $this->_database = $database;
      }

   require_once 'phemto/phemto.php';
   $injector = new Phemto();
   $injector->forVariable('database')
         ->willUse(new Reused('VMDatabase'));

   $stores = $injector->create('Stores');


VETMANAGER TEAM               26.4.12   弌. 12
亠

    舒从 亠亳仂于舒?(仍亠亞从仂)

    舒从 舒弍仂舒  仆亠从仂仍从亳仄亳 从仂仆仆亠从舒仄亳? (仍亠亞从仂)

    舒从 仗仂亟仄亠仆亳 弌丕?(仍亠亞亠)

    舒从 亳仗仂仍亰仂于舒 于 亟亞仂仄 仗仂亠从亠?(仍亠亞亠)

    舒从 仗亠亠亶亳 仆舒 NoSQL?(仆亳从舒从)




VETMANAGER TEAM             26.4.12   弌. 13
ActiveRecord
  class Stores {
     function dataWrap($data){...}
     protected function getEntity(){...}
     function add($data){
       $entity = $this->getEntity();
       $entity->import($this->dataWrap($data));
       $entity->save();
     }



VETMANAGER TEAM              26.4.12   弌. 14
亠?(getEntity)

    舒从 亠亳仂于舒?(???)

    舒从 舒弍仂舒  仆亠从仂仍从亳仄亳 从仂仆仆亠从舒仄亳?(???)

    舒从 仗仂亟仄亠仆亳 弌丕?(仍亠亞从仂)

    舒从 亳仗仂仍亰仂于舒 于 亟亞仂仄 仗仂亠从亠?(???)

    舒从 仗亠亠亶亳 仆舒 NoSQL?(仍亠亞从仂)




VETMANAGER TEAM             26.4.12   弌. 15
弌舒仍仆仂亶 getEntity
            protected function getEntity(){
              $entity = new StoresRow();
              return $entity;
            }




VETMANAGER TEAM               26.4.12   弌. 16
个舒弍亳仆亶 getEntity
  protected function getEntity(){
    return $this->factory->create('StoresRow');
  }

丐亠
$ourMock = $this->getMock('StoresRow');
$factoryMock = $this->expects($this->once())
     ->method('create')
     ->will($this->returnValue($ourMock))

  VETMANAGER TEAM           26.4.12   弌. 17
getEntity
            protected function getEntity(){
              return $this->_entity;
            }




VETMANAGER TEAM               26.4.12   弌. 18
仂于亶 Stores
   class Stores {
      public function __construct($entity) {
        $this->_entity = $entity;
      }


   $injector->whenCreating('Stores')
     ->forVariable('entity')
     ->willUse('StoresRow');

VETMANAGER TEAM             26.4.12   弌. 19
仂于亶 亠 Stores
$this->entityMock->expects($this->once())
        ->method('import')
        ->with($this->equalTo($needParam));

$this->entityMock->expects($this->once())
        ->method('save');

$this->testObject->add($testData);



VETMANAGER TEAM           26.4.12    弌. 20
舒 ActiveRecord
 class StoresRow extends Entity {
    protected $_fields = array ();
    protected $_table = 'stores';
    public function __construct($database) {
      $this->setDB($database);
    }




VETMANAGER TEAM             26.4.12   弌. 21
丐亠 ActiveRecord
function testSaveWithoutId(){
  $this->dbMock->expects($this->once())
        ->method('query')
        ->with($this->stringContains("INSERT"));
  $this->testObject->import($testData);
  $this->testObject->save();
}




  VETMANAGER TEAM              26.4.12   弌. 22
Phemto 亳仂亞
    #wiring.php
    require_once 'phemto/phemto.php';
    $injector = new Phemto();
    $injector->forVariable('database')
    ->willUse(new Reused('VMDatabase'));
    $injector->whenCreating('Stores')
    ->forVariable('entity')->willUse('StoresRow');
    return $injector;
    #ajax_store.php
    $injector = include 'wiring.php';
    $stores = $injector->create('Stores');

VETMANAGER TEAM                26.4.12   弌. 23
亠

    舒从 亠亳仂于舒?(仍亠亞从仂)

    舒从 舒弍仂舒  仆亠从仂仍从亳仄亳 从仂仆仆亠从舒仄亳? (仍亠亞从仂)

    舒从 仗仂亟仄亠仆亳 弌丕?(仍亠亞从仂)

    舒从 亳仗仂仍亰仂于舒 于 亟亞仂仄 仗仂亠从亠?(仍亠亞从仂)

    舒从 仗亠亠亶亳 仆舒 NoSQL?(仍亠亞从仂)




VETMANAGER TEAM             26.4.12   弌. 24
亠




VETMANAGER TEAM           26.4.12   弌. 25
弌仗舒亳弍仂 亰舒 于仆亳仄舒仆亳亠!




VETMANAGER TEAM       26.4.12   弌. 26

More Related Content

Dependency injection, phemto

  • 1. Dependency Injection(DI), Phemto 仂仄舒仆亳亠于 仍舒亟亳仄亳 VETMANAGER TEAM 26.4.12 弌. 1
  • 2. VETMANAGER TEAM 26.4.12 弌. 2
  • 3. 仍舒仆 1)舒从 仗仍仂仂 弍亠亰 Dependency Injection 2)舒从 仂仂仂 Dependency Injection 3)舒从 仗仍仂仂 Dependency Injection 4)舒从 仂仂仂 Phemto 5)舒从 仂仂仂 :) VETMANAGER TEAM 26.4.12 弌. 3
  • 4. 舒从 仗仍仂仂 class Stores { function add($data){ $query = 'INSERT INTO stores SET title=? , assigned_user_id=?, type=?'; NDatabase::query($query, array($data['title'] , $data['assigned_user_id'], $data['type_id'])); } Stores::add VETMANAGER TEAM 26.4.12 弌. 4
  • 5. 舒从 仗仍仂仂 舒从 亠亳仂于舒?(仆亳从舒从) 舒从 舒弍仂舒 仆亠从仂仍从亳仄亳 从仂仆仆亠从舒仄亳? (仆亳从舒从) 舒从 仗仂亟仄亠仆亳 弌丕?(从仂亠-从舒从) 舒从 亳仗仂仍亰仂于舒 于 亟亞仂仄 仗仂亠从亠?(从仂亠-从舒从) 舒从 仗亠亠亶亳 仆舒 NoSQL?(仆亳从舒从) VETMANAGER TEAM 26.4.12 弌. 5
  • 6. 亠仍舒亠仄 亳仆亠从亳 class Stores{ public function __construct($database) { } function add($data){ $query = 'INSERT INTO stores SET title=? , assigned_user_id=?, type=?'; $this->_database ->query($query, $data); } VETMANAGER TEAM 26.4.12 弌. 6
  • 7. 舒从 亠亳仂于舒?(仍亠亞从仂) 舒从 舒弍仂舒 仆亠从仂仍从亳仄亳 从仂仆仆亠从舒仄亳? (仍亠亞从仂) 舒从 仗仂亟仄亠仆亳 弌丕?(仍亠亞亠) 舒从 亳仗仂仍亰仂于舒 于 亟亞仂仄 仗仂亠从亠?(仍亠亞亠) 舒从 仗亠亠亶亳 仆舒 NoSQL?(仆亳从舒从) VETMANAGER TEAM 26.4.12 弌. 7
  • 8. 亳亠仄 亠... class StoresTest extends PHPUnit_Framework_TestCase { function setUp(){ $this->dbMock = $this ->getMock('VMDatabase'); $this->testObject = new Stores($this->dbMock } VETMANAGER TEAM 26.4.12 弌. 8
  • 9. 亳亠仄 亠... function testAdd(){ $this->dbMock->expects($this->once()) ->method('query') ->with( $this->stringContains("INSERT") , $this->equalTo(array('title', 1, 2)) ); $this->testObject->add($testData); } VETMANAGER TEAM 26.4.12 弌. 9
  • 10. 舒从 仗仍仂仂 DI $stores = new Stores(new VMDatabase()); $stores = new Stores( new VMDatabase() , new VMRequest() , new VMSession() , new UserModel() ); VETMANAGER TEAM 26.4.12 弌. 10
  • 11. 舒从 仂仂仂 Phemto public function __construct(VMDatabase $database) { $this->_database = $database; } require_once 'phemto/phemto.php'; $injector = new Phemto(); $stores = $injector->create('Stores'); VETMANAGER TEAM 26.4.12 弌. 11
  • 12. 舒从 仂仂仂 Phemto2 public function __construct($database) { $this->_database = $database; } require_once 'phemto/phemto.php'; $injector = new Phemto(); $injector->forVariable('database') ->willUse(new Reused('VMDatabase')); $stores = $injector->create('Stores'); VETMANAGER TEAM 26.4.12 弌. 12
  • 13. 舒从 亠亳仂于舒?(仍亠亞从仂) 舒从 舒弍仂舒 仆亠从仂仍从亳仄亳 从仂仆仆亠从舒仄亳? (仍亠亞从仂) 舒从 仗仂亟仄亠仆亳 弌丕?(仍亠亞亠) 舒从 亳仗仂仍亰仂于舒 于 亟亞仂仄 仗仂亠从亠?(仍亠亞亠) 舒从 仗亠亠亶亳 仆舒 NoSQL?(仆亳从舒从) VETMANAGER TEAM 26.4.12 弌. 13
  • 14. ActiveRecord class Stores { function dataWrap($data){...} protected function getEntity(){...} function add($data){ $entity = $this->getEntity(); $entity->import($this->dataWrap($data)); $entity->save(); } VETMANAGER TEAM 26.4.12 弌. 14
  • 15. 亠?(getEntity) 舒从 亠亳仂于舒?(???) 舒从 舒弍仂舒 仆亠从仂仍从亳仄亳 从仂仆仆亠从舒仄亳?(???) 舒从 仗仂亟仄亠仆亳 弌丕?(仍亠亞从仂) 舒从 亳仗仂仍亰仂于舒 于 亟亞仂仄 仗仂亠从亠?(???) 舒从 仗亠亠亶亳 仆舒 NoSQL?(仍亠亞从仂) VETMANAGER TEAM 26.4.12 弌. 15
  • 16. 弌舒仍仆仂亶 getEntity protected function getEntity(){ $entity = new StoresRow(); return $entity; } VETMANAGER TEAM 26.4.12 弌. 16
  • 17. 个舒弍亳仆亶 getEntity protected function getEntity(){ return $this->factory->create('StoresRow'); } 丐亠 $ourMock = $this->getMock('StoresRow'); $factoryMock = $this->expects($this->once()) ->method('create') ->will($this->returnValue($ourMock)) VETMANAGER TEAM 26.4.12 弌. 17
  • 18. getEntity protected function getEntity(){ return $this->_entity; } VETMANAGER TEAM 26.4.12 弌. 18
  • 19. 仂于亶 Stores class Stores { public function __construct($entity) { $this->_entity = $entity; } $injector->whenCreating('Stores') ->forVariable('entity') ->willUse('StoresRow'); VETMANAGER TEAM 26.4.12 弌. 19
  • 20. 仂于亶 亠 Stores $this->entityMock->expects($this->once()) ->method('import') ->with($this->equalTo($needParam)); $this->entityMock->expects($this->once()) ->method('save'); $this->testObject->add($testData); VETMANAGER TEAM 26.4.12 弌. 20
  • 21. 舒 ActiveRecord class StoresRow extends Entity { protected $_fields = array (); protected $_table = 'stores'; public function __construct($database) { $this->setDB($database); } VETMANAGER TEAM 26.4.12 弌. 21
  • 22. 丐亠 ActiveRecord function testSaveWithoutId(){ $this->dbMock->expects($this->once()) ->method('query') ->with($this->stringContains("INSERT")); $this->testObject->import($testData); $this->testObject->save(); } VETMANAGER TEAM 26.4.12 弌. 22
  • 23. Phemto 亳仂亞 #wiring.php require_once 'phemto/phemto.php'; $injector = new Phemto(); $injector->forVariable('database') ->willUse(new Reused('VMDatabase')); $injector->whenCreating('Stores') ->forVariable('entity')->willUse('StoresRow'); return $injector; #ajax_store.php $injector = include 'wiring.php'; $stores = $injector->create('Stores'); VETMANAGER TEAM 26.4.12 弌. 23
  • 24. 舒从 亠亳仂于舒?(仍亠亞从仂) 舒从 舒弍仂舒 仆亠从仂仍从亳仄亳 从仂仆仆亠从舒仄亳? (仍亠亞从仂) 舒从 仗仂亟仄亠仆亳 弌丕?(仍亠亞从仂) 舒从 亳仗仂仍亰仂于舒 于 亟亞仂仄 仗仂亠从亠?(仍亠亞从仂) 舒从 仗亠亠亶亳 仆舒 NoSQL?(仍亠亞从仂) VETMANAGER TEAM 26.4.12 弌. 24
  • 25. 亠 VETMANAGER TEAM 26.4.12 弌. 25