简体   繁体   English

自动映射,按命名约定映射

[英]Automapper, map by naming convention

I'm using automapper to map my entities. 我正在使用automapper来映射我的实体。 But entities have different structure. 但实体有不同的结构。

Source: 资源:

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
}

Also I have HasFlag() extension method that will return true if flag is selected. 我还有HasFlag()扩展方法,如果选择了flag,它将返回true。

Destination entity: 目标实体:

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; }
}

My map: 我的地图:

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

But I can't figure out the best way to map Types properties. 但我无法找出映射类型属性的最佳方法。 Is it possible to map it without typing something like: 是否可以在不输入类似内容的情况下进行映射:

.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)))

For each of this properties. 对于每个属性。 Is there any way to map by naming convention? 有没有办法通过命名约定进行映射? Or any other ways? 还是其他任何方式?

You can use 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. 虽然AutoMapper涵盖了相当多的目标成员映射方案,但是有1到5%的目标值需要一些帮助才能解析。 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. 在这些情况下,AutoMapper允许为目标成员配置自定义值解析器。

Example of custom value resolver: 自定义值解析程序的示例:

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;
    }
}

Your mapping will look like this: 您的映射将如下所示:

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));

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

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