Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. public abstract class BaseRepository<T> : IRepository<T> where T : class
  2. {
  3. internal readonly TinRollContext context;
  4.  
  5. public BaseRepository(TinRollContext context)
  6. {
  7. this.context = context;
  8. }
  9.  
  10. public async Task<T> CreateAsync(T entity)
  11. {
  12. await context.AddAsync<T>(entity);
  13. await context.SaveChangesAsync();
  14. return entity;
  15. }
  16.  
  17. public async Task<T> GetAsync(int id)
  18. {
  19. return await context.FindAsync<T>(id);
  20. }
  21.  
  22. public async Task<IEnumerable<T>> GetAsync(
  23. Expression<Func<T, bool>> filter = null,
  24. Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
  25. string includeProperties = "")
  26. {
  27. IQueryable<T> query = context.Set<T>();
  28.  
  29. if (filter != null)
  30. {
  31. query = query.Where(filter);
  32. }
  33.  
  34. if (includeProperties != null)
  35. {
  36. foreach (var includeProperty in includeProperties.Split
  37. (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
  38. {
  39. query = query.Include(includeProperty);
  40. }
  41. }
  42.  
  43.  
  44. if (orderBy != null)
  45. {
  46. return await orderBy(query).ToListAsync();
  47. }
  48. else
  49. {
  50. return await query.ToListAsync();
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement