Table of Contents

Method MapFast

Namespace
Mapperion
Assembly
Mapperion.dll

MapFast<TSource, TDestination>(IMapper, TSource)

Maps source to a new instance of TDestination, skipping the cost of calling a generic method through an interface.

public static TDestination MapFast<TSource, TDestination>(this IMapper mapper, TSource source)

Parameters

mapper IMapper

The mapper to use.

source TSource

The source object. May be null.

Returns

TDestination

The mapped destination instance.

Type Parameters

TSource

The source type.

TDestination

The destination type.

Remarks

The result is the same as Map<TSource, TDestination>(TSource) in every respect. The only difference is how the call gets there: a generic method reached through an interface makes the runtime work out its type arguments on each call, which on a small map is about a quarter of the time it takes. This checks for the mapper the library builds and calls it directly when it finds it, and falls back to the interface for any other implementation, so a decorator or a test double still works.

Worth reaching for in a loop that maps a great many objects, and not worth the noise anywhere else: the saving is measured in nanoseconds per call, which is nothing next to almost anything else a program does. If mapping really is the hot path, the source generator is the answer rather than this; it runs at roughly the speed of code written by hand, which is far more than this can give back.

Exceptions

ArgumentNullException

mapper is null.

MapFast<TSource, TDestination>(IMapper, TSource, TDestination)

Maps source onto an existing instance, skipping the cost of calling a generic method through an interface.

public static TDestination MapFast<TSource, TDestination>(this IMapper mapper, TSource source, TDestination destination)

Parameters

mapper IMapper

The mapper to use.

source TSource

The source object. May be null.

destination TDestination

The destination instance to populate.

Returns

TDestination

The populated destination instance.

Type Parameters

TSource

The source type.

TDestination

The destination type.

Remarks

The same trade as MapFast<TSource, TDestination>(IMapper, TSource), and the same advice: worth it in a loop over a great many objects, not worth the noise otherwise.

Exceptions

ArgumentNullException

mapper is null.