forMember
Select a destination path and assign one member-mapping function, with an optional precondition.
forMember() targets one destination member and assigns its transformation:
The callbacks are contextually typed from the source and destination passed to createMap().
createMap<User, UserDto>(mapper: Mapper, source: ModelIdentifier<User>, destination: ModelIdentifier<UserDto>, ...mappingConfigFns: (MappingConfiguration<User, UserDto> | undefined)[]): Mapping<User, UserDto> (+1 overload)createMap(
const mapper: Mappermapper,
class UserUser,
class UserDtoUserDto,
forMember<User, UserDto, string>(selector: Selector<UserDto, string>, preCondOrMapMemberFn: PreConditionReturn<User, UserDto, string> | MemberMapReturn<User, UserDto, string> | undefined, mapMemberFn?: MemberMapReturn<User, UserDto, string> | undefined): MappingConfiguration<User, UserDto>forMember(
(destination: UserDtodestination) => destination: UserDtodestination.UserDto.fullName: stringfullName,
mapFrom<User, UserDto, string>(from: ((source: User) => MaybePromise<string>) | Resolver<User, UserDto, string>): MapFromReturn<User, UserDto, string>mapFrom((source: Usersource) => `${source: Usersource.User.firstName: stringfirstName} ${source: Usersource.User.lastName: stringlastName}`),
),
);
The destination selector is strongly typed. The second argument determines how AutoMapper produces that value.
Precondition
Add preCondition() before the member function to skip its work unless a source predicate succeeds. An optional default is assigned when the predicate fails.
forMember(
(destination) => destination.fullName,
preCondition((source) => source.visible, "Hidden user"),
mapFrom((source) => `${source.firstName} ${source.lastName}`),
);
Use custom members for exceptions to convention. If almost every destination path needs one, an explicit transformation may be easier to understand.