title: Encapsulating common logic (domain driven design, best practices)
body: <p><strong>Updated:</strong> 09/02/2009 - Revised question, provided better examples, added bounty.</p>
  
  <hr>
  
  <p>Hi,<br>
  I'm building a PHP application using the data mapper pattern between the database and the entities (domain objects). My question is:</p>
  
  <p><em>What is the best way to encapsulate a commonly performed task?</em> </p>
  
  <p>For example, one common task is retrieving one or more site entities from the site mapper, and their associated (home) page entities from the page mapper. At present, I would do that like this:</p>
  
  <pre><code>$siteMapper = new Site_Mapper();
  $site = $siteMapper-&gt;findByid(1);
  
  $pageMapper = new Page_Mapper();
  $site-&gt;addPage($pageMapper-&gt;findHome($site-&gt;getId()));
  </code></pre>
  
  <p>Now that's a fairly trivial example, but it gets more complicated in reality, as each site also has an associated locale, and the page actually has multiple revisions (although for the purposes of this task I'd only be interested in the most recent one).</p>
  
  <p>I'm going to need to do this (get the site and associated home page, locale etc.) in multiple places within my application, and I cant think of the best way/place to encapsulate this task, so that I don't have to repeat it all over the place. Ideally I'd like to end up with something like this:</p>
  
  <pre><code>$someObject = new SomeClass();
  $site = $someObject-&gt;someMethod(1); // or
  $sites = $someObject-&gt;someOtherMethod();
  </code></pre>
  
  <p>Where the resulting site entities already have their associated entities created and ready for use.</p>
  
  <p>The same problem occurs when saving these objects back. Say I have a site entity and associated home page entity, and they've both been modified, I have to do something like this:</p>
  
  <pre><code>$siteMapper-&gt;save($site);
  $pageMapper-&gt;save($site-&gt;getHomePage());
  </code></pre>
  
  <p>Again, trivial, but this example is simplified. Duplication of code still applies.</p>
  
  <p>In my mind it makes sense to have some sort of central object that could take care of:</p>
  
  <ul>
  <li>Retrieving a site (or sites) and all nessessary associated entities</li>
  <li>Creating new site entities with new associated entities</li>
  <li>Taking a site (or sites) and saving it and all associated entities (if they've changed)</li>
  </ul>
  
  <p>So back to my question, what should this object be?</p>
  
  <ul>
  <li>The existing mapper object?</li>
  <li>Something based on the repository pattern?*</li>
  <li>Something based on the unit of work patten?*</li>
  <li>Something else?</li>
  </ul>
  
  <p>* I don't <em>fully</em> understand either of these, as you can probably guess.</p>
  
  <p>Is there a standard way to approach this problem, and could someone provide a short description of how they'd implement it? I'm not looking for anyone to provide a fully working implementation, just the theory.</p>
  
  <p>Thanks,<br>
  Jack</p>