Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any advantage to using Mapper vs Implicit Operators?

Mapper Automap:

Mapper.CreateMap<ObjectType1, ObjectType2>()
    .ForMember(o1 => o1.PropName, mapper => mapper.MapFrom(o2 => o2.Prop2Name));

Mapper.Map(object1, object2);

Implicit operator:

public static implicit operator Object1(Object2 o2)
{ 
    Object1 o1 = new Object2(); 
    //Mapping code here...
    return o1;
}
like image 770
Boanerge Avatar asked Mar 10 '13 18:03

Boanerge


People also ask

Why do we use Mapper?

AutoMapper is used whenever there are many data properties for objects, and we need to map them between the object of source class to the object of destination class, Along with the knowledge of data structure and algorithms, a developer is required to have excellent development skills as well.

What is implicit operator C#?

The Implicit Operator According to MSDN, an implicit keyword is used to declare an implicit user-defined type conversion operator. In other words, this gives the power to your C# class, which can accepts any reasonably convertible data type without type casting.


1 Answers

There's no reason you couldn't use both together, by calling Mapper.Map from the implicit operator.

Using AutoMapper allows you to rely on automatically generated mapping code, so that you don't have to use ForMember to map each member individually.

like image 148
David Pfeffer Avatar answered Oct 28 '22 00:10

David Pfeffer