0

After migration from an old version of AutoMapper (before 5) to version 9 there is one spot which causes headache. Old implementation:

.ForMember(a => a.Definition, o =>
{
    o.Condition(s => s.TypeId == DocumentationType.Medication);
    o.ResolveUsing((d, ctx) => ctx.Engine.Map<MedicationDefinitionContent>(d.Content.MedicationContentData));
})

which uses this extension method:

public static class MappingExtensions
{
    public static void ResolveUsing<TType>(this IMemberConfigurationExpression<TType> expression, Func<TType, ResolutionContext, object> map)
    {
        expression.ResolveUsing(result => map((TType)result.Value, result.Context));
    }
}

I fixed the first error that that IMemberConfigurationExpression needs 3 arguments, but then I learned that ResolutionContext does not contain a definition for engine anymore. I looked in the upgrade guide of version 5 and found that the ResolutionContext has been changed, but I do not understand how to fix this. The code seems to be pretty tricky. Can someone help, please?

manon
  • 252
  • 2
  • 9
  • `MapFrom(s => s.Content.MedicationContentData)` – Lucian Bargaoanu Sep 23 '22 at 07:34
  • @LucianBargaoanu Thanks. I already tried that and tried it again. It leads to: AutoMapper.AutoMapperConfigurationException: The following member on Comp.Model.Entities.DocumentationDefinition cannot be mapped: Definition Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type Comp.Model.Entities.DocumentationDefinition. Context: Mapping to member Definition from Comp.App.Extensions.Prod.Dtos.v1.Documentation to Comp.Model.Entities.DocumentationDefinition – manon Sep 23 '22 at 09:39
  • These are user errors, they are trying to tell you what's wrong with your code. If you don't try to understand what they're are telling you, you'll never be effective using AM. Take the time to do that. – Lucian Bargaoanu Sep 23 '22 at 10:05

1 Answers1

0

@Lucian Bargaoanu Ok, but the member "Definition" is the member wie map with MapFrom(s => s.Content.MedicationContentData). So different to the exception there is already a mapping. The member "Definition" is of type SerialisationHelper a helper class for Json stuff. It also has a mapping.

                CreateMap<MedicationDefinitionContent, SerialisationHelper>()
                    .IgnoreAllUnmapped()
                    .AfterMap((s, t) => t.Write = s);

And MedicationDefinitionContent has a separate mapping.

CreateMap<MedicationContentData, MedicationDefinitionContent>()

MedicationDefinitionContent is annotated with [JsonObject(MemberSerialization.OptIn)]

so, a direct mapping from MedicationDefinitionContent to "Definition" does not work.

How you see I try to understand it, but maybe it needs more time.

manon
  • 252
  • 2
  • 9