平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SqlSugarUnitOfWork.cs 2.1 KiB

4 月之前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 
  2. //
  3. namespace SafeCampus.SqlSugar;
  4. /// <summary>
  5. /// SqlSugar 事务和工作单元
  6. /// </summary>
  7. public sealed class SqlSugarUnitOfWork : IUnitOfWork
  8. {
  9. /// <summary>
  10. /// SqlSugar 对象
  11. /// </summary>
  12. private readonly ISqlSugarClient _sqlSugarClient;
  13. private readonly ILogger<SqlSugarUnitOfWork> _logger;
  14. /// <summary>
  15. /// 构造函数
  16. /// </summary>
  17. /// <param name="sqlSugarClient"></param>
  18. /// <param name="logger"></param>
  19. public SqlSugarUnitOfWork(ISqlSugarClient sqlSugarClient, ILogger<SqlSugarUnitOfWork> logger)
  20. {
  21. _sqlSugarClient = sqlSugarClient;
  22. _logger = logger;
  23. }
  24. /// <summary>
  25. /// 开启工作单元处理
  26. /// </summary>
  27. /// <param name="context"></param>
  28. /// <param name="unitOfWork"></param>
  29. /// <exception cref="NotImplementedException"></exception>
  30. public void BeginTransaction(FilterContext context, UnitOfWorkAttribute unitOfWork)
  31. {
  32. _sqlSugarClient.AsTenant().BeginTran();
  33. }
  34. /// <summary>
  35. /// 提交工作单元处理
  36. /// </summary>
  37. /// <param name="resultContext"></param>
  38. /// <param name="unitOfWork"></param>
  39. /// <exception cref="NotImplementedException"></exception>
  40. public void CommitTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
  41. {
  42. _sqlSugarClient.AsTenant().CommitTran();
  43. }
  44. /// <summary>
  45. /// 回滚工作单元处理
  46. /// </summary>
  47. /// <param name="resultContext"></param>
  48. /// <param name="unitOfWork"></param>
  49. /// <exception cref="NotImplementedException"></exception>
  50. public void RollbackTransaction(FilterContext resultContext, UnitOfWorkAttribute unitOfWork)
  51. {
  52. _sqlSugarClient.AsTenant().RollbackTran();
  53. }
  54. /// <summary>
  55. /// 执行完毕(无论成功失败)
  56. /// </summary>
  57. /// <param name="context"></param>
  58. /// <param name="resultContext"></param>
  59. /// <exception cref="NotImplementedException"></exception>
  60. public void OnCompleted(FilterContext context, FilterContext resultContext)
  61. {
  62. _sqlSugarClient.Dispose();
  63. }
  64. }