模型转换

框架通过特性实现模型转换。

ClassMapper 特性标记在类上,PropertyMapper 特性标记在属性上。

将 CMSColumn 转换为 ColumnVo:

[ClassMapper(typeof(CMSColumn))]
public class ColumnVo
{
    [PropertyMapper(nameof(CMSColumn.ColumnId))]
    public string ColumnId { get; set; }

    [PropertyMapper(nameof(CMSColumn.ColumnName))]
    public string ColumnName { get; set; }

    [PropertyMapper(nameof(CMSColumn.ColumnSummary))]
    public string ColumnSummary { get; set; }

    [PropertyMapper(nameof(CMSColumn.ColumnPic))]
    public string ColumnPic { get; set; }

    [PropertyMapper(nameof(CMSColumn.ColumnPicM))]
    public string ColumnPicM { get; set; }

    [PropertyMapper(nameof(CMSColumn.ColumnSequence))]
    public int ColumnSequence { get; set; }

    [PropertyMapper(nameof(CMSColumn.ColumnStatus))]
    public int ColumnStatus { get; set; }
}
                    

如果转换前后的属性名一致,可以省略标记 PropertyMapper 特性。

调用方式:

[Autowired]
public IMapper _iMapper { get; set; }

CMSColumn column = await _columnRepository.FindAsync(dto.Id);
ColumnVo vo = _iMapper.Map<ColumnVo>(column);