1

I have a situation where I need to flatten a collection of items.

I set my Mapping profile like this:

CreateMap<IEnumerable<IMySetting>, IMySettingLookup>()
    .ForMember(l => l.MySetting1,
        o => o.MapFrom(sc =>
            sc.FirstOrDefault(s => s.SettingName == nameof(IMySettingLookup.MySetting1)).SettingValue))
    .ForMember(l => l.MySetting2,
        o => o.MapFrom(sc =>
            sc.FirstOrDefault(s => s.SettingName == nameof(IMySettingLookup.MySetting2)).SettingValue))
    .As<MySettingLookup>(); // notice this is a concrete type

when i call the map function like this:

IEnumerable<IMySetting> settings =....;
IMySettingLookup lookup = _mapper.Map<IMySettingLookup>(settings);

i get this error:

    AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
    
    Mapping types:
    Object -> IMySettingLookup
    System.Object -> MyStuff.Models.IMySettingLookup
  Stack Trace: 
    lambda_method(Closure , Object , IMySettingLookup, ResolutionContext )

However when I set my mapping profile using concrete types and call the Map method with the concrete type like below, everything works:

CreateMap<IEnumerable<MySetting>, MySettingLookup>() // notice everything is concrete types
   .ForMember(...).... 



and

IEnumerable<MySetting> settings =....;
IMySettingLookup lookup = _mapper.Map<MySettingLookup>(settings); // notice concrete types

My Question is how can I define my mapping profiles when my source type is an IEnumerable<T> where T is an interface?

EDIT

I have uploaded a minimal reproducible example to https://github.com/nandun5/so-68219262-automapper

Note that I won't see any issues if i simply use concrete classes instead of the interfaces. however this is not really an option for me in my actual scenario because all of my data service contracts are interfaces.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Nandun
  • 1,802
  • 2
  • 20
  • 35

1 Answers1

2

It seems that .As<MyType> doesn't give the concrete type to use, rather it tells AutoMapper which mapping to look up. This is supported by the documentation which states:

For simple cases, you can use As to redirect a base map to an existing derived map:

In your case, it's looking up a mapping for IEnumerable<MySetting> to MySettingLookup, which it isn't finding.

Therefore, you actually want to declare two mappings:

CreateMap<IEnumerable<IMySetting>, MySettingLookup>()
   .ForMember(...);

CreateMap<IEnumerable<IMySetting>, IMySettingLookup>()
   .As<MySettingLookup>();

This will then use the <IEnumerable<MySetting>, MySettingLookup> mapping when trying to map <IEnumerable<MySetting>, IMySettingLookup>.

Alternatively, you can simply replace .As<MySettingLookup>() with the following .ConstructUsing(_ => new MySettingLookup()):

CreateMap<IEnumerable<IMySetting>, IMySettingLookup>()
   .ForMember(...)
   .ConstructUsing(_ => new MySettingLookup());
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • 1
    https://github.com/AutoMapper/AutoMapper/issues/3526#issuecomment-713628068 – Lucian Bargaoanu Jul 02 '21 at 04:32
  • thanks @Llama unfortunately this didn't work for me. – Nandun Jul 02 '21 at 14:03
  • @Nandun please provide a full [mcve] as this worked in my tests, with my approximation of your scenario. – ProgrammingLlama Jul 02 '21 at 14:25
  • @Llama i have created a minimal reproducible example in my github repo - https://github.com/nandun5/so-68219262-automapper – Nandun Jul 06 '21 at 21:38
  • 1
    @Nandun I've taken a look at your example, and I think there might be a bug in Automapper: https://dotnetfiddle.net/mGbguS - It seems to be interpreting the source type as "Object". It looks like it's something to do with `return new Setting[]` vs `return new ISetting[]`. I think it's perhaps something to do wtih the covariant nature of `IEnumerable`. – ProgrammingLlama Jul 07 '21 at 03:51
  • 1
    I've added an isssue on the AutoMapper Git repo [here](https://github.com/AutoMapper/AutoMapper/issues/3685). – ProgrammingLlama Jul 07 '21 at 04:02
  • 1
    Pending them fixing it / dismissing the issue, I'd suggest either explicitly returning an `ISetting` array from your repository method, or being explicit about what you're mapping from/to (i.e. `IMySettingLookup lookup = mapper.Map, IMySettingLookup>(settings);`). – ProgrammingLlama Jul 07 '21 at 04:09