Search code examples
automapperautomapper-5

Automapper (v5.1) ConstructedBy replacement?


I just upgraded my Automapper from 4.1 to 5.1 and I've found that ConstructedBy doesn't appear to be a valid function anymore. I read though the help files and it still references ConstructedBy as valid.

Here is what the code looks like:

cfg.CreateMap<XElement, Article>()
        .ForMember(
            dest => dest.Publication,
            opt => opt.ResolveUsing<XAttributeResolver<string>>()
                      .ConstructedBy(() => new XAttributeResolver<string>("publication_name", "publication", "publications")));

Solution

  • Just construct the resolver directly. I removed the redundant configuration in favor of an overload:

    cfg.CreateMap<XElement, Article>()
        .ForMember(
            dest => dest.Publication,
            opt => opt.ResolveUsing(new XAttributeResolver<string>("publication_name", "publication", "publications")));