Do i need to create automapper createmap both ways?
Asked Answered
T

4

55

This might be a stupid question! (n00b to AutoMapper and time-short!)

I want to use AutoMapper to map from EF4 entities to ViewModel classes.

1) If I call

CreateMap<ModelClass, ViewModelClass>()

then do I also need to call

CreateMap<ViewModelClass, ModelClass>()

to perform the reverse?

2) If two classes have the same property names, then do I need a CreateMap statement at all, or is this just for "specific/custom" mappings?

Triarchy answered 31/5, 2011 at 8:44 Comment(2)
Use CreateMap<in,out>().ReverseMap() with all the chained trimmings will save you some keystrokes -- see #13490956Spital
One thing to note is that using ReverseMap with AssertConfigurationIsValid, is that the reverse side will not be validated. I have begun to prefer explicit maps for both sides to ensure that properties added to one side or the other don't get missed in validation.Schaffer
M
19

In AutoMapper you have a Source type and a Destination type. So you will be able to map between this Source type and Destination type only if you have a corresponding CreateMap. So to answer your questions:

  1. You don't need to define the reverse mapping. You have to do it only if you intend to map back.
  2. Yes, you need to call CreateMap to indicate that those types are mappable otherwise an exception will be thrown when you call Map<TSource, TDest> telling you that a mapping doesn't exist between the source and destination type.
Musket answered 31/5, 2011 at 8:47 Comment(3)
I know this is not the place to ask questions. Usually I'm using AutoMapper to initiate an Entity object with DTO and DTO is the only model I use in client application. My question is about reading from Database, In that time I've a collection of Entity model which should initiate DTO model. Do I need to create another reverse mapping ?Resort
@saber yes. I mean that's what I am doing unless someone else proofs the reverse.Milliemillieme
The question still remains, WHY do we have to call CreateMap? Forcing me to manually designate mapping isn't being true to the whole "auto" part of things...Lowman
J
139

For the info of the people who stumble upon this question. There appears to be now a built-in way to achieve a reverse mapping by adding a .ReverseMap() call at the end of your CreateMap() configuration chain.

Jorum answered 26/7, 2012 at 16:27 Comment(2)
Apparently it was included a couple months after the first answerSpital
.ReverseMap() doesn't work for .ForMember(dest => dest.prop, opt => optMapFrom(src => src.prop))Pavo
M
19

In AutoMapper you have a Source type and a Destination type. So you will be able to map between this Source type and Destination type only if you have a corresponding CreateMap. So to answer your questions:

  1. You don't need to define the reverse mapping. You have to do it only if you intend to map back.
  2. Yes, you need to call CreateMap to indicate that those types are mappable otherwise an exception will be thrown when you call Map<TSource, TDest> telling you that a mapping doesn't exist between the source and destination type.
Musket answered 31/5, 2011 at 8:47 Comment(3)
I know this is not the place to ask questions. Usually I'm using AutoMapper to initiate an Entity object with DTO and DTO is the only model I use in client application. My question is about reading from Database, In that time I've a collection of Entity model which should initiate DTO model. Do I need to create another reverse mapping ?Resort
@saber yes. I mean that's what I am doing unless someone else proofs the reverse.Milliemillieme
The question still remains, WHY do we have to call CreateMap? Forcing me to manually designate mapping isn't being true to the whole "auto" part of things...Lowman
R
11

I've used an extension method do mapping both ways

    public static IMappingExpression<TDestination, TSource> BothWays<TSource, TDestination>
        (this IMappingExpression<TSource, TDestination> mappingExpression)
    {
        return Mapper.CreateMap<TDestination, TSource>();
    }

usage:

 CreateMap<Source, Dest>().BothWays();
Ruffi answered 29/2, 2012 at 18:36 Comment(3)
I think this extension already exists as .ReverseMap() i.e. CreateMap<In,Out>().ReverseMap() as seen here...and as I just noticed on the answer belowSpital
I think ReverseMap was added after Feb 2012.Ruffi
maybe, I'm just going by the commit timestamp of Nov 2011Spital
R
1
  1. Yes, or you can call CreateMap<ModelClass, ViewModelClass>().ReverseMap().
  2. If two classes have same Member(Property,Field,GetMethod()), you needn't call CreateMap<TSrc,TDest>. Actually, if every member in TDest are all exist in TSrc, you needn't call CreateMap<TSrc,TDest>. The following code works.
class Person
{
    public string Name { get; set; }
    public int Age { get; set; }  
}
class Person2
{
   public string Name { get; set; }
   public int? Age { get; set; }
   public DateTime BirthTime { get; set; }
}
public class NormalProfile : Profile
{
    public NormalProfile()
    {
       //CreateMap<Person2, Person>();//
    }
}
   
var cfg = new MapperConfiguration(c => 
{ 
    c.AddProfile<NormalProfile>();
});
//cfg.AssertConfigurationIsValid();
var mapper = cfg.CreateMapper();
var s3 = mapper.Map<Person>(new Person2 { Name = "Person2" });
Rebba answered 19/11, 2020 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.