Introduction to AutoMapper for C# (.NET)
In modern C# (.NET) development, the task of converting one object to another often arises. This may be necessary when transferring data between application layers (for example, from a database to a DTO for an API), when mapping a ViewModel to an Entity, or when working with different API versions. Manually writing code for each such conversion is tedious, error-prone, and difficult to maintain. This is where AutoMapper comes to the rescue.
AutoMapper is a popular open-source library for .NET that automates the process of mapping properties from one object to another. It is based on the principle of "convention over configuration." This means that if the names and types of properties in the source and target objects match (or follow certain rules), AutoMapper can perform the mapping without explicitly specifying rules. If the rules differ, you can easily configure them using a flexible API.
The library significantly reduces the amount of boilerplate code, makes the code cleaner, reduces errors associated with manual assignment, and simplifies refactoring. AutoMapper is widely used in projects on ASP.NET Core, WPF, Xamarin, and other .NET applications.
Installing AutoMapper
AutoMapper is distributed via NuGet, the official package manager for .NET. You can install it in several ways:
1. Via the Package Manager Console
Install-Package AutoMapper2. Via .NET CLI
dotnet add package AutoMapper3. Via the Visual Studio Interface (Manage NuGet Packages)
Open the project -> Right-click on the project -> "Manage NuGet Packages" -> search for "AutoMapper" -> install the latest stable version.
For working with ASP.NET Core, it is also recommended to install the AutoMapper.Extensions.Microsoft.DependencyInjection package, which simplifies integration with the DI container:
dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjectionQuick Start: First Mapping
Let's create a simple example. We have a UserEntity class (from the database) and a UserDto class (for sending to the client).
using AutoMapper;
// 1. Source and target classespublic class UserEntity{ public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; }}
public class UserDto{ public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; }}
// 2. Creating configuration and Mappervar config = new MapperConfiguration(cfg =>{ cfg.CreateMap<UserEntity, UserDto>();});
var mapper = config.CreateMapper();
// 3. Usagevar userEntity = new UserEntity{ Id = 1, FirstName = "Ivan", LastName = "Petrov", Email = "ivan@example.com"};
var userDto = mapper.Map<UserDto>(userEntity);
Console.WriteLine(userDto.FirstName); // Outputs: IvanIn this example, we created a map between UserEntity and UserDto. Since the property names match, AutoMapper automatically mapped them. The Map<TDestination>(source) method performs the mapping and returns a new object of the target type.
Main Methods and Functions of AutoMapper
AutoMapper provides a rich API for configuring mapping. Below are 10 key methods and functions you will use most often.
1. CreateMap<TSource, TDestination>()
What it does: Creates a mapping configuration between two types. This is the main method for declaring a map.