c# - Automapper,按命名约定映射

标签 c# .net automapper

我正在使用自动映射器来映射我的实体。但是实体有不同的结构。

来源:

public class SourceEntity
{
    public string Name { get; set; }
    public Type Type { get; set; }
    public Communication SelectedCommunication { get; set; }
}

public enum Type
{
    Type1=1,
    Typ2
}

[Flags]
public enum Communication
{
    Phone =1,
    Email =2,
    Post =4
}

我还有 HasFlag() 扩展方法,如果选择了标志,它将返回 true。

目标实体:

public class DestinationEntity
{
    public string Name { get; set; }
    public bool Type1_PhoneSelected { get; set; }
    public bool Type1_EmailSelected { get; set; }
    public bool Type1_PostSelected { get; set; }
    public bool Type2_PhoneSelected { get; set; }
    public bool Type2_EmailSelected { get; set; }
    public bool Type2_PostSelected { get; set; }
}

我的 map :

        CreateMap<SourceEntity, DestinationEntity>()
            .ForMember(v => v.Name, opt => opt.MapFrom(i => i.Name));

但我想不出映射类型属性的最佳方法。 是否可以在不输入类似内容的情况下映射它:

.ForMemeber(v=>v.Test1_PhoneSelected, opt=>opt.MapFrom(i=>i.SelectedCommunication.HasFlag(Communication.Phone)))
.ForMemeber(v=>v.Test2_PhoneSelected, opt=>opt.MapFrom(i=>i.SelectedCommunication.HasFlag(Communication.Phone)))

对于每个属性。 有没有办法通过命名约定进行映射? 还是其他方式?

最佳答案

您可以使用 custom value resolvers

Although AutoMapper covers quite a few destination member mapping scenarios, there are the 1 to 5% of destination values that need a little help in resolving. Many times, this custom value resolution logic is domain logic that can go straight on our domain. However, if this logic pertains only to the mapping operation, it would clutter our source types with unnecessary behavior. In these cases, AutoMapper allows for configuring custom value resolvers for destination members.

自定义值解析器示例:

public class YourCustomResolver
    : IMemberValueResolver<object, object, Communication, bool>
{
    private Communication _communication;

    public YourCustomResolver(
        Communication communication)
    {
    }

    public bool Resolve(
        object source, 
        object destination,
        Communication sourceMember, 
        bool destMember, 
        ResolutionContext context)
    {
        return _communication == sourceMember;
    }
}

您的映射将如下所示:

CreateMap<SourceEntity, DestinationEntity>()
    .ForMember(dest => dest.Type1_PhoneSelected, opt => opt.ResolveUsing(new YourCustomResolver(Communication.Phone), src => src.SelectedCommunication))
    .ForMember(dest => dest.Type1_EmailSelected, opt => opt.ResolveUsing(new YourCustomResolver(Communication.Email), src => src.SelectedCommunication))
    .ForMember(dest => dest.Type1_PostSelected , opt => opt.ResolveUsing(new YourCustomResolver(Communication.Post) , src => src.SelectedCommunication))
    .ForMember(dest => dest.Type2_PhoneSelected, opt => opt.ResolveUsing(new YourCustomResolver(Communication.Phone), src => src.SelectedCommunication))
    .ForMember(dest => dest.Type2_EmailSelected, opt => opt.ResolveUsing(new YourCustomResolver(Communication.Email), src => src.SelectedCommunication))
    .ForMember(dest => dest.Type2_PostSelected , opt => opt.ResolveUsing(new YourCustomResolver(Communication.Post) , src => src.SelectedCommunication));

关于c# - Automapper,按命名约定映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50230052/

相关文章:

c# - 主构造函数未在 C# 6.0 中编译

c# - 自动生成 xaml ResourceDictionary

c# - Automapper v5 升级后的空属性值

c# - 使用正则表达式验证 C# 中的输入格式

c# - 在 Graphics 对象上绘制图像时出现 gdi+ 一般错误

c# - 按项目名称选择项目 ID

javascript - 如何在 Visual Studio 2010 中对文件系统更改运行命令?

.net - 使用 Inno Setup 在 Windows 8 上安装 .Net 3.5

c# - 使用 AutoMapper 将对象的属性映射到字符串

c# - 要列出的数据表 - AutoMapperException 或值未映射