Introduction
In this article, we are going to explore how to use AutoMapper in the .NET Web API Application with examples.
What is AutoMapper?
AutoMapper is an object-to-object mapping library that helps transform one object type into another. It's mainly useful for mapping complex objects, such as Data Transfer Objects (DTOs), to domain models and vice versa. It reduces the need for manual property mapping and simplifies the code.
Why Use AutoMapper?
- Reduces Code: Instead of writing repetitive code to map properties from one object to another, AutoMapper handles property mapping between the objects.
- Improves Maintainability: AutoMapper centralizes the mapping definitions. When updates or additions are needed, adjust the configuration, making the code easier to manage.
- Increases Productivity: AutoMapper speeds up development by saving time when writing and testing mapping code.
- Standardizes Mapping: AutoMapper ensures consistent and reliable mappings, reducing errors and improving data integrity.
How to use AutoMapper?
Step 1. Install AutoMapper
Install AutoMapper and AutoMapper.Extensions.Microsoft.DependencyInjection using NuGet Package Manager or the Package Manager Console.
Install-Package AutoMapper
Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection
Step 2. Create Models and DTOs
Define the models (Person) and corresponding DTOs (PersonDTO).
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
public class PersonDTO
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
Step 3. Create an AutoMapper Profile:
Create a MappingProfile class inheriting from Profile to configure the mappings between models and DTOs using CreateMap.
using AutoMapper;
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Person, PersonDTO>();
CreateMap<PersonDTO, Person>();
}
}
Step 4. Register AutoMapper in Program.cs:
Register AutoMapper using the ConfigureServices() method to enable dependency injection and automatic mapping configuration.
using AutoMapper;
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddAutoMapper(typeof(Program)); // Register AutoMapper
}
Step 5. Use AutoMapper in a Controller
The PersonController uses AutoMapper to map between Person and PersonDTO objects in the CreatePerson() and GetPerson() action methods. The CreatePerson() method maps the PersonDTO to a Person entity for saving to the database, while the GetPerson() method maps the Person entity to a PersonDTO for returning as a response.
[ApiController]
[Route("api/[controller]")]
public class PersonController : ControllerBase
{
private readonly IMapper _mapper;
public PersonController(IMapper mapper)
{
_mapper = mapper;
}
[HttpPost]
public IActionResult CreatePerson([FromBody] PersonDTO personDto)
{
var person = _mapper.Map<Person>(personDto);
// Save the person entity to the database
return Ok(person);
}
[HttpGet("{id}")]
public IActionResult GetPerson(int id)
{
// Retrieve the person entity from the database
var person = new Person { Id = id, FirstName = "Test", LastName = "Test1", Age = 30 };
var personDto = _mapper.Map<PersonDTO>(person);
return Ok(personDto);
}
}
Mapping with Custom Logic
Sometimes, we need to apply custom logic during the mapping process.
example, we have a scenario where we need to combine the FirstName and LastName properties into a single FullName property in the destination object
Define Models and DTOs
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
}
public class UserDTO
{
public string FullName { get; set; }
public int Age { get; set; }
}
Create a Mapping Profile
- Create a UserProfile class that inherits from Profile.
- Use the CreateMap method to define the mapping between User and UserDTO.
- Use ForMember to apply custom logic:
- Combine FirstName and LastName into FullName.
- Compute Age using the CalculateAge method.
using AutoMapper;
public class UserProfile : Profile
{
public UserProfile()
{
CreateMap<User, UserDTO>()
.ForMember(dest => dest.FullName, opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}"))
.ForMember(dest => dest.Age, opt => opt.MapFrom(src => CalculateAge(src.DateOfBirth)));
}
private int CalculateAge(DateTime dateOfBirth)
{
var today = DateTime.Today;
var age = today.Year - dateOfBirth.Year;
if (dateOfBirth.Date > today.AddYears(-age))
{
age--;
}
return age;
}
}
Use AutoMapper in a Controller
Use AutoMapper to map between User and UserDTO in a controller.
[HttpPost]
public IActionResult CreateUser([FromBody] User user)
{
var userDto = _mapper.Map<UserDto>(user);
return Ok(userDto);
}
Sample Response
CreateUser Request (Input: User)
{
"FirstName": "John",
"LastName": "Doe",
"DateOfBirth": "1990-01-01"
}
CreateUser Response (Output: UserDto)
{
"FullName": "John Doe",
"Age": 34
}
Flattening Complex Objects
Flattening complex objects in AutoMapper involves transforming nested objects into a simpler, flat structure. This is useful when you want to combine properties from nested objects into a single object, such as when preparing data for a UI or for API responses.
Define Models and DTOs

Comments
Join the conversation! Your thoughts help the community grow.