3

タイトルがこの質問を説明する最良の方法であるかどうかはわかりません。

この本 ( http://apress.com/book/view/9781590599099 ) は、Unit of Work パターンの実装を示しています。こんな感じで少し進みます。

class UoW(){
   private array $dirty;
   private array $clean;
   private array $new;
   private array $delete;

   public static function markClean(Entity_Class $Object){
   //remove the class from any of the other arrays, add it to the clean array
   }

   public static function markDirty(Entity_Class $Object){
   //remove the class from any of the other arrays, add it to the dirty array
   }

   public static function markNew(Entity_Class $Object){
   //add blank entity classes to the new array
   }

   public static function markDelete(Entity_Class $Object){
   //remove the class reference from other arrays, mark for deletion
   }

   public function commit(){
   //perform all queued operations, move all objects into new array, remove any deleted objects
   }
}

class MyMapper(){
  public function setName($value){
     $this->name = $value;
     UoW::markDirty($this);//here's where the problem is
   }
} 

(静的呼び出しと依存関係の問題をしばらく無視します)

著者は、この実装ではコーダーが関連する UoW マーキング メソッドを挿入する必要があり、パターンを選択的に尊重するとエラーが発生する可能性があることに注意しています。ここで、具象アクセサーの長所と短所を踏まえて、次のように UoW 呼び出しを自動化できます。

public function __set($key,$value){
   //check to see if this is valid property for this class
   //assuming the class has an array defining allowed properties 
   if(property_exists($this,$key)){
       $this->$key = $value;
       UoW::markDirty($this);

       /*
       * or alternatively use an observer relationship 
       * i.e. $this->notify();
       * assuming the UoW has been attached prior to this operation
       */
      }
   } 

それで、私の質問は、ドメイン オブジェクトにプロパティを設定するときに、適切な UoW メソッドが呼び出されたことをどのように保証しますか?

4

1 に答える 1

0

私が見つけた最善の方法は、プロパティを「保護」と宣言してから、PHPDoc コメントを使用してそれらをパブリック プロパティとして「公開」することです。何かのようなもの:

/**
 * @property string $prop
 */
class Foo extends UoW {
    protected $prop = "foo";

    public function __set($name, $value) {
        if (property_exists($this, $name)) {
            $this->$name = $value;
            $this->markDirty();
        }
    }
}

これにより、ダーティ マークが設定されていることを確認しながら、IDE とほとんどのツールが Foo->prop がプロパティであることを認識します。

于 2013-07-25T07:49:58.763 に答える