title: PHP - Where should Unit Of Work lie in an MVC application?
body: <h1>Background Info for Question</h1>
  
  <p>In my index.php file I have this:</p>
  
  <pre class="lang-php prettyprint-override"><code>$service_factory = new ServiceFactory(new MapperFactory($db), new DomainFactory);
  </code></pre>
  
  <p>My understanding of how a UnitOfWork should work with my application is this (state is referring to dirty, clean, etc.):</p>
  
  <ul>
  <li>Mapper - marks an object's state 
  <ul>
  <li>Either <code>$user-&gt;markNew();</code> or <code>$uow-&gt;registerNew($user);</code></li>
  </ul></li>
  <li>Domain - can check it's current state. Ex: <code>$this-&gt;isDirty();</code></li>
  <li>Service - where <code>commit()</code> is called to complete the transactions
  <ul>
  <li>Either <code>$this-&gt;uow-&gt;commit()</code> or <code>$mapper-&gt;commit()</code></li>
  </ul></li>
  </ul>
  
  <p>My commit function of my UnitOfWork looks like this (feel free to improve):</p>
  
  <pre class="lang-php prettyprint-override"><code>public function commit()
  {
      // Inserts the new objects
      foreach ($this-&gt;newObjects as $newObject) {
          $mapper = $this-&gt;mapperFactory-&gt;createFromDomainObject($newObject);
          $mapper-&gt;insert($newObject);
      }
  
      // Update the dirty objects
      foreach ($this-&gt;dirtyObjects as $updateObject) {
          $mapper = $this-&gt;mapperFactory-&gt;createFromDomainObject($updateObject);
          $mapper-&gt;update($updateObject);
      }
  
      // Delete the removed
      foreach ($this-&gt;deletedObjects as $deletedObject) {
          $mapper = $this-&gt;mapperFactory-&gt;createFromDomainObject($deletedObject);
          $mapper-&gt;delete($deletedObject);
      }
  }
  </code></pre>
  
  <h1>Option #1 - ServiceFactory</h1>
  
  <p><strong>Pro</strong>: If I have one service calling another, I won't have to worry about not passing Domain objects between each other, since they will share the same instance of the UnitOfWork.</p>
  
  <p><strong>Con</strong>: I would have to pass the UnitOfWork object all the way down to the mapper object, which would be this many levels: ServiceFactory -> Service -> MapperFactory -> Mapper... Ew. That doesn't sit well with me.
  Also, since the UnitOfWork requires a MapperFactory, my index.php would change to this:</p>
  
  <pre class="lang-php prettyprint-override"><code>$uow = new UnitOfWork(new MapperFactory($db));
  $mapper_factory = new MapperFactory($db, $uow);
  $service_factory = new ServiceFactory($mapper_factory, new DomainFactory, $uow);
  </code></pre>
  
  <p>As you can see, the it's like a bundle of UnitOfWorks and MapperFactories everywhere, just not pretty.</p>
  
  <h1>Option #2 - Service</h1>
  
  <p><strong>Pro</strong>: I wouldn't have an ugly index.php file. I would just create a new instance of UnitOfWork for each Service object. Inside my ServiceFactory would look like this:</p>
  
  <pre class="lang-php prettyprint-override"><code>/**
   * Create method
   *
   * This will create a new Service class if it hasn't 
   * already been instantiated, and return it.
   *
   * @param string $name          The class name
   * @return mixed
   */
  public function create($name)
  {
      $class = '\\MyApp\\Service\\' . $name;
      if ( array_key_exists($class, $this-&gt;cache) === false) {
          if (class_exists($class)) {
              $uow = new UnitOfWork($this-&gt;mapperFactory);
              $this-&gt;cache[$class] = new $class(
                  $this-&gt;mapperFactory, 
                  $this-&gt;domainFactory,
                  $uow
              );
          } else {
              throw new Exception(sprintf('Service class %s does not exist.', $class));
          }
      }
      return $this-&gt;cache[$class];
  }
  </code></pre>
  
  <p>This seems a lot cleaner to me.</p>
  
  <p><strong>Con</strong>: I would not be able to have services call each other. However, I am in the beginning stages of this project, and this is my first attempt at a somewhat "correct" MVC application, so I'm not sure if I will have Services calling each other a lot or not at all.</p>
  
  <h1>Option #3 - MapperFactory</h1>
  
  <p><strong>Pro</strong>: Not really any that I can see, but it's still an option. In my index.php I could have this:</p>
  
  <pre class="lang-php prettyprint-override"><code>$uow = new UnitOfWork($db);
  $service_factory = new ServiceFactory(new MapperFactory($db,$uow), new DomainFactory);
  </code></pre>
  
  <p>My UnitOfWork class could extend the MapperFactory class, and therefore not needing to create two instances of MapperFactory. I would pass the same instance of the UnitOfWork to each Mapper.</p>
  
  <p><strong>Con</strong>: I would have to commit the transactions in my Service class by doing something like this:</p>
  
  <pre class="lang-php prettyprint-override"><code>$this-&gt;mapperFactory-&gt;unitOfWork-&gt;commit();
  </code></pre>
  
  <p>Too many function calls for my liking. Also, I'm not sure how I would allow the Domain Objects to check their state without a reference to the UnitOfWork Object.</p>
  
  <h2>Maybe there's something I'm missing, but any suggestions or helpful insight is greatly appreciated!</h2>