In every application, there will be a need to map the object of one type to an object of another type. Automapper .NET Core is used to map these objects of dissimilar types without the need to write boring code to map each data member. Automapper in ASP.NET core comes in handy when mapping objects between Model & DTO.
Table of Contents
What is automapper & when to use it?
Automapper in ASP.NET Core is a popular small library for an object to object mapping used to map objects of different classes.
Let’s say you have two different classes EmployeeDTO & EmployeeModel with the same data members and you need to copy data from one object to another. Now you cannot set an instance of one object to another object as though data members match but objects are of different classes. You will have to use the traditional approach i.e. copy data from one object to another field by field.
To simplify this job of mapping data from one object to objects AutoMapper .NET Core can be used. Automapper in ASP.NET Core is an open-source library and It maps the properties of two different objects.
Implement automapper in ASP.NET Core
Here is a short & quick video on how to implement automapper in ASP.NET Core
Setup project for demonstration
Create new ASP.NET Core API Project
For the implementation of Automapper in ASP.NET Core, I have created a default ASP.NET Core 3.1 API Project using Visual Studio 2019
Install automapper NuGet package
Install package AutoMapper.Extensions.Microsft.DependencyInjection & this package depends on the AutoMapper package which will also get installed with this package.
Create sample EmployeeDTO & EmployeeModel classes
public class EmployeeDTO { public long Id { get; set; } public string Name { get; set; } public int Age { get; set; } public double Salary { get; set; } }
public class EmployeeModel { public long Id { get; set; } public string Name { get; set; } public int Age { get; set; } public double Salary { get; set; } }
Configure automapper in startup.cs
public void ConfigureServices(IServiceCollection services) { services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); } //Remaining code has been removed
Create an automapper mapping profile as EmployeeProfile.cs
Creating Mapping profiles is a better way to organize your object mappings. This profile tells automapper about which object links to which another object, which property of one object links to which property of another object and even let’s configure conditional mappings.
Create a mapping profile class that inherits from AutoMapper.Profile and add the configuration in the constructor of that mapping profile class
public class EmployeeProfile : Profile { public EmployeeProfile() { CreateMap<EmployeeDTO, EmployeeModel>(); } }
Do note that with the above CreateMap you can map from EmployeeDTO to EmployeeModel but reverse mapping will not be supported. Reverse Maps will have to be used for bidirectional mapping.
Using employee mapping in Employee controller
Create an Employee controller as shown below. Automapper service will have to be injected into the controller using constructor dependency injection (Refer here for more details on Dependency Injection). Here Employee controller’s post method takes an object of type EmployeeDTO and using automapper maps it to EmployeeModel & returns the same as JSON.
[Route("api/[controller]")] [ApiController] public class EmployeeController : ControllerBase { private readonly IMapper _mapper; public EmployeeController(IMapper mapper) { _mapper = mapper; } // POST api/<EmployeeController> [HttpPost] public IActionResult Post([FromBody] EmployeeDTO _employeeDTO) { var employeeModel = _mapper.Map<EmployeeModel>(_employeeDTO); return Ok(employeeModel); } }
After executing the above project and calling the Employee controller post-action using postman we got the following result
Mapping objects having different property names
To demonstrate how to map objects using automapper having different property names. We have modified the class EmployeeModel property name for Name to FullName.
public class EmployeeDTO { public long Id { get; set; } public string Name { get; set; } public int Age { get; set; } public double Salary { get; set; } }
public class EmployeeModel { public long Id { get; set; } public string FullName { get; set; } public int Age { get; set; } public double Salary { get; set; } }
Below is the code for the Employee Mapping Profile class in which we have added a mapping for properties having different names i..e. EmployeeDTO=>Name to EmployeeModel=>FullName. ere we have used method ForMember to specify the mapping
public class EmployeeProfile : Profile { public EmployeeProfile() { CreateMap<EmployeeDTO, EmployeeModel>() .ForMember(empmodel => empmodel.FullName, empdto => empdto.MapFrom(empdto => empdto.Name)); } }
After executing the above project and calling the Employee controller post-action using postman we got the following result.
Mapping objects with conditional mapping
Here is the how-to implement conditional mapping for objects in the Employee Mapping Profile class
public class EmployeeProfile : Profile { public EmployeeProfile() { CreateMap<EmployeeDTO, EmployeeModel>() .ForMember(empmodel => empmodel.FullName, empdto => empdto.MapFrom(empdto => empdto.Name)) .ForMember(empmodel => empmodel.Salary, empdto => empdto.MapFrom(empdto => empdto.Age > 55 ? empdto.Salary * 10 : empdto.Salary)); } }
Mapping objects with Null Subsitution
This allows you to set a default or alternate value for the destination data member in case the source data member is null
public EmployeeProfile() { CreateMap<EmployeeDTO, EmployeeModel>() .ForMember(empmodel => empmodel.Telephone, empdto => empdto.NullSubstitute("Not Available")); }
Mapping objects with Reverse Map
Do note that with CreateMap you can map from source (EmployeeDTO) to destination (EmployeeModel) but reverse mapping will not be supported. Reverse Maps will have to be used for bidirectional mapping.
public class EmployeeProfile : Profile { public EmployeeProfile() { CreateMap<EmployeeDTO, EmployeeModel>() .ForMember(empmodel => empmodel.FullName, empdto => empdto.MapFrom(empdto => empdto.Name)) .ForMember(empmodel => empmodel.Salary, empdto => empdto.MapFrom(empdto => empdto.Age > 55 ? empdto.Salary * 10 : empdto.Salary)) .ReverseMap(); } }
Mapping Objects of Complex Types
EmployeeDTO & EmployeeModel classes have been modified to add a new property which is of a type of custom class as shown below
public class TelephoneNumberDTO { //1 - Home, 2 - Office, 3 - Mobile public int PhoneType { get; set; } public string PhoneNumber { get; set; } }
public class TelephoneNumberModel { //1 - Home, 2 - Office, 3 - Mobile public int PhoneType { get; set; } public string PhoneNumber { get; set; } }
public class EmployeeDTO { public long Id { get; set; } public string Name { get; set; } public int Age { get; set; } public double Salary { get; set; } public TelephoneNumberDTO Telephone { get; set; } }
public class EmployeeModel { public long Id { get; set; } public string FullName { get; set; } public int Age { get; set; } public double Salary { get; set; } public TelephoneNumberModel Telephone { get; set; } }
Here are the how-to map complex objects in the Employee Mapping Profile class
public class EmployeeProfile : Profile { public EmployeeProfile() { CreateMap<EmployeeDTO, EmployeeModel>() .ForMember(empmodel => empmodel.FullName, empdto => empdto.MapFrom(empdto => empdto.Name)) .ForMember(empmodel => empmodel.Salary, empdto => empdto.MapFrom(empdto => empdto.Age > 55 ? empdto.Salary * 10 : empdto.Salary)) .ReverseMap(); CreateMap<TelephoneNumberDTO, TelephoneNumberModel>() .ReverseMap(); } }
Advantages of automapper in ASP.NET Core
- No need to write boring code so saves time
EmployeeDTO.Name = EmployeeModel.Name EmployeeDTO.Id = EmployeeModel.Id
- Handles conditional mapping
- Handles complex objects
Summary
We learned about how to use Automapper in ASP.NET Core to map objects of dissimilar types without the need to write boring code to map each data member.
We used automapper in ASP.NET Core to map objects of dissimilar types. It was easy to map objects having the same property names with minimal coding. It was even possible to map properties with different names but it required some additional mapping configuration to map properties with different names within classes.
You can also check my other article on Top 12 ASP.NET Core libraries for developers – https://procodeguide.com/programming/top-12-essential-asp-net-core-libraries
Complete source code for Automapper in ASP.NET Core is available on my GitHub for download
https://github.com/procodeguide/Automapper.Sample
What is Automapper?
Automapper in ASP.NET Core is a popular small library for an object to object mapping used to map objects of different classes.
When to use Automapper?
Use it simply for jobs like when you have two different classes EmployeeDTO & EmployeeModel with the same data members and you need to copy data from one object to another
What are the advantages of Automapper?
With Automapper in ASP.NET Core there is no need to write boring code so saves time, handles complex objects, conditional mapping, etc.