1

It is weird but I am stuck with a pretty simple case:

Say I have a class:

class Record
{
    public List<Data> Data {get; set;}

    public string OtherProp1 {get; set;}
    public string OtherProp2 {get; set;}
}

And I want to map a List<Model> from it. Which is basically:

mapper.Map<List<Model>>(Record.Data);

But I want Mapster (or Mapper) to be able to make it right from the Record like this:

mapper.Map<List<Model>>(Record);

So

mapper.Map<List<Model>>(Record.Data); // Works easily
mapper.Map<List<Model>>(Record);      // Can't config properly
Meligy
  • 35,654
  • 11
  • 85
  • 109
Alexander
  • 49
  • 1
  • 7

1 Answers1

1

I did find one way with Mapster but it seems like cheating:

var config = new TypeAdapterConfig();

config.ForType<Data, Model>();
config.ForType<Record, List<Model>>().MapWith(record => record.Data.Adapt<List<Model>>(config));

Because I sort of use Mapster inside of Mapster config )

It must be the way to point the source path like this:

config.ForType<Record, List<Model>>().Map(dest => dest, record => record.Data);

But all such attempts lead to:

  1. Always empty destination collection in Mapster
  2. Exceptions in AutoMapper:

Expression 'dest => dest' must resolve to top-level member and not any child object's properties. You can use ForPath, a custom resolver on the child type, or the AfterMap option instead. (Parameter 'lambdaExpression') -- using .ForMember().

Only member accesses are allowed. dest => dest (Parameter 'destinationMember') -- using .ForPath().

Alexander
  • 49
  • 1
  • 7