-
Notifications
You must be signed in to change notification settings - Fork 332
Custom mapping
You can customize how Mapster maps values to a property.
TypeAdapterConfig<TSource, TDestination>
.NewConfig()
.Map(dest => dest.FullName,
src => $"{src.FirstName} {src.LastName}");
You can even map when source and destination property types are different.
TypeAdapterConfig<TSource, TDestination>
.NewConfig()
.Map(dest => dest.Gender, //Genders.Male or Genders.Female
src => src.GenderString); //"Male" or "Female"
The Map configuration can accept a third parameter that provides a condition based on the source. If the condition is not met, Mapster will retry with next conditions. Default condition should be added at the end without specifying condition. If you do not specify default condition, null or default value will be assigned.
TypeAdapterConfig<TSource, TDestination>
.NewConfig()
.Map(dest => dest.FullName, src => "Sig. " + src.FullName, srcCond => srcCond.Country == "Italy")
.Map(dest => dest.FullName, src => "Sr. " + src.FullName, srcCond => srcCond.Country == "Spain")
.Map(dest => dest.FullName, src => "Mr. " + src.FullName);
NOTE: if you would like to skip mapping, when condition is met, you can use IgnoreIf
(https://github.com/MapsterMapper/Mapster/wiki/Ignoring-members#ignore-conditionally).
Map
command can map to private member by specify name of the members.
TypeAdapterConfig<TSource, TDestination>
.NewConfig()
.Map("PrivateDestName", "PrivateSrcName");
For more information about mapping non-public members, please see https://github.com/MapsterMapper/Mapster/wiki/Mapping-non-public-members.
Map
can be defined to map deep destination property.
TypeAdapterConfig<Poco, Dto>.NewConfig()
.Map(dest => dest.Child.Name, src => src.Name);
If Map
contains only property path, null propagation will be applied.
TypeAdapterConfig<Poco, Dto>.NewConfig()
.Map(dest => dest.Name, src => src.Child.Name);
From above example, if src.Child
is null, mapping will return null instead of throw NullPointerException
.
Example 1: Include property to Poco
public class SubDto
{
public string Extra { get; set; }
}
public class Dto
{
public string Name { get; set; }
public SubDto SubDto { get; set; }
}
public class Poco
{
public string Name { get; set; }
public string Extra { get; set; }
}
In this case, you would like to map all properties from Dto
to Poco
, and also include all properties from Dto.SubDto
to Poco
. You can do this by just mapping dto.SubDto
to poco
in configuration.
TypeAdapterConfig<Dto, Poco>.NewConfig()
.Map(poco => poco, dto => dto.SubDto);
Example 2: Mapping 2 objects to poco
In this example, you have Dto1
and Dto2
, and you would like to map both objects to a Poco
. You can do this by wrapping Dto1
and Dto2
into a tuple. And then mapping tuple.Item1
and tuple.Item2
to Poco
.
TypeAdapterConfig<(Dto1, Dto2), Poco>.NewConfig()
.Map(dest => dest, src => src.Item1)
.Map(dest => dest, src => src.Item2);
- Configuration
- Config inheritance
- Config instance
- Config location
- Config validation & compilation
- Config for nested mapping
- Custom member matching logic
- Constructor mapping
- Before & after mapping
- Setting values
- Shallow & merge mapping
- Recursive & object references
- Custom conversion logic
- Inheritance