Skip to main content

MapFrom

Call mapFrom() to select the value, from the sourceObject, to map to the property being configured

Value Selector

mapFrom() accepts a ValueSelector that AutoMapper will use to get the value from the sourceObject upon mapping the configured property

createMap(
mapper,
User,
UserDto,
forMember(
(d) => d.fullName,
mapFrom((source) => source.firstName + ' ' + source.lastName)
)
);

Value Resolver

A slightly less common approach is to use a Resolver with mapFrom(). Resolver has the following interface:

export interface Resolver<TheSource, TheDestination, TheReturnType> {
resolve(source: TheSource, destination?: TheDestination): TheReturnType;
}

Resolver#resolve() is called with the whole sourceObject and the destinationObject which allows us to handle some complex logic to arrive at the result for the configured property. We can reuse Resolver as well as separating Resolver in a different file to manage easier.

export const taxResolver: Resolver<Item, ItemDto, number> = {
resolve(item): number {
return item.type === 'A' ? item.price * 0.5 : item.price * 0.9;
},
};

createMap(
mapper,
Item,
ItemDto,
forMember((d) => d.tax, mapFrom(taxResolver))
);

mapFrom() sets the TransformationType to TransformationType.MapFrom