繁体   English   中英

自动映射器可以处理某些属性不匹配的情况吗?

[英]Can automapper handle a case where the some of the properties do not match?

假设我有一个目标类和一个源类,它们基本相同。 几乎所有属性都使用自动映射器自动映射。

假设这些类的30个属性中,有两个没有自动映射器可以自动找出的任何直接关联。

有没有办法告诉自动映射器手动连接两个属性?

例如:

class DTOMyObject
{
     public int Test {get; set;}
     public int Test2 {get; set;}
     public int Test3 {get; set;}
     public int Test4 {get; set;}
     public int Test5 {get; set;}
     // Continues for many many more properties.

     public int RandomOtherName {get; set;}
     public int SecondRandomName {get; set;}
}

class ViewMyObject
{
     public int Test {get; set;}
     public int Test2 {get; set;}
     public int Test3 {get; set;}
     public int Test4 {get; set;}
     public int Test5 {get; set;}
     // Continues for many many more properties.

     public int MapsToTheFirstRandomName {get; set;}
     public int ShouldMapToTheRandomNameThatIsSecond {get; set;}
}

由于可以自动映射的属性比例很高,因此我想使用自动映射器。 但是我阅读/观看过的文档和视频并未显示如何处理极端情况。

有没有办法让这些类自动映射? 如果是这样,请提供代码示例?

谢谢

 Mapper.CreateMap<DTOMyObject, ViewMyObject>()
     .ForMember(dest => dest.MapsToTheFirstRandomName, opt => opt.MapFrom(src => src.RandomOtherName))
     .ForMember(dest => dest.ShouldMapToTheRandomNameThatIsSecond , opt => opt.MapFrom(src => src.SecondRandomName));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM