トランザクションスクリプト
ドメインロジックでも最もシンプルなパターン。
ドメインオブジェクトは作らない。
ビジネスロジックの一連の処理をまとめる。処理の関連するものを1クラスにまとめる方法と、Command パターンによる方法がある。
例)「非公開ページは取得できない」というロジックをはさむ。
>|php|
<?php
class PageGateway{
protected $pdo;
protected $findSql = 'select * from page where id = ?';
function find($id){
return $this->pdo->query($this->findSql, $id);
}
}
class PageService{
protected $db;
function __construct(PageGateway $db){
$this->db = $db;
}
function findPage($id, $user){
$result = $this->db->find($id);
if ($result['status'] === 'private'
&& $result['user_id'] != $user){
return null;
}
return $result;
}
}
||<