Нет описания

GenericRepository.cs 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using Backend.ApplicationCore.Interfaces.IRepositories;
  2. using Microsoft.EntityFrameworkCore;
  3. using Microsoft.EntityFrameworkCore.ChangeTracking;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace Backend.Infrastructure.Repositories
  11. {
  12. public class GenericRepository<TEntity> : IDisposable, IGenericRepository<TEntity> where TEntity : class
  13. {
  14. protected readonly IApplicationDatabaseContext _context;
  15. protected readonly DbSet<TEntity> _dbSet;
  16. public GenericRepository(IApplicationDatabaseContext context)
  17. {
  18. _context = context;
  19. _dbSet = context.Set<TEntity>();
  20. }
  21. /// <summary>
  22. /// FindAsync find entity by Id
  23. /// </summary>
  24. /// <param name="id"></param>
  25. /// <returns></returns>
  26. public virtual async Task<TEntity> FindAsync(object id)
  27. {
  28. _dbSet.AsNoTracking();
  29. return await _dbSet.FindAsync(id);
  30. }
  31. /// <summary>
  32. /// FindAllAsync find all entries Without pagination
  33. /// </summary>
  34. /// <returns></returns>
  35. public virtual async Task<IEnumerable<TEntity>> FindAllAsync()
  36. {
  37. using var findTask = Task.FromResult(_dbSet.AsEnumerable());
  38. return await findTask;
  39. }
  40. /// <summary>
  41. /// CountAsync - Count entries
  42. /// </summary>
  43. /// <returns></returns>
  44. public virtual async Task<int> CountAsync()
  45. {
  46. using var countTask = _dbSet.CountAsync();
  47. return await countTask;
  48. }
  49. /// <summary>
  50. /// AddAsync - use to add an Entity without relationship
  51. /// </summary>
  52. /// <param name="entity"></param>
  53. /// <returns></returns>
  54. public virtual async Task<TEntity> AddAsync(TEntity entity)
  55. {
  56. await _dbSet.AddAsync(entity);
  57. return entity;
  58. }
  59. /// <summary>
  60. /// UpdateAsync update an Entity
  61. /// </summary>
  62. /// <param name="entity"></param>
  63. /// <returns></returns>
  64. public virtual async Task<TEntity> UpdateAsync(TEntity entity)
  65. {
  66. using var updateTask = Task.FromResult(_dbSet.Update(entity));
  67. await updateTask;
  68. return entity;
  69. }
  70. public virtual EntityEntry<TEntity> Entry(TEntity entity, TEntity ExistEntity)
  71. {
  72. _context.Entry(ExistEntity).State = EntityState.Detached;
  73. _context.Entry(ExistEntity).CurrentValues.SetValues(entity);
  74. return _context.Entry(entity);
  75. }
  76. /// <summary>
  77. /// DeleteAsync delete en antity by Id
  78. /// </summary>
  79. /// <param name="id"></param>
  80. /// <returns></returns>
  81. public virtual async Task DeleteAsync(Guid id)
  82. {
  83. var entity = await FindAsync(id);
  84. await DeleteAsync(entity);
  85. }
  86. public virtual async Task DeleteAsync(int id)
  87. {
  88. var entity = await FindAsync(id);
  89. await DeleteAsync(entity);
  90. }
  91. /// <summary>
  92. /// DeleteAsync delete entity
  93. /// </summary>
  94. /// <param name="entity"></param>
  95. /// <returns></returns>
  96. public virtual async Task DeleteAsync(TEntity entity)
  97. {
  98. using var removeTask = Task.FromResult(_dbSet.Remove(entity));
  99. await removeTask;
  100. }
  101. /// <summary>
  102. /// SaveChangesAsync save dbContext changes
  103. /// </summary>
  104. /// <param name="cancellationToken"></param>
  105. /// <returns></returns>
  106. public virtual async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
  107. {
  108. using var saveChangeTask = _context.SaveChangesAsync(cancellationToken);
  109. return await saveChangeTask;
  110. }
  111. /// <summary>
  112. /// Dispose
  113. /// </summary>
  114. public void Dispose()
  115. {
  116. _context?.Dispose();
  117. }
  118. }
  119. }

Powered by TurnKey Linux.