forked from MapsterMapper/Mapster
-
Notifications
You must be signed in to change notification settings - Fork 2
Custom conversion logic
chaowlert edited this page May 27, 2017
·
2 revisions
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.