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.

Navigating the Seas of Software Development

Navigating the Seas of Software Development

A Comprehensive Guide for Beginners

Geetanjali 10 Jan 2024
Empowering Business Excellence

Empowering Business Excellence

SGR Software Solution's Journey in Digital Transformation

Geetanjali 5 Jan 2024
SOLID with a Simple Example

"SOLID" with a Simple Example

In this article, we will learn the SOLID principle with a simple example. In an interview, this is a common...

Geetanjali 26 Dec 2023
Integrating Auto Mapper in ASP.NET Core

Integrating Auto Mapper in ASP.NET Core

This article demonstrates what Automapper is and how we can integrate Automapper to our ASP.NET...

Geetanjali 24 Dec 2023
Replace Conditional Statements (IF/ELSE Or SWITCH) With Factory

Replace Conditional Statements (IF/ELSE Or SWITCH) With Factory

In this article, we will learn how we can replace long IF/ELSE or long SWITCH (long conditional statement)...

Geetanjali 23 Dec 2023