using System.Linq.Expressions; using AutoMapper; using DS.Module.Core.Extensions; namespace DS.Module.AutoMapper; /// /// /// public static class AutoMapperExtension0 { private static IMapper _mapper = null; /// /// 写入AutoMapper实例 /// public static void SetMapper(IMapper mapper) { mapper.NotNull(nameof(mapper)); _mapper = mapper; } /// /// 检查传入的实例 /// private static void CheckMapper() { _mapper.NotNull(nameof(_mapper)); } /// /// /// /// 要映射的目标类型 /// 源对象 /// 目标类型的对象 public static TTarget MapTo(this object source) { CheckMapper(); source.NotNull(nameof(source)); return _mapper.Map(source); } /// /// 使用源类型的对象更新目标类型的对象 /// /// 源类型 /// 目标类型 /// 源对象 /// 待更新的目标对象 /// 更新后的目标类型对象 public static TTarget MapTo(this TSource source, TTarget target) { CheckMapper(); source.NotNull(nameof(source)); target.NotNull(nameof(target)); return _mapper.Map(source, target); } /// /// 将数据源映射为指定的集合 /// /// 动态实体 /// 数据源 /// public static IEnumerable MapToList(this IEnumerable sources) { CheckMapper(); sources.NotNull(nameof(sources)); return _mapper.Map>(sources); } /// /// 将数据源映射为指定的集合 /// /// 数据源 /// 成员展开 public static IQueryable ToOutput(this IQueryable source, params Expression>[] membersToExpand) { CheckMapper(); return _mapper.ProjectTo(source, membersToExpand); } }