Mapping entities to DTOs with AutoMapper

AutoMapper (http://automapper.org/) is an object-to-object mapper that use conventions and configurations to reduce the amount of code you need to write. To use AutoMapper, you need to install the AutoMapper NuGet package and then configure the mapping you wish to support. After the mappings are configured, you need to create an instance of the mapper and use it to run mappings.

Since such mappings are stateless and exist across your application, I usually define them inside the static constructor of the classes that need them (in our case, the Controller), and then use the mapper throughout application lifetime, but if the mappings are shareable between classes, then you can create a shared class ...

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.