MapperGuidExt.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. return Mapper<MapperGuid>.Instance.Map<TDao, TDto>(dao);
  14. }
  15. public static TDao? MapDto<TDto, TDao>(this TDto dto)
  16. where TDao : class, IDao<Guid>, new()
  17. where TDto : class, IDto, new()
  18. {
  19. return Mapper<MapperGuid>.Instance.Map<TDto, TDao>(dto);
  20. }
  21. public static void CopyToDto<TDto, TDao>(this TDto @from, TDao? to)
  22. where TDao : class, IDao<Guid>, new()
  23. where TDto : class, IDto, new()
  24. {
  25. if (to == null) return;
  26. Mapper<MapperGuid>.Instance.Update(@from,to);
  27. }
  28. public static void CopyToDto<TDto, TDao>(this TDao @from, TDto? to)
  29. where TDao : class, IDao<Guid>, new()
  30. where TDto : class, IDto, new()
  31. {
  32. if (to == null) return;
  33. Mapper<MapperGuid>.Instance.Update(@from,to);
  34. }
  35. public static List<TDto>? MapList<TDao, TDto>(this IQueryable<TDao> dao)
  36. where TDao : class, IDao<Guid>, new()
  37. where TDto : class, IDto, new()
  38. {
  39. return Mapper<MapperGuid>.Instance.MapList<TDao, TDto>(dao);
  40. }
  41. public static List<TDto>? MapList<TDao, TDto>(this IEnumerable<TDao> dao)
  42. where TDao : class, IDao<Guid>, new()
  43. where TDto : class, IDto, new()
  44. {
  45. return Mapper<MapperGuid>.Instance.MapList<TDao, TDto>(dao);
  46. }
  47. public static IQueryable<TDao> MapListDto<TDto,TDao>(this IEnumerable<TDto> dto)
  48. where TDao : class, IDao<Guid>, new()
  49. where TDto : class, IDto, new()
  50. {
  51. return (Mapper<MapperGuid>.Instance.MapList<TDto, TDao>(dto?.AsQueryable()) ?? throw new InvalidOperationException()).AsQueryable();
  52. }
  53. }
  54. }