Do i need to create automapper createmap both ways?

asp.net Mvc-3ModelViewmodelAutomapper

asp.net Mvc-3 Problem Overview


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()

then do I also need to call

CreateMap<ViewModelClass, ModelClass>()

to perform the reverse?

  1. 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?

asp.net Mvc-3 Solutions


Solution 1 - asp.net Mvc-3

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.

Solution 2 - asp.net Mvc-3

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.

Solution 3 - asp.net Mvc-3

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();

Solution 4 - asp.net Mvc-3

  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" });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlueChippyView Question on Stackoverflow
Solution 1 - asp.net Mvc-3Ivan ZlatevView Answer on Stackoverflow
Solution 2 - asp.net Mvc-3Darin DimitrovView Answer on Stackoverflow
Solution 3 - asp.net Mvc-3BrianView Answer on Stackoverflow
Solution 4 - asp.net Mvc-3JimView Answer on Stackoverflow