title: Repository Pattern Dilemma: Redundant Queries vs. Database Round Trips
body: <p><strong>This is the situation:</strong></p>
  
  <p>Say I have an application in which two entity types exist:</p>
  
  <ul>
  <li><code>Company</code></li>
  <li><code>Person</code></li>
  </ul>
  
  <p>Moreover, <code>Person</code> has a reference to <code>Company</code> via <code>Person.employer</code>, which denotes the company a person is employed at.</p>
  
  <p>In my application I am using <em>repositories</em> to separate the database operations from my business-model related services: I have a <code>PersonRepository.findOne(id)</code> method to retrieve a <code>Person</code> entity and a <code>CompanyRepository.findOne(id)</code> method to retrieve a <code>Company</code>. So far so good.</p>
  
  <p><strong>This is the dilemma:</strong></p>
  
  <p>Now if I make a call to <code>PersonRepository.findOne(id)</code> to fetch a <code>Person</code> entity, I also need to have a fully resolved <code>Company</code> included inline via the <code>Person.employer</code> property – and this is where I am facing the dilemma of having two implementation options that are both suboptimal:</p>
  
  <p><strong>Option A)</strong> <em>Redundant queries throughout my repositories but less database round trips:</em></p>
  
  <p>Within the <code>PersonRepository</code> I can build a query which selects the user and also selects the company in a single query – however, the <code>select</code> expression  for the company is difficult and includes some joins in order to assemble the company correctly. The <code>CompanyRepository</code> already contains this logic to <code>select</code> the company and rewriting it in the <code>UserRepository</code> is redundant. Hence, ideally I only want the <code>CompanyRepository</code> to take care of the company selection logic in order to avoid having to code the same query expression redundantly in two repositories.</p>
  
  <p><strong>Option B):</strong> <em>Separation of concerns without query-code redundancy but at the price of additional db roundtrips and repo-dependencies:</em></p>
  
  <p>Within the <code>PersonRepository</code> I could reference the <code>CompanyRepository</code> to take care of fetching the <code>Company</code> object and then I would add this entity to the <code>Person.employer</code> property in the <code>PersonRepository</code>. This way, I kept the logic to query the company encapsulated inside the <code>CompanyRepository</code> by which a clean separation of concerns is achieved. The downside of this is that I make additional round trips to the database as two separate queries are executed by two repositories.</p>
  
  <p>So generally speaking, what is the preferred way to deal with this dilemma?</p>
  
  <p>Also, what is the preferred way to handle this situation in ASP.NET Core and EF Core?</p>
  
  <p><strong>Edit:</strong> To avoid opinion based answers I want to stress: I am not looking for a pros and cons of the two options presented above but rather striving for a solution that integrates the good parts of both options – because maybe I am just on the wrong track here with my two listed options. I am also fine with an answer that explains why there is no such integrative solution, so I can sleep better and move on.</p>