2

I have a problem mapping the following complex types:

RequestDTO
{
    int OldUserId;
    string OldUsername;
    int NewUserId;
    string NewUsername;
}

Request
{
    User OldUser;
    User NewUser;
}

User
{
    int UserId;
    string Username;
}

Mapping/flattening Request to RequestDTO is easy. But how to unflatten this object?

Matthias
  • 280
  • 2
  • 9
  • 19

1 Answers1

3

Assuming that you make your classes and fields public, the example below shows how to handle this in AutoMapper. You need to tell it how to reconstruct your complex types from the dto.

        Mapper.CreateMap<RequestDTO, Request>()
            .ForMember(request => request.OldUser,
                mappingOption =>
                    mappingOption.MapFrom(dto => new User {UserId = dto.OldUserId, Username = dto.OldUsername}))
            .ForMember(request => request.NewUser,
                opt => opt.MapFrom(dto => new User {UserId = dto.NewUserId, Username = dto.NewUsername}));
JonMac1374
  • 466
  • 2
  • 7