Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages of using automapper?

I have to do some object to object mapping between domain classes used in a C# project and classes which are sent to flash clients.

My first choice was Automapper. But I've had some issues with it (nested properties, no parameterless constructor defined). It turns out that it is not so easy to map a really complex type with Automapper.

Why not implement methods like the following?

  ClassA GetClassAByClassB(ClassB pObj)

   {  
     ClassA objA = new ClassA();  
     objA.Prop1 = pObj.Prop1;  
     objA.NestedType.Prop2 = pObj.Prop2;  
     //....Some more.....  
     return objA;  
   }  

It has exactly the same level of flexibility as mapping done using Automapper. You still have to provide which property from the source object is copied into what property in destinations object. You just do this using = instead of a lambda expression.

But if you change something in your domain classes you have to change this "mapping" part anyway. What, then, is the main advantage to using Automapper over literal mappings?

like image 298
Katalonis Avatar asked Oct 22 '10 11:10

Katalonis


People also ask

What is AutoMapper good for?

AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.

When should you use AutoMapper?

If you need to go from one similar object to another similar object and don't want to manually set the properties every time you use AutoMapper.

What is the use of AutoMapper in MVC?

AutoMapper is an external component that helps in creating a mapping between a source and destination model types. Once the mapping is created then the source model object can be converted to a destination model object with ease and with less cluttered code.

Does AutoMapper affect performance?

AutoMapper affects the performance of your application. It takes time to load during project startup and when you map between objects. But in most cases, this should not be an issue because most applications don't have objects with thousands of properties.


1 Answers

The one benefit that AutoMapper claims is mapping-by-convention. Here is a quote from "AutoMapper Introduction and Samples"

Herein lies the beauty of AutoMapper. When your classes align themselves conventionally, your mapping configuration can be as simple

This comes with a cost. Renaming or changing either the target or the source property name will break the mapping and introduce a run-time bug.

If you do not use mapping-by-convention, AutoMapper loses its advantage. In this case, I'd rather write a factory function like this one.

public static ClassA MapToClassA(this ClassB b) => 
    new ClassA()
    {
        propA = b.propA;
        propB = b.propB;
        propC = b.propC;
    }

Then you would construct the destination object like

var classA = classB.MapToClassA();

instead of

var classA = Mapper.Map<ClassB, ClassA>(classB)

Personally, I'd prefer a factory function for its explicitness, readability, and debug friendliness. Good luck with trying to find out, in the second case, how ClassB is mapped to ClassA, whether the mapping profile is loaded, or why there is an exception when the Map<>() function is called, or why some of the properties have been assigned wrong values.

like image 143
Xiaoguo Ge Avatar answered Oct 24 '22 20:10

Xiaoguo Ge