A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/MapsterMapper/Mapster/wiki/Two-ways below:

Two ways · MapsterMapper/Mapster Wiki · GitHub

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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4