Automapper
Installation
With ASP.Net Core - use the nuget package manager to install Automapper.Extensions.Microsoft.DependencyInjection
Adding into your program
Startup.CS
Adding to program
Update startup.cs
1 | using AutoMapper; |
Then add classes to hold mapping information
CompanyMapping.cs
1 | using AutoMapper; |
In this case, I have a CompanyViewModel, and a CompanyEntity objects. CompanyViewModel is what is exposed to the API, CompanyEntity is used for database.
Then I have methods like this
CompanyService.cs
1 | : |
Line 9, is where the mapper is called, this converts a list of CompanyEntity objects into a list of CompanyViewModel objects.