AutoMapperExt.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using AutoMapper;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace WMS.Util
  10. {
  11. public static class AutoMapperExt
  12. {
  13. private static readonly IMapper _mapper;
  14. static AutoMapperExt()
  15. {
  16. var config = new MapperConfiguration(cfg =>
  17. {
  18. //cfg.CreateMap<SourceClass, DestinationClass>();
  19. //cfg.CreateMap<SubSourceClass, SubDestinationClass>();
  20. });
  21. _mapper = config.CreateMapper();
  22. }
  23. public static TDestination Map<TDestination>(object source)
  24. {
  25. return _mapper.Map<TDestination>(source);
  26. }
  27. public static TDestination Map<TSource, TDestination>(TSource source)
  28. {
  29. return _mapper.Map<TSource, TDestination>(source);
  30. }
  31. public static List<TDestination> MapToList<TDestination>(this IEnumerable source)
  32. {
  33. return _mapper.Map<List<TDestination>>(source);
  34. }
  35. public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source)
  36. {
  37. return _mapper.Map<List<TSource>, List<TDestination>>(source.ToList());
  38. }
  39. /// <summary>
  40. /// DataReader映射
  41. /// </summary>
  42. public static IEnumerable<T> DataReaderMapTo<T>(this IDataReader reader)
  43. {
  44. return _mapper.Map<IDataReader, IEnumerable<T>>(reader);
  45. }
  46. }
  47. }