forked from MapsterMapper/Mapster
-
Notifications
You must be signed in to change notification settings - Fork 3
Setting values
chaowlert edited this page May 25, 2017
·
1 revision
You can use Map
method to specify logic to compute value. For example, compute full name from first name and last name.
TypeAdapterConfig<Poco, Dto>.NewConfig()
.Map(dest => dest.FullName, src => src.FirstName + " " + src.LastName);
While Map
method specify logic for single property, AddDestinationTransform
allows transforms for all items of a type, such as trimming all strings. But really any operation can be performed on the destination value before assignment.
//Global
TypeAdapterConfig.GlobalSettings.Default.AddDestinationTransforms((string x) => x.Trim());
//Per mapping configuration
TypeAdapterConfig<TSource, TDestination>.NewConfig()
.AddDestinationTransforms((string x) => x.Trim());
In some cases, you might would like to pass runtime values (ie, current user). On configuration, we can receive run-time value by MapContext.Current.Parameters
.
TypeAdapterConfig<Poco, Dto>.NewConfig()
.Map(dest => dest.CreatedBy,
src => MapContext.Current.Parameters["user"]);
To pass run-time value, we need to use BuildAdapter
method, and call AddParameters
method to add each parameter.
var dto = poco.BuildAdapter()
.AddParameters("user", this.User.Identity.Name)
.AdaptToType<Dto>();