.net Automapper:使用 ReverseMap() 和 ForMember() 进行双向映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13490456/
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-09-03 16:43:54  来源:igfitidea点击:

Automapper: bidirectional mapping with ReverseMap() and ForMember()

.netautomapperbidirectional

提问by toni

I have the case where I want to map an entity to a viewmodel and back. I have to specify the mapping explicitly with ForMember()because their properties do not share the exact same names. Here is a short example of how my classes look like:

我有一种情况,我想将一个实体映射到一个视图模型并返回。我必须明确指定映射,ForMember()因为它们的属性不共享完全相同的名称。这是我的课程的简短示例:

public class PartTwo {
    public int Integer { get; set; }
}

public class PartTwoViewModel {
    public int PartInteger { get; set; }
}

And I want to use them this way:

我想这样使用它们:

Mapper.CreateMap<PartTwo, PartTwoViewModel>()
    .ForMember(dst => dst.PartInteger, opt => opt.MapFrom(src => src.Integer))
    .ReverseMap();

var partTwoViewModel = new PartTwoViewModel() { PartInteger = 42 };
var partTwo = Mapper.Map<PartTwoViewModel, PartTwo>(partTwoViewModel);
Assert.AreEqual(partTwoViewModel.PartInteger, partTwo.Integer);

But it does not match the property PartIntegerto Integer. (Integeris 0.)

不过,这并不属性匹配PartIntegerInteger。(Integer0。)

Is there a way to make this work? (When the properties of both classes have the same names it works.) Do I have to set some kind of option in the method ForMember()?

有没有办法使这项工作?(当两个类的属性具有相同的名称时,它可以工作。)我是否必须在方法中设置某种选项ForMember()

采纳答案by Mightymuke

You could define your configuration like this:

你可以这样定义你的配置:

Mapper.CreateMap<PartTwo, PartTwoViewModel>()
    .ForMember(dst => dst.PartInteger, opt => opt.MapFrom(src => src.Integer));

Mapper.CreateMap<PartTwoViewModel, PartTwo>()
    .ForMember(dst => dst.Integer, opt => opt.MapFrom(src => src.PartInteger));

UPDATE

更新

Here is the commitwhere ReverseMapwas initially implemented. From what I can see in the code, it only creates a simple reverse mapping. For example, in this case it would automatically configure the equivalent of:

这里是提交哪里ReverseMap开始实施。从我在代码中看到的,它只创建了一个简单的反向映射。例如,在这种情况下,它会自动配置以下内容:

Mapper.CreateMap<PartTwoViewModel, PartTwo>();

To get anything more complex, I'm afraid that you're going to have to configure it manually.

要获得更复杂的东西,恐怕您将不得不手动配置它。

回答by Jon Wingfield

ReverseMapreturns an IMappingExpressionthat represents the reversal of the mapping. Once you call, it subsequent calls will be for configuring the reversal of the map.

ReverseMap返回IMappingExpression表示映射反转的 。调用后,后续调用将用于配置地图的反转。

Here's an example:

下面是一个例子:

Mapper.CreateMap<CartItemDto, CartItemModel>()
      .ForMember(dest => dest.ExtendedCost, opt => opt.Ignore())
      .ReverseMap()
          .ForMember(dest => dest.Pricing, opt => opt.Ignore())

This will ignore the Pricingfield in the reverse direction.

这将忽略Pricing相反方向的字段。