如何将外部和内部源映射到同一对象-自动映射器

时间:2018-11-05 15:41:27

标签: c# automapper

我有这样的结构:

public class UserEntity
{
    public long Id { get; set; }
    public string Name { get; set; }
    public string UserName { get; set; }
    public ComplexObject TimeZone { get; set; }
}

public class UserQueryResult
{
    public UserEntity User { get; set; }
    public ObjectId AccountId { get; set; }
}

public class UserDto
{
    public long Id { get; set; }
    public string Name { get; set; }
    public string UserName { get; set; }
    public string TimeZoneId { get; set; }
    public string AccountId { get; set; }
}

我有这样的UserEntity到UserDto映射:

CreateMap<UserEntity, UserDto>()
    .ForMember(dest => dest.TimeZoneId, opt => opt.ResolveUsing<UserTimeZoneResolver>())
    .ForMember(dest => dest.AccountId, opt => opt.Ignore());

如您所见,从UserEntity到UserDto有一个解析器。现在,如果我想将UserQueryResult映射到UserDto,如何在不需要手动映射所有属性的情况下重用上述映射器?

CreateMap<UserQueryResult, UserDto>()
    .ForMember(dest => dest.AccountId, opt => opt.MapFrom(src => src.AccountId.ToString()))
    .// use the UserEntity to UserDto map?

0 个答案:

没有答案