Naming Conventions
NamingConvention
allows AutoMapper to map models with different casing convention in terms of properties' names. Out of the box, AutoMapper provides 3 conventions:
CamelCaseNamingConvention
PascalCaseNamingConvention
SnakeCaseNamingConvention
By default, AutoMapper does not set a default convention. Auto Flattening can only be applied when NamingConvention
is set on the models, even if they share the same casing.
// Mapper level conventions (global for all Mappings)
const mapper = createMapper({
strategyInitializer: classes(),
namingConventions: new CamelCaseNamingConvention(),
});
// Mapping level conventions (applied for one specific Mapping)
createMap(
mapper,
User,
UserDto,
namingConventions(new CamelCaseNamingConvention())
);
// Profile level conventions (applied for ALL Mappings inside a Profile)
addProfile(
mapper,
userProfile,
namingConventions(new CamelCaseNamingConvention())
);
Different conventions for Source
and Destination
All variants of namingConventions
also accept an object type { source: NamingConvention, destination: NamingConvention }
to set different conventions on different models.
const mapper = createMapper({
strategyInitializer: classes(),
namingConventions: {
source: new PascalCaseNamingConvention(),
destination: new CamelCaseNamingConvention(),
},
});