How to specify mapping rule when names of properties differ

C#Automapper

C# Problem Overview


I am a newbie to the Automapper framework. I have a domain class and a DTO class as follows:

public class Employee
{
   public long Id {get;set;}
   public string Name {get;set;}
   public string Phone {get;set;}
   public string Fax {get;set;}
   public DateTime DateOfBirth {get;set;}
}

public class EmployeeDto
{
   public long Id {get;set;}
   public string FullName {get;set;}
   public DateTime DateOfBirth {get;set;}
}

Note: The name of property "Name" of Employee class is not the same as that of property "FullName" of EmployeeDto class.

And here's the code to map the Employee object to EmployeeDto:

Mapper.CreateMap<Employee, EmployeeDto>(); // code line (***)
EmployeeDto dto = Mapper.Map<Employee, EmployeeDto>(employee); 

My question is: If I want to map Employee (source class) to EmployeeDto (destination class), how can I specify the mapping rule? In other words, how should I do more with code line (***) above?

C# Solutions


Solution 1 - C#

Never mind, I myself found a solution:

Mapper.CreateMap<Employee, EmployeeDto>()
  	.ForMember(dest => dest.FullName, opt => opt.MapFrom(src => src.Name));

Solution 2 - C#

Just to roll the comments above into an updated approach using Automapper 8.1+...

var mapConfig = new MapperConfiguration(
   cfg => cfg.CreateMap<Employee, EmployeeDto>()
      .ForMember(dest => dest.FullName, opt => opt.MapFrom(src => src.Name))
);

Then you would build the mapper using the mapConfig:

var mapper = mapConfig.CreateMapper();

Solution 3 - C#

We can also specify on Class attributes for mapping

From https://docs.automapper.org/en/stable/Conventions.html#attribute-support

> ##Attribute Support > > AddMemberConfiguration().AddName<SourceToDestinationNameMapperAttributesMember>(); > * Currently is always on > > Looks for instances of SourceToDestinationMapperAttribute for > Properties/Fields and calls user defined isMatch function to find > member matches. > > MapToAttribute is one of them which will match the property based on > name provided. > > public class Foo > { > [MapTo("SourceOfBar")] > public int Bar { get; set; } > }

Solution 4 - C#

Considering that we have two classes

public class LookupDetailsBO
    {
        public int ID { get; set; }

        public string Description { get; set; }

    }

and the other class is

public class MaterialBO
    {
        [MapTo(nameof(LookupDetailsBO.ID))]
        public int MaterialId { get; set; }

        [MapTo(nameof(LookupDetailsBO.Description))]
        public string MaterialName { get; set; }

        public int LanguageId { get; set; }
    }

In this way you know typically to which property you follow . and you make sure of the naming convention , so if you have changed the propery name in the source . The MapTo() will prompt an error

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
QuestionThomas.BenzView Question on Stackoverflow
Solution 1 - C#Thomas.BenzView Answer on Stackoverflow
Solution 2 - C#ebol2000View Answer on Stackoverflow
Solution 3 - C#Pranay DevOpsView Answer on Stackoverflow
Solution 4 - C#Shiva BrahmaView Answer on Stackoverflow