RepositoryBase.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 {
  33. IsDeleted = true,
  34. ModifiedUserId = _user.Id,
  35. ModifiedUserName = _user.Name
  36. })
  37. .WhereDynamic(id)
  38. .ExecuteAffrowsAsync();
  39. return true;
  40. }
  41. public async Task<bool> SoftDeleteAsync(TKey[] ids)
  42. {
  43. await UpdateDiy
  44. .SetDto(new {
  45. IsDeleted = true,
  46. ModifiedUserId = _user.Id,
  47. ModifiedUserName = _user.Name
  48. })
  49. .WhereDynamic(ids)
  50. .ExecuteAffrowsAsync();
  51. return true;
  52. }
  53. }
  54. public abstract class RepositoryBase<TEntity> : RepositoryBase<TEntity, long> where TEntity : class, new()
  55. {
  56. protected RepositoryBase(UnitOfWorkManager uowm, IUser user) : base(uowm, user)
  57. {
  58. }
  59. }
  60. }