Repository.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Linq.Expressions;
  6. using System.Data.Entity;
  7. using System.Data.Entity.Infrastructure;
  8. using Bowin.Common.Data;
  9. using Bowin.Common.Linq.Entity;
  10. namespace EMIS.DataLogic.Repositories.HRService
  11. {
  12. public class Repository<TEntity> where TEntity : class
  13. {
  14. private HRUnitOfWork _unitOfWork;
  15. private Repository()
  16. {
  17. }
  18. public Repository(HRUnitOfWork unitOfWork)
  19. {
  20. this._unitOfWork = unitOfWork;
  21. }
  22. public TEntity GetSingle(Expression<Func<TEntity, bool>> expression)
  23. {
  24. return this.Entities.Where<TEntity>(expression).FirstOrDefault<TEntity>();
  25. }
  26. public TEntity GetSingle(Expression<Func<TEntity, bool>> expression, params string[] paths)
  27. {
  28. IQueryable<TEntity> source = this.Entities;//.Include(path)
  29. foreach (var path in paths)
  30. {
  31. source = source.Include(path);
  32. }
  33. return source.Where<TEntity>(expression).FirstOrDefault<TEntity>();
  34. }
  35. public TEntity GetSingle(Expression<Func<TEntity, bool>> expression, params Expression<Func<TEntity, object>>[] paths)
  36. {
  37. IQueryable<TEntity> source = this.Entities;//.Include(path)
  38. foreach (var path in paths)
  39. {
  40. source = source.Include(path);
  41. }
  42. return source.Where<TEntity>(expression).FirstOrDefault<TEntity>();
  43. }
  44. public IQueryable<TEntity> Entities
  45. {
  46. get
  47. {
  48. return this._unitOfWork.Set<TEntity>();
  49. }
  50. }
  51. public HRUnitOfWork UnitOfWork
  52. {
  53. get
  54. {
  55. return this._unitOfWork;
  56. }
  57. }
  58. public GridResultSet<TEntity> GetPagedList<S>(Expression<Func<TEntity, bool>> expression, int pageIndex, int pageSize, Expression<Func<TEntity, S>> orderBy, bool descending, params Expression<Func<TEntity, object>>[] paths)
  59. {
  60. IQueryable<TEntity> source = this.Entities;//.Include(path)
  61. foreach (var path in paths)
  62. {
  63. source = source.Include(path);
  64. }
  65. source = source.Where<TEntity>(expression);
  66. source = descending ? source.OrderByDescending<TEntity, S>(orderBy) : source.OrderBy<TEntity, S>(orderBy);
  67. GridResultSet<TEntity> gridResult = new GridResultSet<TEntity>();
  68. gridResult.rows = source.Skip<TEntity>((pageIndex) * pageSize).Take<TEntity>(pageSize).ToList();
  69. gridResult.total = source.Count();
  70. return gridResult;
  71. }
  72. public GridResultSet<TEntity> GetPagedList<S>(Expression<Func<TEntity, bool>> expression, int pageIndex, int pageSize, Expression<Func<TEntity, S>> orderBy, bool descending)
  73. {
  74. try
  75. {
  76. IQueryable<TEntity> source = this.Entities.Where<TEntity>(expression);
  77. source = descending ? source.OrderByDescending<TEntity, S>(orderBy) : source.OrderBy<TEntity, S>(orderBy);
  78. GridResultSet<TEntity> gridResult = new GridResultSet<TEntity>();
  79. gridResult.rows = source.Skip<TEntity>((pageIndex) * pageSize).Take<TEntity>(pageSize).ToList();
  80. gridResult.total = source.Count();
  81. return gridResult;
  82. }
  83. catch (Exception ex)
  84. {
  85. throw ex;
  86. }
  87. }
  88. public IQueryable<TEntity> GetList<S>(Expression<Func<TEntity, bool>> expression, Expression<Func<TEntity, S>> orderBy, bool descending)
  89. {
  90. var source = GetList(expression);
  91. return descending ? source.OrderByDescending<TEntity, S>(orderBy) : source.OrderBy<TEntity, S>(orderBy);
  92. }
  93. public IQueryable<TEntity> GetList(Expression<Func<TEntity, bool>> expression)
  94. {
  95. return this.Entities.Where<TEntity>(expression);
  96. }
  97. public IQueryable<TEntity> GetList(Expression<Func<TEntity, bool>> expression, params string[] paths)
  98. {
  99. IQueryable<TEntity> source = this.Entities;//.Include(path)
  100. foreach (var path in paths)
  101. {
  102. source = source.Include(path);
  103. }
  104. return source.Where<TEntity>(expression);
  105. }
  106. public IQueryable<TEntity> GetList(Expression<Func<TEntity, bool>> expression, params Expression<Func<TEntity, object>>[] paths)
  107. {
  108. IQueryable<TEntity> source = this.Entities;//.Include(path)
  109. foreach (var path in paths)
  110. {
  111. source = source.Include(path);
  112. }
  113. return source.Where<TEntity>(expression);
  114. }
  115. }
  116. }