is a popular object-to-object mapping library that can be used to map objects belonging to dissimilar types. As an example, you might need to map the DTOs (Data Transfer Objects) in your application to the model objects. AutoMapper saves you the tedious effort of having to manually map one or more properties of such incompatible types.

To start working with AutoMapper, you should create a project in Visual Studio and then install AutoMapper. You can install AutoMapper from NuGet using the following command in the NuGet Package Manager Console window:

PM> Install-Package AutoMapper

Create mappings using AutoMapper

An object-to-object mapper such as AutoMapper converts an input object of one type into an output object of another type. Consider 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 following code snippet shows how you can create a map between these two types, AuthorModel and AuthorDTO.

var config = new MapperConfiguration(cfg => {
                cfg.CreateMap<AuthorModel, AuthorDTO>();
            });

Then to perform the mapping between the types is as simple as the following piece of code shows.

IMapper iMapper = config.CreateMapper();
var source = new AuthorModel();
var destination = iMapper.Map<AuthorModel, AuthorDTO>(source);

An AutoMapper example 

Let’s now work with some data. Refer to the following piece of code that stores some data into the source object and then displays the property values in the destination object after the mapping is done.

.