Technology

Integrating AutoMapper in ASP.NET Core

By, administrator
  • 12 Oct, 2024
  • 45 Views
  • 0 Comment

Tools Used for development

  • Visual Studio 2019

What is AutoMapper?

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.

Why do we need AutoMapper in C#?

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.

Step by step walkthroughs

Will see step by step how we can integrate Automapper to a project.

Step 1

Add the AutoMapper.Extensions.Microsoft.DependencyInjection Package to your solution via NuGet.

Step 2

Create two files as Customer.cs and CustomerModel.cs and add class Customer and CustomerModel.

Step 3

Create mapping profile as CustomerProfile.cs file

Step 4

Configure AutoMapperConfiguration in the Startup.cs as shown below

Step 5

To invoke the mapped object in code, do something like the following:

Conclusion

In this article, we have seen what Automapper is and how we can integrate Automapper to our ASP.NET Core application.

Leave a comment

Your email address will not be published. Required fields are marked *