title: Repository with Multiple Entities Creating a DTO
body: <p>Doing some C# code review here, and noticed a developer doing a join with 5 additional tables, and an inline IQueryable projection into a DTO. The tables being joined in the repository are <strong>User</strong>, <strong>Appointment</strong>, <strong>CommunicationPreference</strong>, <strong>UserProduct</strong>, and <strong>Product</strong>. I didn't design the system, and i'm probably simplifying it somewhat, but let's just go with this as is for now. Using Entity Framework and Web API.</p>
  
  <p>This is a little long so I apologize, but really was curious to hear others' thoughts on this matter.</p>
  
  <p>So these tables relate in that each User has a list of upcoming service appointments to service their products. The UserProduct table shows the relationship between a product and a user, and the product table shows the kinds of products. CommunicationPreference deals with how they want to be contacted about things like when their service is due, if someone is coming to service the product, how they want to be contacted (phone email text etc).</p>
  
  <p>So the developer joins these tables together in the repository itself, and then creates a DTO called "UserServiceOverviewDto" that is returned to the controller and then the front end and basically has information to display in a grid to the customer representative (who the user is, upcoming appointments, what products, etc. basically everything I just wrote above).</p>
  
  <p>I understand why they did this - instead of making 5 calls to 5 different repositories, they did it all in one, and then instead of having to convert each one to a DTO, they did it all in one shot. But I understand that a repository shouldn't return a DTO, either. And ideally your repository should return something that logically is aggregated and representative of one entity that solves your problem.</p>
  
  <p>As I was thinking about this, I stumbled upon this in SO <a href="https://stackoverflow.com/questions/14986035/what-type-to-return-when-querying-multiple-entities-in-repository-layer">What type to return when querying multiple entities in Repository layer?</a> but it doesn't really help me enough and wanted to ask for more advice on what to do.</p>
  
  <p>Looking at this from a DDD perspective, I'm wondering what new "thing" needs to be introduced. Maybe I need to create a new context/repository that is this new thing. If i'm trying to say that this is a common grouping  and give it a name like the "X" does this mean I want to create a new database table to represent that? Probably not, because I already have the tables that store this information.</p>
  
  <p>Does this mean that instead I want to create a new class called "X" without a corresponding database table? So how is this any different from a DTO? In fact, it almost suggests that by making this class, i'm trying to make my repository more pure by instantiating X instead of XDto and then casting X to a DTO in a service layer anyway (I haven't gained anything other than shifting stuff around and creating a very anemic entity).</p>
  
  <p>So in summary, then, the question is:</p>
  
  <p>If I have multiple entities that I'm grouping together for convenience on the front end, is it better to:</p>
  
  <ol>
  <li>Have the service layer make 5 distinct database calls, and assemble a DTO in this layer to return to the controller? Isn't this bad? 5 calls just to make my code seem more "pure"? Isn't it better to do your joins in one shot and get back what you need from the database?</li>
  <li>Create a new class in C# that represents the marriage of these 5 tables, and then implement a mapping between this class and the corresponding DTO. This class would not be persisted as I said above.</li>
  <li>Have the repository perform its joins to create a new DTO as an inline projection (seems wrong b/c repositories should return an entity not a DTO)</li>
  </ol>
  
  <p>Really looking forward to some feedback here! Thanks!</p>