Integrate AutoMapper In ASP.NET Core Web API

 AutoMapper is a component that helps to copy data from one type of object to another type of object. It is more like an object-object mapper.

AutoMapper is an object-object mapper. Object-object mapping works by transforming an input object of one type into an output object of a different type.

Why do we need to use AutoMapper?

When we have to map two different objects we can use AutoMapper. One of the problems is, while building the application we have some entities which are dealing with DB (forex DTO’s) and for some entities we are dealing with a client. So we have to map those DTO’s to the entities dealing with clients.

How AutoMapper works?

AutoMapper internally uses the concept called Reflection. Reflection in C# is used to retrieve metadata on types at runtime. With the help of Reflection, we can dynamically get a type of existing object and invoke its methods or access its fields and properties.

We can implement AutoMapper using the following steps : -

1. Install AutoMapper Nuget Package

Open Package Manager Console and run below command - 

Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection 

2. Configure AutoMapper for our ASP.NET Core Web API. 

So open the Startup.cs class and add the below code into ConfigureServices method.

public void ConfigureServices(IServiceCollection services)  

{  

    services.AddControllers();  

    services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());  

}  

3. Create two classes called Student.cs and StudentDTO.cs

namespace automapper_sample  

{  

    public class Student  

    {  

        public string Name { get; set; }  

        public int Age { get; set; }  

        public string City { get; set; }  

    }  

}  

namespace automapper_sample  

{  

    public class StudentDTO  

    {  

        public string Name { get; set; }  

        public int Age { get; set; }  

        public string City { get; set; }  

    }  

}  

4. Creating Profiles

Create a new class called AutoMapperProfile.cs which inherits from Profile class of AutoMapper. Use CreateMap<source, destination>() to create a mapping between classes.

using AutoMapper;  

namespace automapper_sample  

{  

    public class AutoMapperProfile : Profile  

    {  

        public AutoMapperProfile()  

        {  

            CreateMap<StudentDTO, Student>();  

        }  

    }  

}  

5. Using IMapper

IMapper interface is used to map two objects. Create a new controller called StudentController.cs. Resolve the IMapper dependency in the controller constructor.

using AutoMapper;  

using Microsoft.AspNetCore.Mvc;  

namespace automapper_sample.Controllers  

{  

    [Route("api/[controller]")]  

    public class StudentController : Controller  

    {  

        private readonly IMapper _mapper;  

        public StudentController(IMapper mapper)  

        {  

            _mapper = mapper;  

        }  

        // GET: api/<controller>  

        [HttpGet]  

        public Student Get()  

        {  

            StudentDTO studentDTO = new StudentDTO()  

            {  

                Name = "Student 1",  

                Age = 25,  

                City = "New York"  

            };  

            return _mapper.Map<Student>(studentDTO);  

        }  

    }  

}