In every application, there will be a need to map the object of one type to object of another type. Automapper is used to map these objects of dissimilar types without the need to write boring code to map each data member. Automapper in ASP.NET core comes handy when mapping objects between Model & DTO.

What is automapper & when to use it?

Automapper

Automapper is a popular small library for an object to object mapping used to map objects of different classes.

Let’s say you have two different classes EmployeeDTO & EmployeeModel with the same data members and you need to copy data from one object to another. Now you cannot set an instance of one object to another object as though data member match but objects are of different classes. You will have to use the traditional approach i.e. copy data from one object to another field by field.

To simply this job of mapping data from one object to objects Auto Mapper can be used. Auto Mapper is an open-source library and It maps the properties of two different objects.

Setup project for demonstration

Create new ASP.NET Core API Project

For implementation purpose, I have created a default ASP.NET Core 3.1 API Project using Visual Studio 2019

ASP.NET Core 3.1 API Project Creation

Install automapper NuGet package

Install package AutoMapper.Extensions.Microsft.DependencyInjection & this package depends on the AutoMapper package which will also get installed with this package.

NuGet Extension

#programming #asp.net core 3.1 #asp.net core automapper #automapper #automapper .net

Implement Automapper in ASP.NET Core 3.1
11.20 GEEK