2

I am trying to map a source object to a DTO:

var queryable = GetDtoQueryable(); //IQueryable<DTO>
return _mapper //IMapper
    .Map<IQueryable<Source>,List<DTO>>(queryable);

The problem with this is that GetDtoQueryable() is generating an IQueryable using Entity Framework, so the call to Map() is roundtripping on every element in the collection. The fix to this should be easy:

var queryable = GetDtoQueryable();
return queryable
    .ProjectTo<DTO>()
    .ToList();

And it mostly is, however, I discovered that this mapping is using the default property mapper via reflection and not a custom converter that was established separately:

CreateMap<Source, DTO>()
    .ConvertUsing<SourceDtoConverter>();

public class SourceDtoConverter : ITypeConverter<Source, DTO>
{
    public DTO Convert(Source source, DTO dest, ResolutionContext context){ ... }
}

I need ProjectTo() to invoke this converter. I am guessing this is not possible to ensure that the converter doesn't make any calls LinqToEntities can't execute, but I want to know if there are any alternative solutions that can avoid roundtripping while giving me the power of the custom converter. So far I have been trying to replace it with the ForMember() method but that is effectively a rewrite of this converter.

jokul
  • 1,269
  • 16
  • 37

1 Answers1

0

The ProjectTo<>() extension method should allow you to pass in the configuration. Inject an instance of the mapper—which it seems like you already have-and pass the configuration to the Extension method:

var queryable = GetDtoQueryable();

return queryable
    .ProjectTo<DTO>(_mapper.ConfigurationProvider)
    .ToList();

Hope that helps.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77