When mapping objects in your application, use AutoMapper to reduce the need to create boring lines of code.
AutoMapper is a popular object-to-object mapping library that may be used to map objects of different types.
For example, you may need to map your application’s DTOs (Data Transfer Objects) to model objects. AutoMapper saves you the time and effort of manually mapping one or more attributes of incompatible types.
To start using AutoMapper, first, create a project in Visual Studio and then install AutoMapper. In the NuGet Package Manager Console window, type the following command to install AutoMapper :
PM> Install-Package AutoMapper
Use AutoMapper to create mappings
An input object of one type is converted into an output object of another kind using an object-to-object mapper, such as AutoMapper. Consider about the following two classes.
public class AuthorModel { public int Id { get; set; } public string FirstName { get;set; } public string LastName { get; set; } public string Address { get; set; } } public class AuthorDTO { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } }
The code snippet that follows explains how to create a map between AuthorModel and AuthorDTO.
var config = new MapperConfiguration(cfg => { cfg.CreateMap<AuthorModel, AuthorDTO>(); });
The following piece of code demonstrates how simple it is to perform out the mapping between the types.
IMapper iMapper = config.CreateMapper(); var source = new AuthorModel(); var destination = iMapper.Map<AuthorModel, AuthorDTO>(source);
An AutoMapper example
Let’s start using some data. Refer to the code below, which after performing the mapping displays the property values in the destination object after storing some data in the source object.
var config = new MapperConfiguration(cfg => { cfg.CreateMap<AuthorModel, AuthorDTO>(); }); IMapper iMapper = config.CreateMapper(); var source = new AuthorModel(); source.Id = 1; source.FirstName = "Joydip"; source.LastName = "Kanjilal"; source.Address = "India"; var destination = iMapper.Map<AuthorModel, AuthorDTO>(source); Console.WriteLine("Author Name: "+ destination.FirstName + " " + destination.LastName);
When you execute the above piece of code, the author name that is stored inside the object will be shown. As a result of your successful usage of AutoMapper to map the objects, the values of the destination FirstName and destination LastName properties will be the same as the source object.
Remember that any collection of classes can be mapped by AutoMapper. But AutoMapper follows certain rules, one of which is that the names of the properties being mapped should be the same. You must specify AutoMapper and how the properties should be mapped if the property names are unique. The example that follows shows how to map the properties Contact and contact details, assuming that this is what we want to achieve.
var config = new MapperConfiguration(cfg => { cfg.CreateMap<AuthorModel, AuthorDTO>() .ForMember(destination => destination.ContactDetails, opts => opts.MapFrom(source => source.Contact)); });
Make sure that the destination object was created using the following statement.
var destination = iMapper.Map<AuthorModel, AuthorDTO>(source);
Use the following statement in its instead if the target object already exists.
iMapper.Map(sourceObject, destinationObject);
In essence, the above code snippet can be used to map two existing items.
Using projections in AutoMapper
Projections are well supported by AutoMapper. Projections are used to translate values from a source to a destination where the structure is different from the source.
Now let’s check a projection. Consider the following class, for example.
public class Address { public string City { get; set; } public string State { get; set; } public string Country { get; set; } }
Let’s use the Address class to store the authors’ addresses in our AuthorModel class. The updated AuthorModel class would look like this.
public class AuthorModel { public int Id { get; set; } public string FirstName { get;set; } public string LastName { get; set; } public Address Address { get; set; } }
The modified AuthorDTO class is shown below.
public class AuthorDTO { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string City { get; set; } public string State { get; set; } public string Country { get; set; } }
Let’s say that the AuthorDTO and AuthorModel classes need to be mapped. This is shown by the following line of code.
var config = new MapperConfiguration(cfg => { cfg.CreateMap<AuthorDTO, AuthorModel>() .ForMember(destination => destination.Address, map => map.MapFrom( source => new Address { City = source .City, State = source .State, Country = source.Country }));