C# 自动映射器不映射基
嗨,我有一些麻烦,使我的映射工作在automapper 我有两个DTO BaseDto和BaseOrganizationDtoC# 自动映射器不映射基,c#,automapper,C#,Automapper,嗨,我有一些麻烦,使我的映射工作在automapper 我有两个DTO BaseDto和BaseOrganizationDto public class BaseDto {} public class SensitiveBaseDto : BaseDto {} 我使用以下映射: CreateMap<IEntity, BaseDto>() .Include<IEntity, SensitiveBaseDto>()
public class BaseDto
{}
public class SensitiveBaseDto : BaseDto
{}
我使用以下映射:
CreateMap<IEntity, BaseDto>()
.Include<IEntity, SensitiveBaseDto>()
.IncludeBase<IEntity, BaseDto>();
CreateMap()
.包括()
.IncludeBase();
我试着根据一些逻辑得到一个特定的dto,比如
public BaseDto MapToDto(Guid asSeenById, IEntity entity)
if (entity.Id != asSeenById)
{
return this.MapToDto<BaseDto>(entity);
}
return this.MapToDto<SensitiveBaseDto>(entity);
}
public-BaseDto映射到(Guid-asSeenById,IEntity实体)
if(entity.Id!=asSeenById)
{
返回此.maptodo(实体);
}
返回此.maptodo(实体);
}
但是它总是返回一个SensitiveBaseDto,我已经验证了maptodo方法中的逻辑是否正确执行
我遗漏了什么?通过这样做解决了这个问题:
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;
}
public override TDtoType maptodo(entity实体)
{
var dto=类型化(TDtoType)=类型化(敏感性)
?新的敏感度()
:new BaseDto();
此.Engine.Map(实体,dto);
将dto作为TDtoType返回;
}
AFAIK,使用Include
设置映射时,源类和目标类层次结构必须完全匹配。更好的解决方案可能是创建自定义转换器,并在转换器中创建正确的Dto
。