MapperGuidExt.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Quadarax.Foundation.Core.Data.Interface.Entity;
  5. namespace Quadarax.Foundation.Core.Data.Mapper
  6. {
  7. public static class MapperGuidExt
  8. {
  9. public static TDto Map<TDao, TDto>(this TDao dao)
  10. where TDao : class, IDao<Guid>, new()
  11. where TDto : class, IDto, new()
  12. {
  13. if (dao == null) return null;
  14. return Mapper<MapperGuid>.Instance.Map<TDao, TDto>(dao);
  15. }
  16. public static TDao MapDto<TDto, TDao>(this TDto dto)
  17. where TDao : class, IDao<Guid>, new()
  18. where TDto : class, IDto, new()
  19. {
  20. if (dto == null) return null;
  21. return Mapper<MapperGuid>.Instance.Map<TDto, TDao>(dto);
  22. }
  23. public static void CopyToDto<TDto, TDao>(this TDto @from, TDao to)
  24. where TDao : class, IDao<Guid>, new()
  25. where TDto : class, IDto, new()
  26. {
  27. if (@from == null) return;
  28. if (to == null) return;
  29. Mapper<MapperGuid>.Instance.Update(@from,to);
  30. }
  31. public static void CopyToDto<TDto, TDao>(this TDao @from, TDto to)
  32. where TDao : class, IDao<Guid>, new()
  33. where TDto : class, IDto, new()
  34. {
  35. if (to == null) return;
  36. if (@from == null) return;
  37. Mapper<MapperGuid>.Instance.Update(@from,to);
  38. }
  39. public static List<TDto> MapList<TDao, TDto>(this IQueryable<TDao> dao)
  40. where TDao : class, IDao<Guid>, new()
  41. where TDto : class, IDto, new()
  42. {
  43. if (dao == null) return new List<TDto>();
  44. return Mapper<MapperGuid>.Instance.MapList<TDao, TDto>(dao);
  45. }
  46. public static List<TDto> MapList<TDao, TDto>(this IEnumerable<TDao> dao)
  47. where TDao : class, IDao<Guid>, new()
  48. where TDto : class, IDto, new()
  49. {
  50. if (dao == null) return new List<TDto>();
  51. return Mapper<MapperGuid>.Instance.MapList<TDao, TDto>(dao);
  52. }
  53. public static IQueryable<TDao> MapListDto<TDto,TDao>(this IEnumerable<TDto> dto)
  54. where TDao : class, IDao<Guid>, new()
  55. where TDto : class, IDto, new()
  56. {
  57. if (dto == null) return new List<TDao>().AsQueryable();
  58. return Mapper<MapperGuid>.Instance.MapList<TDto, TDao>(dto.AsQueryable()).AsQueryable();
  59. }
  60. }
  61. }