SugarUnitOfWork.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace SqlSugar
  8. {
  9. public interface ISugarUnitOfWork<T> where T : SugarUnitOfWork, new()
  10. {
  11. ISqlSugarClient Db { get; set; }
  12. T CreateContext(bool isTran=true);
  13. }
  14. public class SugarUnitOfWork<T> : ISugarUnitOfWork<T> where T : SugarUnitOfWork, new()
  15. {
  16. public SugarUnitOfWork(ISqlSugarClient db)
  17. {
  18. this.Db = db;
  19. }
  20. public ISqlSugarClient Db { get; set; }
  21. public T CreateContext(bool isTran=true)
  22. {
  23. return Db.CreateContext<T>(isTran);
  24. }
  25. }
  26. public interface ISugarUnitOfWorkClear
  27. {
  28. RepositoryType GetMyRepository<RepositoryType>() where RepositoryType : new();
  29. bool Commit();
  30. }
  31. /// <summary>
  32. /// ISugarUnitOfWorkClear not exists SqlSugar method
  33. /// ISugarUnitOfWork exists SqlSugar method
  34. /// </summary>
  35. public interface ISugarUnitOfWork : ISugarUnitOfWorkClear
  36. {
  37. ISqlSugarClient Db { get; }
  38. ITenant Tenant { get; }
  39. SimpleClient<T> GetRepository<T>() where T : class, new();
  40. }
  41. /// <summary>
  42. /// SugarUnitOfWork->ISugarUnitOfWork->ISaugarUnitOfWorkClear
  43. /// ISaugarUnitOfWorkClear not exists SqlSugar method
  44. /// ISugarUnitOfWork exists SqlSugar method
  45. /// </summary>
  46. public class SugarUnitOfWork : IDisposable, ISugarUnitOfWork
  47. {
  48. public ISqlSugarClient Db { get; internal set; }
  49. public ITenant Tenant { get; internal set; }
  50. public bool IsTran { get; internal set; }
  51. public bool IsCommit { get; internal set; }
  52. public bool IsClose { get; internal set; }
  53. public void Dispose()
  54. {
  55. if (this.IsTran && IsCommit == false)
  56. {
  57. this.Tenant.RollbackTran();
  58. }
  59. if (this.Db.Ado.Transaction==null&&IsClose == false)
  60. {
  61. this.Db.Close();
  62. }
  63. }
  64. public SimpleClient<T> GetRepository<T>() where T : class, new()
  65. {
  66. TenantAttribute tenantAttribute = typeof(T).GetCustomAttribute<TenantAttribute>();
  67. if (tenantAttribute == null)
  68. {
  69. return new SimpleClient<T>(Db);
  70. }
  71. else
  72. {
  73. return new SimpleClient<T>(Db.AsTenant().GetConnection(tenantAttribute.configId));
  74. }
  75. }
  76. public RepositoryType GetMyRepository<RepositoryType>() where RepositoryType:new()
  77. {
  78. var result = (ISugarRepository)new RepositoryType();
  79. var type = typeof(RepositoryType).GetGenericArguments().FirstOrDefault();
  80. TenantAttribute tenantAttribute = type?.GetCustomAttribute<TenantAttribute>();
  81. if (tenantAttribute == null)
  82. {
  83. result.Context = this.Db;
  84. }
  85. else
  86. {
  87. result.Context = this.Db.AsTenant().GetConnection(tenantAttribute.configId);
  88. }
  89. return (RepositoryType)result;
  90. }
  91. public bool Commit()
  92. {
  93. if (this.IsTran && this.IsCommit == false)
  94. {
  95. this.Tenant.CommitTran();
  96. IsCommit = true;
  97. }
  98. if (this.Db.Ado.Transaction==null&&this.IsClose == false)
  99. {
  100. this.Db.Close();
  101. IsClose = true;
  102. }
  103. return IsCommit;
  104. }
  105. }
  106. }