RepositoryBase.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 
  2. using System;
  3. using System.Threading.Tasks;
  4. using System.Linq.Expressions;
  5. using FreeSql;
  6. using Admin.Core.Common.Auth;
  7. namespace Admin.Core.Repository
  8. {
  9. public abstract class RepositoryBase<TEntity,TKey> : BaseRepository<TEntity, TKey> where TEntity : class,new()
  10. {
  11. private readonly IUser _user;
  12. protected RepositoryBase(UnitOfWorkManager uowm, IUser user) : base(uowm.Orm, null, null)
  13. {
  14. uowm.Binding(this);
  15. _user = user;
  16. }
  17. public virtual Task<TDto> GetAsync<TDto>(TKey id)
  18. {
  19. return Select.WhereDynamic(id).ToOneAsync<TDto>();
  20. }
  21. public virtual Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> exp)
  22. {
  23. return Select.Where(exp).ToOneAsync();
  24. }
  25. public virtual Task<TDto> GetAsync<TDto>(Expression<Func<TEntity, bool>> exp)
  26. {
  27. return Select.Where(exp).ToOneAsync<TDto>();
  28. }
  29. public async Task<bool> SoftDeleteAsync(TKey id)
  30. {
  31. await UpdateDiy
  32. .SetDto(new { IsDeleted = true, ModifiedUserId = _user.Id, ModifiedUserName = _user.Name })
  33. .WhereDynamic(id)
  34. .ExecuteAffrowsAsync();
  35. return true;
  36. }
  37. public async Task<bool> SoftDeleteAsync(TKey[] ids)
  38. {
  39. await UpdateDiy
  40. .SetDto(new { IsDeleted = true, ModifiedUserId = _user.Id, ModifiedUserName = _user.Name })
  41. .WhereDynamic(ids)
  42. .ExecuteAffrowsAsync();
  43. return true;
  44. }
  45. }
  46. public abstract class RepositoryBase<TEntity> : RepositoryBase<TEntity, long> where TEntity : class, new()
  47. {
  48. protected RepositoryBase(UnitOfWorkManager uowm, IUser user) : base(uowm, user)
  49. {
  50. }
  51. }
  52. }