C# 属性名称不同时如何指定映射规则

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14777601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:58:09  来源:igfitidea点击:

How to specify mapping rule when names of properties differ

c#automapper

提问by Thomas.Benz

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

我是 Automapper 框架的新手。我有一个域类和一个 DTO 类,如下所示:

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 Employeeclass is not the same as that of property "FullName" of EmployeeDtoclass.

注意:类的属性“ Name”的名称Employee类的属性“ FullName”的名称不同EmployeeDto

And here's the code to map the Employeeobject to EmployeeDto:

这是将Employee对象映射到的代码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?

我的问题是:如果我想将Employee(源类)映射到EmployeeDto(目标类),我该如何指定映射规则?换句话说,我应该如何对上面的代码行(***)做更多的事情?

采纳答案by Thomas.Benz

Never mind, I myself found a solution:

没关系,我自己找到了一个解决方案:

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

回答by ebol2000

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

只是为了使用 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:

然后您将使用 mapConfig 构建映射器:

var mapper = mapConfig.CreateMapper();

回答by Pranay DevOps

We can also specify on Class attributes for mapping

我们还可以指定用于映射的类属性

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

来自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; }
}

属性支持

AddMemberConfiguration().AddName<SourceToDestinationNameMapperAttributesMember>();* 当前始终开启

查找属性/字段的 SourceToDestinationMapperAttribute 实例,并调用用户定义的 isMatch 函数来查找成员匹配项。

MapToAttribute 是其中之一,它将根据提供的名称匹配属性。

public class Foo
{
    [MapTo("SourceOfBar")]
    public int Bar { get; set; }
}