Getting Started With AutoMapper

Automapper is an utility library designed for object-to-object mapping. Let's take the case of mapping columns in a data table to its equivalent business entity, we will have to manually write code for mapping each of these columns and assume if there are a lot of entities, it's quite boring as well as creates redundant code. Some bright minds in the industry took note of it and set to solve the problem by automating it and thus AutoMapper was born. 

First of all you need to install AutoMapper package, which can be done from the Package Manager Console using the following statement

PM> Install-Package AutoMapper

Let's create a simple program to do a basic mapping procedure.

  1. Create a map which indicates the mapping flow for automapper.

var config = new MapperConfiguration(cfg => {
cfg.CreateMap<Source type, Destination type>();

IMapper mapper = config.CreateMapper(); 

  Suppose if you want to convert from class A to class B, then your mapping statement will be like 

var config = new MapperConfiguration(cfg => {
cfg.CreateMap<A, B>();

IMapper mapper = config.CreateMapper();

2. The actual mapping happens when we call the Mapper.Map function

var destinationObj =  mapper.Map<Source type>(Source instance);

Eg: var bObj = mapper.Map<A>(aObj);

So in this example, I have created two classes, one is called Foo and other is a replica of Foo class called FooCopy and I am trying to map the contents of the properties in Foo class to FooCopy class.

For that I have specified the mapping rule that needs to be followed by AutoMapper in line #19. After that I have initialized the properties for Foo instance which will be our source data. And the magic is happening in line #23 where the actual mapping for data from foo object to fooCopy object.

AutoMapper is based on convention mapping algorithms to map the source to destination. Here it will analyse the two classes and if it finds a match for property which is have same type and name in both instances then the value will be copied.

Now let's modify the above example to add more properties in the source entity but the destination entity is not modified. 

If you look at the output section, you can see that there are no compilation error and all the info are correctly printed. So here what happened is that AutoMapper mapped items which are existing in both places and all other items are ignored. Again one thing to note is that even though we have made changes to entities, there is no need to do any thing with the mapping configuration. This applies to when we rename the properties in the entities also. When we rename the properties, the above rule is applied unless we have provided custom rules for mapping

In the coming posts in this series how can we make mappings with complex objects, ignoring properties from getting mapped etc.

Note: All these examples are based on 4.2.1 version of AutoMapper and there's some significant changes in library by removing the static API. Please refer here for futher details

Till then, Happy Coding !!!

No Comments

Add a Comment