2

I have some objects with a large number of properties. When I started using AutoMapper I was attempting to just map a few nested objects to understand how it works. All the time I thought it would map over properties with the same name at the source and the destination. However it seems that once I have some ForMember() rules it only maps the properties for which I have rules.

Is there a way to only a specify a few rules, where the source and destination object differ? Or do I have to be explicit and map every property?

Mapper.CreateMap<MessageWireFormat, Message>()
    .ForMember(dest => dest.PlainBody, opt => opt.MapFrom(src => src.Body.Plain))
    .ForMember(dest => dest.ParsedBody, opt => opt.MapFrom(src => src.Body.Parsed))
    .ForMember(dest => dest.RichBody, opt => opt.MapFrom(src => src.Body.Rich))
    .ForMember(dest => dest.Excerpt, opt => opt.MapFrom(src => src.ContentExcerpt))
    .ForMember(dest => dest.AttachmentCount, opt => opt.MapFrom(src => src.Attachments.Length))
    .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
    .ForMember(dest => dest.Url, opt => opt.MapFrom(src => src.Url))
    .ForMember(dest => dest.WebUrl, opt => opt.MapFrom(src => src.WebUrl))
    .ForMember(dest => dest.NetworkId, opt => opt.MapFrom(src => src.NetworkId))
    .ForMember(dest => dest.GroupId, opt => opt.MapFrom(src => src.GroupId))
    .ForMember(dest => dest.SystemMessage, opt => opt.MapFrom(src => src.SystemMessage))
    .ForMember(dest => dest.Privacy, opt => opt.MapFrom(src => src.Privacy))
    .ForMember(dest => dest.DirectMessage, opt => opt.MapFrom(src => src.DirectMessage))
    .ForMember(dest => dest.SenderType, opt => opt.MapFrom(src => src.SenderType))
    .ForMember(dest => dest.SenderId, opt => opt.MapFrom(src => src.SenderId))
    .ForMember(dest => dest.CreatedAt, opt => opt.MapFrom(src => src.CreatedAt))
    .ForMember(dest => dest.ClientType, opt => opt.MapFrom(src => src.ClientType))
    .ForMember(dest => dest.ClientUrl, opt => opt.MapFrom(src => src.ClientUrl))
    .ForMember(dest => dest.RepliedToId, opt => opt.MapFrom(src => src.RepliedToId))
    .ForMember(dest => dest.Language, opt => opt.MapFrom(src => src.Language));
Brian Lyttle
  • 14,558
  • 15
  • 68
  • 104

1 Answers1

2

Holy moly. Nearly all of those mappings are unnecessary. MapFrom is only intended when names don't match, not when they do. Even when they don't match, I'd need a really good reason to not have "BodyPlain", which would map without configuration, instead of "PlainBody", which doesn't match "Body.Plain".

Jimmy Bogard
  • 26,045
  • 5
  • 74
  • 69
  • It turns out that MessageWireFormat was changed to internal. When I made it public again I was able to get rid of the mappings. I saw that there is still a bug open for this, and it's a bit of a contentious issue. – Brian Lyttle Oct 18 '13 at 15:41