RepositoryBase.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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(IFreeSql orm, IUnitOfWork uow, IUser user) : base(orm, null, null)
  13. {
  14. uow.Close();
  15. UnitOfWork = uow;
  16. _user = user;
  17. }
  18. public virtual Task<TDto> GetAsync<TDto>(TKey id)
  19. {
  20. return Select.WhereDynamic(id).ToOneAsync<TDto>();
  21. }
  22. public virtual Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> exp)
  23. {
  24. return Select.Where(exp).ToOneAsync();
  25. }
  26. public virtual Task<TDto> GetAsync<TDto>(Expression<Func<TEntity, bool>> exp)
  27. {
  28. return Select.Where(exp).ToOneAsync<TDto>();
  29. }
  30. public async Task<bool> SoftDeleteAsync(TKey id)
  31. {
  32. await UpdateDiy
  33. .SetDto(new { IsDeleted = true, ModifiedUserId = _user.Id, ModifiedUserName = _user.Name })
  34. .WhereDynamic(id)
  35. .ExecuteAffrowsAsync();
  36. return true;
  37. }
  38. public async Task<bool> SoftDeleteAsync(TKey[] ids)
  39. {
  40. await UpdateDiy
  41. .SetDto(new { IsDeleted = true, ModifiedUserId = _user.Id, ModifiedUserName = _user.Name })
  42. .WhereDynamic(ids)
  43. .ExecuteAffrowsAsync();
  44. return true;
  45. }
  46. }
  47. public abstract class RepositoryBase<TEntity> : RepositoryBase<TEntity, long> where TEntity : class, new()
  48. {
  49. protected RepositoryBase(IFreeSql orm, IUnitOfWork uow, IUser user) : base(orm, uow, user)
  50. {
  51. }
  52. }
  53. }