title: Mapping a User entity into a User DTO in .NET Core
body: <p>I'm developing a web app that contains a <code>User</code> entity that is derived from .NET Core's <code>IdentityUser</code>. Lets suppose there is another entity called <code>Comment</code> which has a relation to a user (the user who posted the comment):</p>
  
  <pre><code>public class User : IdentityUser
  {
      public string SomeExtraField { get; set; }
  }
  
  public class Comment
  {
  
      //Owner (Creator) of the feedback
      public User User { get; set; }
      //body of the comment
      public string Body { get; set; }    
  }
  </code></pre>
  
  <p>Now suppose I have an API endpoint that returns all of the comments in the system. If I query for all comments and include the User relation, when the object gets serialized, everything in the User class is serialized and sent to the client (including the users hashed password, etc). Obviously I don't want this. So I've created a <code>CommentService</code> layer that grabs the <code>Comments</code> from a <code>CommentRepository</code>. From my understanding, the service layer should do the job of mapping the raw <code>Comment</code> object into a Comment DTO, which only contains data that should be sent to the client. I've defined a comment and user DTO like this:</p>
  
  <pre><code>public class UserOutput
  {
      public string Id { get; set; }
      public string SomeExtraField { get; set; }
  }
  
  public class CommentOutput
  {
      public UserOutput User { get; set; }
      public string Body { get; set; }
  }
  </code></pre>
  
  <p>Then in my service layer I have something like the following:</p>
  
  <pre><code>//Fetch all comments
  var list =  await _repository.ListAsync();
  
  //Map comments to DTO
  var result = list.Select(x =&gt; new CommentOutput
  {        
      Body = x.Body,
      User = new UserOutput
      {
          Id = x.User.Id,
          SomeExtraField = x.User.SomeExtraField,
      }
  });
  </code></pre>
  
  <p>This all seems to work great. However I can foresee one problem. Lets say I have a large system with Comments, Posts, Likes, Private Messages, etc. I can map them all in a similar fashion above. Then one day I decide to add another field to the UserOutput DTO. Now I have to go through potentially hundreds of mapping code like the sample above to map the new field properly, and whats worse is the compiler wont tell me if I've missed anything. I would like to have a function somewhere that maps a <code>User</code> to a <code>UserOutput</code> but I don't know where it should go.</p>
  
  <p>I've seen some suggestions to put a constructor to the DTO that does the mapping:</p>
  
  <pre><code>public class UserOutput
  {
      public UserOutput(User user)
      {
          Id = user.Id;
          SomeExtraField = user.SomeExtraField
      }
      public string Id { get; set; }
      public string SomeExtraField { get; set; }
  }
  </code></pre>
  
  <p>but I've seen people against this because it tightly couples the DTO with the Entity. I've also seen suggestions of using Auto Mapper but is also seems an equal amount of people are against it.</p>
  
  <p>Where should I place code that can perform these DTO->entity and entity->DTO mappings so I don't repeat myself all over the place?</p>