views:

55

answers:

1

I'm not sure if the title is the best way to describe this question.

This book - http://apress.com/book/view/9781590599099 - illustrates an implementation of the Unit of Work pattern. It goes a little something like this.

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

(Ignoring issues of static calls and dependencies for a moment)

The author notes that this implementation requires the coder to insert the relevant UoW marking methods, and that this elective honouring of the pattern can lead to errors. Now, taking on board the pros and cons of concrete accessors, you could perhaps automate the UoW invocation like this:

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
       */
      }
   } 

So, my question is, how would you go about guaranteeing the appropriate UoW method was called when setting properties on a Domain Object?

A: 

There is several approaches you can use for making sure. The Unit of Work and other patterns are described in detail in Patterns of Enterprise Application Architecture by Martin Fowler. You can read excerpts of it (including the relevant UoW pages) in Google Books. Fowler mentions Object Registration, Caller Registration and UoW Controller as possible approaches.

Gordon
I've got the book right here on my desk - but none of the methods "automate" registration or notification. Object and caller registration omits the need to mark data changes in the object, but require that all changes to the model be performed by the UoW itself in a commit().
sunwukung
@sunwukung well, he mentions AOP, but that is admittedly barely feasible for PHP yet. Observer patterns sounds good to me though.
Gordon
Observer pattern is great if UoW is the sole means of persistence, but if the implementing code attempts persistence via the data mapper - it becomes ineffective. The problem is the reliance on referencing the UoW->notify()/UoW::markStatus method in the accessors. If accessors are replaced with magic methods, it would automate the notification without in the base class. Thanks for your comments though.
sunwukung