Skip to content

create map for member

Bert Loedeman edited this page Aug 27, 2015 · 2 revisions

forMember

The createMap.forMember function specifies what action has to be performed when mapping a certain destination member.

Multiple forMember function calls can be chained together. In most cases, there will be one forMember function call for each destination member; however, it is possible to chain multiple forMember function calls for one destination member. The actions specified will be performed in a top-to-bottom manner, while the intermediate result is stored and passed through in the options object. unless you specify opts.ignore() or opts.mapFrom(), which will already be executed at configuration time.

Usage samples

Constant value

createMap.forMember function sample to always return a constant value.

var constantResult = 2;
automapper
	.createMap(fromKey, toKey)
	.forMember('prop', constantResult);

Function returning a constant value

createMap.forMember function sample to always return a constant value using a function.

var constantResult = 3;
automapper
	.createMap(fromKey, toKey)
	.forMember('prop', () => { return constantResult });

Custom coding using sourceObject and sourcePropertyName

createMap.forMember function sample to return a value calculated using the source object and property name.

automapper
	.createMap(fromKey, toKey)
	.forMember('prop', (opts: AutoMapperJs.IMemberConfigurationOptions) => { return opts.sourceObject[opts.sourcePropertyName].subProp.value * 2; });

Ignore a destination property

createMap.forMember function sample to ignore a property.

automapper
	.createMap(fromKey, toKey)
	.forMember('prop', (opts: AutoMapperJs.IMemberConfigurationOptions) => { opts.ignore(); });

createMap.forMember function sample to map a property from another property.

automapper
	.createMap(fromKey, toKey)
	.forMember('prop', (opts: AutoMapperJs.IMemberConfigurationOptions) => { opts.mapFrom('propDiff'); });

createMap.forMember chained functions sample to map a property using a multi-step missile approach.

automapper
	.createMap(fromKey, toKey)
	.forMember('birthday', (opts: AutoMapperJs.IMemberConfigurationOptions) => { opts.mapFrom('birthdayString'); })
	.forMember('birthday', (opts: AutoMapperJs.IMemberConfigurationOptions) => { return new Date(opts.destinationPropertyValue); });
Clone this wiki locally