基于目标值的 C# AutoMapper 条件映射 [英] C# AutoMapper Conditional Mapping based upon target value

查看:50
本文介绍了基于目标值的 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 的方法绑定到三个实体(http://consultingblogs.emc.com/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 (http://consultingblogs.emc.com/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 条件映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆