akkun_choi pedia
mindiaakkun_choi pedia見出し語

トランザクションスクリプト

akkun_choi · 2009-03-08 更新

ドメインロジックでも最もシンプルなパターン
ドメインオブジェクトは作らない。

ビジネスロジックの一連の処理をまとめる。処理の関連するものを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;
}
}
||<

これに対してドメインモデルドメインオブジェクトを用いる。例えばこの例なら「Page」というオブジェクトを作る。

関連する見出し語