AutoMapper is a library for object mapping that simplifies manual mapping. For example you need to map an input DTO to another model. To do this, you must write code for all the required properties. For example, you should do the following:

static void Main(string[] args)
{
    UserDto userDto = new UserDto
    {
        Id = 1,
        Age = 21,
        FullName = "Farhad Zamani"
    };
    User user = new User
    {
        Age = userDto.Age,
        FullName = userDto.FullName,
        Id = userDto.Id
    };
    Console.WriteLine($"FullName : {user.FullName} \r\nAge : {user.Age}");
}

Well, if the number of properties is large, you actually have to write more code, and it is a tedious task. But with AutoMapper you can automate the mapping operation. To use AutoMapper, first install the AutoMapper library. You can type the following command in the package manager console and download and install version 9 of AutoMapper.

Install-Package AutoMapper -Version 9.0.0

Next we need to configure AutoMapper. In AutoMapper you have to introduce the classes that are to be mapped together to AutoMapper. For the example above, we need to introduce the User and UserDto classes to AutoMapper. In the InitializeMapper method, we first write the classes that are to be mapped together, and then we return an example of the IMapper object to perform the mapping operation.

private static IMapper InitializeMapper()
{
    var configuration = new MapperConfiguration(cfg =>
    {
        cfg.CreateMap<UserDto, User>();
    });
    return configuration.CreateMapper();
}
static void Main(string[] args)
{
    IMapper mapper = InitializeMapper();
    UserDto userDto = new UserDto
    {
        Id = 1,
        Age = 21,
        FullName = "Farhad Zamani"
    };
    User user = mapper.Map<User>(userDto);
    Console.WriteLine($"FullName : {user.FullName} \r\nAge : {user.Age}");
}

The CreateMap method accepts two generic types, the first of which is the class on which the map operation is to be performed, and copies the required data from the first type to the second type. In this example, if you want to map User data in UserDto, it gives the following error because in the InitializeMapper method, you specified that only UserDto data should be in User.

Unhandled exception. AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

But if you want to do the mapping operation from both sides, you can do the following:

var configuration = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<User, UserDto>();
    cfg.CreateMap<UserDto, User>();
});
// or
var configuration = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<User, UserDto>().ReverseMap();
});

You can use one of the above codes for two-way mapping. The ReverseMap method allows you to two-way mapping. But if the number of your models increases, it is also tedious to specify for each model which other model to map. In the following, we will automate the operation of introducing the required models for the map.

Powered by Froala Editor

Comments