What is Auto Mapper and why we use Auto Mapper in ASP.Net Core ?

Auto Mapper is a Simple Library that helps us to transform one object type into other object type.
In simple words we can say that The Auto Mapper in C# is mapper between two objects i.e object to object mapper . It maps the properties of two different objects by transforming the input object of one type to the other type.

Why we need Auto Mapper in ASP.Net Core ?

If you want to copy the data from one object or model to another object or model , then in traditional approach is very time consuming and complex . Auto Mapper fix all these issues.
To use Auto Mapper we need to install Automapper library from nuget package manager and need to add automapper middileware in program.cs file.
For mapping one type to another type Automapper looks for a mapping profiles so we need to create a mappingprofile class and derives it from base Profile class that has CreateMap method to map the source and destination object mapping.

To start with create a Web API project using the web api template as shown in below image and name the project then select for some default options for minimal api's and target framework as like in below images.

Image is not available
Image is not available
Image is not available

Install the Automapper package from nuget package manager as shown in below image.

Image is not available

After installing the packages your solution explorer will be looks like as below image.

Image is not available

Now add the Automapper Middileware in Program.cs file as shown in below code and image.

Image is not available
                    
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddAutoMapper(typeof(Program).Assembly);
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.Run();
 

Now add Employee Model class and add the below properties into it.

                    
public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; } = String.Empty;
        public string Address { get; set; } = String.Empty;
        public int Salary { get; set; }
        public string Email { get; set; }= String.Empty;
        public DateTime Created { get; set; } 
        public DateTime DOB { get; set; }
    }
 

Now add EmployeeDTO class and add the below properties into it.

                    
public class EmployeeDTO
    {
          public string Name { get; set; } = String.Empty;
        public string Address { get; set; } = String.Empty;
        public int Salary { get; set; }
        public string Email { get; set; } = String.Empty;
    }
 

Now add a AutoMapperProfile class for mapping the objects and add the below code for createmap between source and destination object classes.

                    
 public class AutoMapperProfile :Profile
    {
        public AutoMapperProfile()
        {
            CreateMap Employee, EmployeeDTO ();
            CreateMap EmployeeDTO, Employee ();
        }
    }
 
Image is not available

Now create a controller and use a readonly property of IMapper into the constructor and writedown the two action methods one for Get and one for Post and use the mapper.Map() method to map the objects automatically and then run the application as shown in the below images, it will work as expected.

Image is not available
Image is not available
Image is not available
Image is not available

Image is not available

About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server, EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.