-
Notifications
You must be signed in to change notification settings - Fork 331
Two ways
chaowlert edited this page Apr 13, 2019
·
1 revision
If you need to map object from POCO to DTO, and map back from DTO to POCO. You can define the setting once by using TwoWays
.
TypeAdapterConfig<Poco, Dto>
.NewConfig()
.TwoWays()
.Map(dto => dto.Code, poco => poco.Id); //<-- this setting will apply dto.Code = poco.Id & poco.Id = dto.Code for reverse mapping
NOTE: TwoWays
command need to call before setting to take effect.
TypeAdapterConfig<Poco, Dto>
.NewConfig()
.Map(dto => dto.Foo, poco => poco.Bar) //<-- this map only apply to Poco->Dto
.TwoWays()
.Map(dto => dto.Foz, poco => poco.Baz); //<-- this map will apply both side
By default, Mapster will perform flattening.
class Staff {
public string Name { get; set; }
public Staff Supervisor { get; set; }
...
}
struct StaffDto {
public string SupervisorName { get; set; }
}
Above example, without any setup, you can map from POCO to DTO and you will get SupervisorName
from Supervisor.Name
(flattening).
However, unflattening process needed to be defined. You can map to Supervisor.Name
from SupervisorName
by
TypeAdapterConfig<StaffDto, Staff>.NewConfig()
.Unflattening(true);
TypeAdapterConfig<Staff, StaffDto>
.NewConfig()
.TwoWays(); //<-- this will also map poco.Supervisor.Name = dto.SupervisorName for reverse mapping
- 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