Integrating AutoMapper In ASP.NET Core

This article demonstrates what Automapper is and how we can integrate Automapper to our ASP.NET Core application.
 
Download Source code from the given GitHub URL.
 
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.
  1. public class Customer {  
  2.     public int CustomerId { getset; }  
  3.     public string CompanyName { getset; }  
  4.     public string FirstName { getset; }  
  5.     public string MiddleName { getset; }  
  6.     public string LastName { getset; }  
  7.     public string Address { getset; }  
  8.     public string Phone { getset; }  
  9.     public string Country { getset; }  
  10.     public string City { getset; }  
  11.     public string Pincode { getset; }  
  12. }  
  13.   
  14. public class CustomerModel {  
  15.     public int CustomerId { getset; }  
  16.     public string FullName { getset; }  
  17.     public string Phone { getset; }  
  18.     public string Country { getset; }  
  19.     public string City { getset; }  
  20.     public string Pincode { getset; }  
  21. }  
Step 3
 
Create mapping profile as CustomerProfile.cs file
  1. public class CustomerProfile : Profile {  
  2.     public CustomerProfile () {  
  3.         // Mapping properties from Customer to CustomerModel  
  4.         CreateMap<Customer, CustomerModel> ()  
  5.             .ForMember (dest =>  
  6.                 dest.FullName,  
  7.                 opt => opt.MapFrom (src => src.FirstName + " " + src.MiddleName + " " + src.LastName));  
  8.         // ForMember is used incase if any field doesn't match  
  9.     }  
  10. }  
Step 4
 
Configure AutoMapperConfiguration in the Startup.cs as shown below
  1. public void ConfigureServices (IServiceCollection services) {  
  2.     // ...  
  3.     // Auto Mapper Configurations  
  4.     var mappingConfig = new MapperConfiguration (mc => {  
  5.         mc.AddProfile (new CustomerProfile ());  
  6.     });  
  7.     IMapper mapper = mappingConfig.CreateMapper ();  
  8.     services.AddSingleton (mapper);  
  9. }  
Step 5
 
To invoke the mapped object in code, do something like the following:
  1. public class HomeController : Controller {  
  2.     private readonly IMapper _mapper;  
  3.     public HomeController (IMapper mapper) {  
  4.         _mapper = mapper;  
  5.     }  
  6.     public IActionResult Index () {  
  7.         Customer customerdetails = new Customer () {  
  8.             CustomerId = 1,  
  9.             CompanyName = "ABC",  
  10.             Address = "Banaglore",  
  11.             Phone = "000",  
  12.             FirstName = "Shwetha",  
  13.             MiddleName = "Amit",  
  14.             LastName = "Naik",  
  15.             City = "Bangalore",  
  16.             Country = "India",  
  17.             Pincode = "560091"  
  18.         };  
  19.         var customerModel = _mapper.Map<CustomerModel> (customerdetails);  
  20.         var fullname = customerModel.FullName;  
  21.         var address = customerModel.City;  
  22.         return View ();  
  23.     }  
  24. }  

Conclusion

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