To begin this article, let’s define AutoMapper. AutoMapper is an object-object mapper which solves the problem of manually mapping each property of a class to the same properties of another class. Automapper is a convention-based class mapping tool, meaning it relies on naming conventions of parameters.

I have actually known AutoMapper for years now. I used it in a couple of previous projects and I recently decided to pick it up again.

Ideally, if we want to assign one object property to another object property (e.g, business object to data transfer object), we follow a long process of mapping each property of these two different objects.

AddContact Method

In the code block above, we are trying to add contacts to the database using Entity Framework by mapping each object one after the other to the corresponding object in the other class. Imagine that the contact class has 30 properties, we would have to map them one after the other 30 times. Instead of doing that, we can use AutoMapper.

To use AutoMapper, you can run this command on npm:

Install-Package AutoMapper

When using AutoMapper, a source and destination object is to be specified. For example, if you are reading from your database, your source would be your database object while your destination would be the business object receiving the data.

  1. Initialize the mapper with the source and destination objects.
Mapper.Initialize(n=> n.CreateMap<SourceDto,DestinationDto>());

2. Pass the source object to the map. The new object is returned.

Output=Mapper.Map<DestinationDto>(SourceData);

In the example below, I have the Contact object and the ContactDto object. I want to map the values in the ContactDto to the Contact object.

Viola! It is done. See the resulting output from the map below.

I am going to save contacts to the database using Entity Framework. Now, I will rewrite the AddContact method I have (in the first image above) to use AutoMapper.

AddContact Method with AutoMapper

Now I have a simplified method that maps all my values for me neatly. Cool right?

So let’s say you read a lot of records from the database and you want to map them to your business object, you can just pass the list directly to the map instead of looping through each record.

Using AutoMapper is really a scenario based thing. You ask yourself, how much code do I have to write? How many entities need to be mapped? The more lines of code, the more you will benefit from this tool. If you are using Dapper, it would be redundant using AutoMapper as Dapper does mapping internally.

AutoMapper is really interesting to use as it helps optimize our code and the performance of the application. It also helps write less repetitive mapping code.

One thing to note when using AutoMapper is that the property names of the source and destination object have to be the same, as it is a convention-based mapping tool.

To know more about AutoMapper, you can visit https://automapper.org. Let me know what you think about it in the comment section.

.Net | Angular | React.js | Typescript

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store