How to use AutoMapper to map one object to another?

In a real world application, mapping can occur in many places. Mostly we write in the boundaries between layers, such as between the UI/Domain layers, or Service/Domain layers. There is no harm in handling this mapping manually for all the types but everyone knows that it is boring! In this post, I’ll guide you how to use AutoMapper to map one object to another.

Let us take an example to understand why is there a need of a tool like AutoMapper. Below I’ve define some classes and manual mapping is used to map different members.

To serve a view to the user, we have a BookViewModel type that gets populated from other types Author & Book.

Manual mapping like above seems quite simple and straightforward when there are few members to be mapped. However, if view-model needs more information, there will be a requirement to map more properties from one type to the other.

Will it not be great if we can simplify this boring process by writing a mapping mechanism that just do this as if by magic? There is where AutoMapper comes handy.

What is AutoMapper?

AutoMapper is an object-object mapper. Object-object mapping works by transforming an input object of one type into an output object of a different type. What makes AutoMapper interesting is that it provides some interesting conventions to take the dirty work out of figuring out how to map type A to type B. As long as type B follows AutoMapper’s established convention, almost zero configuration is needed to map two types.
–  AutoMapper

How does AutoMapper work?

AutoMapper works really well when the names of the destination type’s members match up to source type’s members. For example, if you have a source member called “ProductName”, this will automatically be mapped to a destination member with the name “ProductName”.

All the null reference exceptions are ignores when AutoMapper maps source to the target which works out of the box as per its design.

How to add AutoMapper to your project?

Simply add the “AutoMapper” package to your project by using the Nuget Package Manager.

How to create a mapping?

There are few ways to create a mapping for your types using this tool. One of the way is mentioned below.

Use the Mapper static member on AutoMapper to create a map. The type on the left is the source type and the type on the right is the destination type. In the next statement, a book is passed to the Map method instructing the required BookViewModel type to the AutoMapper.

AutoMapper is smart enough to map all the properties correctly which is far more beneficial compared to the manual mapping code above.

But wait, there is a catch. Look at the code as Author in BookViewModel type is a string, while Author in Book type is an instance of type Author. AutoMapper needs our help to perform this mapping, which can be done by:

The above code just says that for Author in destination, use Author.Name from the source.

This is a configuration part that is not something we want to repeat over and over in our code-base. Ideally, we would define this mapping configuration for types only once and AutoMapper should perform its magic at any point in run-time where such a conversion is needed.

Where is the correct place to define mappings?

I have an ASP.NET  MVC application and I’d like this mapping configuration code to run once and at startup so that it is available to the app as soon as possible. Like other configuration files in App_Start folder, it will be a good convention to have a file named AutoMapperConfig.cs with following code:

To register the mappings, add a call to AutoMapperConfig lasix pills online.RegisterMappings() in Application_Start method in Global.asax file.

This way, all the mappings defined in the application will be registered at the startup. Using a separate configuration file for this, help in maintenance of these mappings as everything related to AutoMapper tool can be done at one central place.

How to test these Mappings?

Using a tool to do all this tedious work is good but it is also our job to write tests for such mappings. Like any test, create a test that does two things:

  • Call your bootstrapper class to create all the mappings
  • Call MapperConfiguration.AssertConfigurationIsValid

AutoMapper is really a good tool that can save lots of development time to save us from doing boring stuff.

Siddharth Pandey

Siddharth Pandey is a Software Engineer with thorough hands-on commercial experience & exposure to building enterprise applications using Agile methodologies. Siddharth specializes in building, managing on-premise, cloud based real-time standard, single page web applications (SPAs). He has successfully delivered applications in health-care, finance, insurance, e-commerce sectors for major brands in the UK. Other than programming, he also has experience of managing teams, trainer, actively contributing to the IT community by sharing his knowledge using Stack Overflow, personal website & video tutorials.

You may also like...

Advertisment ad adsense adlogger