Method MapperFor
- Namespace
- Mapperion
- Assembly
- Mapperion.dll
MapperFor<TSource, TDestination>(IMapper)
Settles the map for a pair once and hands back the function, for a loop that maps the same pair many times.
public static Func<TSource, TDestination> MapperFor<TSource, TDestination>(this IMapper mapper)
Parameters
mapperIMapperThe mapper to bind.
Returns
- Func<TSource, TDestination>
A function mapping one
TSourceto a newTDestination.
Type Parameters
TSourceThe source type.
TDestinationThe destination type.
Remarks
mapper.Map<Order, OrderDto>(order) is a generic method reached through an
interface, and the runtime works out its type arguments on every call. None of that
work depends on the object being mapped, so in a loop it is the same answer found over
and over. This asks for it once:
Func<Order, OrderDto> toDto = mapper.MapperFor<Order, OrderDto>();
foreach (Order order in orders)
{
results.Add(toDto(order));
}
The result is an ordinary Func<T, TResult>, so it also goes straight into
a Select. It maps exactly what Map<TSource, TDestination>(TSource)
maps, and it is bound to the configuration it came from.
Hold it for as long as the loop, or as a field beside the mapper. Asking for one per
call costs more than it saves, since the work it avoids is the work it does. For a
single map, Map is the one to write, and where mapping is genuinely what a
program spends its time on, the source generator beats all of this.
Exceptions
- ArgumentNullException
mapperis null.