AutoMapper in C# is a mapper between two objects. That is, AutoMapper is an object-object mapper. It maps the properties of two different objects by transforming the input object of one type to the output object of another type.
Let's understand why we need automapper in C# with an example. Let's say we have two classes such as Employee and EmployeeDTO as shown in the below image.
In case we want to copy or transfer data from one CustomerModel to Customer object then it would be more lines of code.
Will see step by step how we can integrate Automapper to a project.
Add the AutoMapper.Extensions.Microsoft.DependencyInjection Package to your solution via NuGet.
Create two files as Customer.cs and CustomerModel.cs and add class Customer and CustomerModel.
Create mapping profile as CustomerProfile.cs file
Configure AutoMapperConfiguration in the Startup.cs as shown below
To invoke the mapped object in code, do something like the following:
In this article, we have seen what Automapper is and how we can integrate Automapper to our ASP.NET Core application.