Mapping the API results to DTOs

The entities definitions that we've used to work with EF Core are designed to give the best results when working with the database; however, they are not designed to be transferable over the wire. There are a few reasons why you wouldn't want to use the same classes that are mapped to your database tables and when sending results from your APIs:

  • Security: Your domain model classes might include various properties that you wouldn't want to expose to your clients. For example, personal information or user roles.
  • Loose coupling: Over time, you might need to change the data model; many times, these changes shouldn't be reflected in changes to your API, but sharing the same data types will force you to.
  • Controllable ...

Get Hands-On Full-Stack Web Development with ASP.NET Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.