title: Repository and Data Mapper Coupling
body: <p>I have a controller that acquires data to pass to a view. Into this is injected (via a pimple container) a service which uses a number of domain models + business logic to create the data. </p>
  
  <p>The service itself has a 'repository' class injected into it which has methods for creating data mappers and returning a domain model instance. </p>
  
  <p>I'm aware that I might not have got my head around the repository concept as <a href="https://martinfowler.com/eaaCatalog/repository.html" rel="nofollow noreferrer">Martin Fowler</a> puts it to "build another layer of abstraction over the mapping layer" &amp; "A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection." So I may be using this term erroneously.</p>
  
  <p>service:</p>
  
  <pre><code>class InputService
      {
          private $repos;
  
          public function __construct($repo) {
              $this-&gt;repos = $repo;
          }
  
          public function getInitialData()
          {
              $product = $this-&gt;repo-&gt;getProduct();
              $country = $this-&gt;repo-&gt;getCountry();
              $spinalPoint = $this-&gt;repo-&gt;getPoint();
  
              /*business logic with model instances to produce data array*/
  
              return //array of data
          }
      }
  </code></pre>
  
  <p>repository:</p>
  
  <pre><code>class InputRepository
      {
          private $db;
  
          public function __construct($db) {
              $this-&gt;db = $db;
          }
  
          public function getCountry()
          {
              $mapper = new CountryMapper($this-&gt;db);
              $country = $mapper-&gt;fetch();
              return $country; //returns country object
          }
          // lots of other methods for returning different model objects
      }
  </code></pre>
  
  <p>mapper:</p>
  
  <pre><code>class CountryMapper
      {
          private $db;
  
          public function __construct($db) {
              $this-&gt;db = $db;
          }
  
          public function fetch()
          {
              $data = //code to grab data from db;
  
              $obj = new Country($data);
              return $obj;
          }
      }
  </code></pre>
  
  <p>As you can see, the mappers are tightly coupled to the repository class, however I can't see a way around it.</p>
  
  <p>I was wondering if there is a way to implement this repository that provides looser coupling to the data mapper classes? </p>
  
  <p>In the grand scheme of things this application is fairly small and so having to update code across both wouldn't be disastrous, but you never now when thing will grow! </p>