基于目标值的 C# AutoMapper 条件映射

编程入门 行业动态 更新时间:2024-10-25 08:25:15
本文介绍了基于目标值的 C# AutoMapper 条件映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请任何人建议如何使用 AutoMapper 中的条件映射根据现有的 TARGET 属性值从 SOURCE 对象映射 TARGET 对象中的值?

Please can any one advise how to use conditional mapping in AutoMapper to map a value in the TARGET object from a SOURCE object based upon an existing TARGET property value?

所以我的源类是:

public class UserDetails { public String Nickname { get; set; } }

我的目标班级是:

public class ProfileViewModel { public Boolean NicknameIsVisible { get; set; public String Nickname { get; set; } }

仅当目标属性NicknameIsVisible"值已设置为 TRUE 时,我想设置 TARGET 中的Nickname"属性值以匹配 SOURCE 中的Nickname"属性值,否则我想设置 TARGET"昵称"属性值为空字符串.

I want to set the "Nickname" property value in the TARGET to match the "Nickname" property value in the SOURCE only if the target property "NicknameIsVisible" value is already set to TRUE, otherwise I want to set the TARGET "Nickname" property value to an empty string.

我正在尝试这样的事情(不会编译)...

I was trying something like this (which wont compile )...

Mapper.CreateMap<UserDetails, ProfileViewModel>() .ForMember( destination => destination.Nickname, option => option. .MapFrom( source => source.NicknameIsVisible ? source.Nickname : String.Empty) );

但是NicknameIsVisible"不是我的源的属性,而是我的目标的属性.

but "NicknameIsVisible" is not a property of my SOURCE but of my TARGET.

顺便说一句,我的 ProfileViewModel 使用 Owain Wragg 的方法绑定到三个实体(consultingblogs.emc/owainwragg/archive/2010/12/22/automapper-mapping-from-multiple-objects.aspx),它是另一个提供值的实体到NicknameIsVisible"属性.

BTW, My ProfileViewModel is bound to three entities using Owain Wragg's method (consultingblogs.emc/owainwragg/archive/2010/12/22/automapper-mapping-from-multiple-objects.aspx) and it is another entity that gives the value to the "NicknameIsVisible" property.

任何人都可以提出正确的语法来解决这个问题吗?

Please can anyone suggest the right syntax to use for this problem?

推荐答案

使用 devduder 的例子我现在有以下代码可以编译:

Using devduder's example I now have the following code which compiles:

.ForMember( destination => destination.Nickname, option => { option.Condition(resolutionContext => (resolutionContext.InstanceCache.First().Value as ProfileViewModel).NicknameIsVisible); option.MapFrom(source => source.Nickname); } );

但是,尽管它编译并运行它并没有用任何东西填充目标.昵称.

我必须更改映射的顺序,以便首选项对象(它具有NicknameIsVisible"属性的值首先被映射,因此该值可用于测试!)

I had to change the order of my mapping so the preferences object (which has the values for the "NicknameIsVisible" property was mapped first so the value was available to test against!)

所以对我的三向映射的调用是:

So the call to my three-way mapping was:

var profileViewModel = EntityMapper.Map<ProfileViewModel>(preferences, member, account);

这确保了 preferences 对象首先被映射到 ViewModel,然后 account 对象的条件映射可以在值已设置.

This ensured that the preferences object was mapped to the ViewModel first, then the conditional mapping for the account object could take place once the values had been set.

所以这是我的解决方案,但我无法对自己的答案投票!

更多推荐

基于目标值的 C# AutoMapper 条件映射

本文发布于:2023-11-30 12:00:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649874.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:目标值   条件   AutoMapper

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!