DB.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using SqlSugar;
  2. namespace DBHelper
  3. {
  4. /// <summary>
  5. /// DB,禁止跨上下文使用
  6. /// 1.异步情况: 在同一串await 中是一个上下文 (await 会改变线程和同步是不一样的)
  7. /// 2.同步情况: 在同一个线程是同一个上下文
  8. /// </summary>
  9. public class Db
  10. {
  11. /// <summary>
  12. /// 上下文集合
  13. /// </summary>
  14. private static readonly List<ContextList> _contexts = new List<ContextList>();
  15. /// <summary>
  16. /// 默认上下文类类型
  17. /// </summary>
  18. public static string DefaultDbContextType { get; private set; } = null!;
  19. /// <summary>
  20. /// 指定连接连接
  21. /// </summary>
  22. /// <typeparam name="T">业务代码</typeparam>
  23. /// <param name="func">业务逻辑</param>
  24. /// <param name="key">连接对应的Key</param>
  25. /// <returns></returns>
  26. /// <exception cref="Exception"></exception>
  27. public static T Do<T>(Func<Db, T> func, string key)
  28. {
  29. var db = new Db();
  30. db.Context(key).BeginTran();
  31. var res = func(db);
  32. db.Context(key).CommitTran();
  33. return res;
  34. }
  35. /// <summary>
  36. /// 使用设置好的默认链接
  37. /// </summary>
  38. /// <param name="act">执行内容</param>
  39. public static void Do(Action<Db> act)
  40. {
  41. Do(db =>
  42. {
  43. act(db);
  44. return 1;
  45. }, DefaultDbContextType);
  46. }
  47. /// <summary>
  48. /// 使用指定默认链接
  49. /// </summary>
  50. /// <param name="act">执行内容</param>
  51. /// <param name="key">连接对应的Key</param>
  52. public static void Do(Action<Db> act, string key)
  53. {
  54. Do(db =>
  55. {
  56. act(db);
  57. return 1;
  58. }, key);
  59. }
  60. /// <summary>
  61. /// 设置默认链接
  62. /// </summary>
  63. /// <param name="key"></param>
  64. public static void SetDefaultDbContextType(string key)
  65. {
  66. DefaultDbContextType = key;
  67. }
  68. /// <summary>
  69. /// 默认链接
  70. /// </summary>
  71. public SqlSugarScope Default
  72. {
  73. get
  74. {
  75. if (DefaultDbContextType == null)
  76. throw new Exception("请先设置默认数据库,调用静态方法SetDefaultDbContextType()");
  77. return Context(DefaultDbContextType);
  78. }
  79. }
  80. /// <summary>
  81. /// 创建一个数据库连接
  82. /// </summary>
  83. /// <param name="config">数据库配置信息</param>
  84. /// <param name="key">数据库Key</param>
  85. /// <returns></returns>
  86. public static SqlSugarScope CreateContext(ConnectionConfig config, string key)
  87. {
  88. var ctx = _contexts.FirstOrDefault(v => v.Key == key);
  89. if (ctx != null) return ctx.Client;
  90. ctx = new ContextList(key, new SqlSugarScope(new ConnectionConfig()
  91. {
  92. ConnectionString = config.ConnectionString,
  93. DbType = config.DbType,
  94. IsAutoCloseConnection = true
  95. }), config);
  96. _contexts.Add(ctx);
  97. return ctx.Client;
  98. }
  99. /// <summary>
  100. /// 根据Key获取指定数据库连接
  101. /// </summary>
  102. /// <param name="key">目标数据库在配置中的Key</param>
  103. /// <returns></returns>
  104. /// <exception cref="Exception"></exception>
  105. public SqlSugarScope Context(string key)
  106. {
  107. var ctx = _contexts.FirstOrDefault(v => v.Key == key);
  108. if (ctx == null) throw new Exception("没有对应的链接,请先调用创建");
  109. return ctx.Client;
  110. }
  111. }
  112. /// <summary>
  113. /// 链接
  114. /// </summary>
  115. public class ContextList
  116. {
  117. /// <summary>
  118. ///
  119. /// </summary>
  120. /// <param name="key"></param>
  121. /// <param name="client"></param>
  122. /// <param name="connectionConfig"></param>
  123. public ContextList(string key, SqlSugarScope client, ConnectionConfig connectionConfig)
  124. {
  125. Key = key;
  126. Client = client;
  127. ConnectionConfig = connectionConfig;
  128. }
  129. /// <summary>
  130. ///
  131. /// </summary>
  132. public string Key { get; set; }
  133. /// <summary>
  134. ///
  135. /// </summary>
  136. public SqlSugarScope Client { get; set; }
  137. /// <summary>
  138. ///
  139. /// </summary>
  140. public ConnectionConfig ConnectionConfig { get; set; }
  141. }
  142. }