我想使用ConvertUsing转换基类的两个属性,但是没有被调用
基类
public abstract class BaseDadoMestreViewModel
{
public DateTime UltimaAtualizacao { get; set; }
public bool Excluido { get; set; }
public bool Ativo { get; set; }
}
public abstract class BaseDadoMestre<TEntity> : EntityCrud<TEntity>
where TEntity : class
{
public DateTime UltimaAtualizacao { get; set; }
public string MarcadoEliminacao { get; set; }
public bool Desabilitado { get; set; }
}
课程
public class CalendarioViewModel: BaseDadoMestreViewModel
{
public DateTime Data { get; set; }
}
public class CalendarioDTO: BaseDadoMestre<CalendarioDTO>
{
public DateTime Data { get; set; }
}
ITypeConverter
public class BaseConverter<TEntity> :
ITypeConverter<BaseDadoMestre<TEntity>, BaseDadoMestreViewModel>,
ITypeConverter<BaseDadoMestreViewModel, BaseDadoMestre<TEntity>>
where TEntity : BaseDadoMestre<TEntity>
{
public BaseDadoMestreViewModel Convert(BaseDadoMestre<TEntity> source, BaseDadoMestreViewModel destination, ResolutionContext context)
{
destination.Excluido = (source.MarcadoEliminacao== "X");
destination.Ativo = !source.Desabilitado;
return destination;
}
public BaseDadoMestre<TEntity> Convert(BaseDadoMestreViewModel source, BaseDadoMestre<TEntity> destination, ResolutionContext context)
{
destination.MarcadoEliminacao = (source.Excluido ? null : "X");
destination.Desabilitado = !source.Ativo;
return destination;
}
}
最后是我的地图
CreateMap(typeof(BaseDadoMestre<>), typeof(BaseDadoMestreViewModel)).ConvertUsing(typeof(BaseConverter<>));
CreateMap(typeof(BaseDadoMestreViewModel), typeof(BaseDadoMestre<>)).ConvertUsing(typeof(BaseConverter<>));
CreateMap<CalendarioDTO, CalendarioViewModel>()
当我运行映射命令时,未调用ConvertUsing。
mapper.Map<CalendarioViewModel>(new CalendarioDTO() { MarcadoEliminacao = "X", Desabilitado = true });
答案 0 :(得分:0)
从Automapper文档开始,在为派生的基类创建映射时,您需要指定继承。
选中此一项:
http://docs.automapper.org/en/stable/Mapping-inheritance.html