Suppose my AutoMapper configuration knows how to map from type T1 to type T2 and from T2 to type T3. I then have the following. It works:
public static class MapperExtensions
{
public static T3 MapVia<T1, T2, T3>(this IMapper mapper, T1 t1) {
var t2 = mapper.Map<T2>(t1);
var t3 = mapper.Map<T3>(t2);
return t3;
}
/// <summary> The calling code needs to ensure that t1 is of a type that the mapper knows how to map to type T2.</summary>
public static T3 MapVia<T2, T3>(this IMapper mapper, object t1) {
var t2 = mapper.Map<T2>(t1);
var t3 = mapper.Map<T3>(t2);
return t3;
}
}
My question is whether it is possible to bypass the middle type? I would love to be able to do something in my configuration to tell it "Generate a map from T1 to T3 that is the composition of your map from T1 to T2 and your map from T2 to T3." Then I could just map from T1 to T3 normally.
There are times when T2 is large, which may make this a performance issue. There are also cases where T2 is not particularly large.