-
Couldn't load subscription status.
- Fork 380
Custom conversion logic
In some cases, you may want to have complete control over how an object is mapped. You can register specific transformations using the MapWith method.
//Example of transforming string to char[].
TypeAdapterConfig<string, char[]>.NewConfig()
.MapWith(str => str.ToCharArray());MapWith also useful if you would like to copy instance rather than deep copy the object, for instance, JObject or DbGeography, these should treat as primitive types rather than POCO.
TypeAdapterConfig<JObject, JObject>.NewConfig()
.MapWith(json => json);In case you would like to combine MapWith with other settings, for example, PreserveReference, Include, or AfterMapping, you can pass applySettings to true.
TypeAdapterConfig<ComplexPoco, ComplexDto>.NewConfig()
.PreserveReference(true)
.MapWith(poco => poco.ToDto(), applySettings: true);You can control mapping to existing object logic by MapToTargetWith. For example, you can copy data to existing array.
TypeAdapterConfig<string[], string[]>.NewConfig()
.MapToTargetWith((src, dest) => Array.Copy(src, dest, src.Length));NOTE: if you set MapWith setting but no MapToTargetWith setting, Mapster will use logic from MapWith setting.
You might not need to specify custom mapping logic completely. You can let Mapster do the mapping, and you do logic where Mapster cannot cover by using AfterMapping.
TypeAdapterConfig<Poco, Dto>.NewConfig()
.AfterMapping((src, dest) => SpecialSetFn(src, dest));- 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