-
Notifications
You must be signed in to change notification settings - Fork 333
Setting values
chaowlert edited this page Apr 10, 2020
·
5 revisions
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.
Trim string
TypeAdapterConfig<TSource, TDestination>.NewConfig()
.AddDestinationTransform((string x) => x.Trim());
Null replacement
TypeAdapterConfig<TSource, TDestination>.NewConfig()
.AddDestinationTransform((string x) => x ?? "");
Return empty collection if null
config.Default.AddDestinationTransform(DestinationTransform.EmptyCollectionIfNull);
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>();
- 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