2015-09-01 71 views
3

嗨,我有一些麻烦,使我的映射在自动映射工作。Automapper不映射基地

我有2个的DTO BaseDto和BaseOrganizationDto

public class BaseDto 
{} 

public class SensitiveBaseDto : BaseDto 
{} 

我用下面的映射:

CreateMap<IEntity, BaseDto>() 
       .Include<IEntity, SensitiveBaseDto>() 
       .IncludeBase<IEntity, BaseDto>(); 

我试图让基于某种逻辑一定DTO像

public BaseDto MapToDto(Guid asSeenById, IEntity entity) 
    if (entity.Id != asSeenById) 
    { 
     return this.MapToDto<BaseDto>(entity); 
    } 
    return this.MapToDto<SensitiveBaseDto>(entity); 
} 

但它总是返回一个SensitiveBaseDto,我已经验证了MapToDto方法中的逻辑是ex正确使用。

我错过了什么?

+0

据我所知,设置了一个映射时'Include',源和目的的类层次结构必须完全匹配。更好的解决方案可能是创建自定义转换器,并在转换器中创建正确的“Dto”。 –

回答

1

通过这样解决的:

public override TDtoType MapToDto<TDtoType>(IEntity entity) 
{ 
    var dto = typeof(TDtoType) == typeof(SensitiveDto) 
     ? new SensitiveDto() 
     : new BaseDto(); 

    this.Engine.Map(entity, dto); 
    return dto as TDtoType; 
}