DB.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace WCS.Core.DbHelper
  6. {
  7. /// <summary>
  8. /// DB,禁止跨上下文使用
  9. /// 1.异步情况: 在同一串await 中是一个上下文 (await 会改变线程和同步是不一样的)
  10. /// 2.同步情况: 在同一个线程是同一个上下文
  11. /// </summary>
  12. public class Db
  13. {
  14. /// <summary>
  15. /// 上下文集合
  16. /// </summary>
  17. private static List<ContextList> _contexts = new List<ContextList>();
  18. /// <summary>
  19. /// 默认上下文类类型
  20. /// </summary>
  21. public static string DefaultDbContextType { get; private set; } = null!;
  22. /// <summary>
  23. ///
  24. /// </summary>
  25. /// <typeparam name="T">业务代码</typeparam>
  26. /// <param name="func">业务逻辑</param>
  27. /// <returns></returns>
  28. /// <exception cref="Exception"></exception>
  29. public static T Do<T>(Func<Db, T> func)
  30. {
  31. var db = new Db();
  32. db.Default.BeginTran();
  33. var res = func(db);
  34. db.Default.CommitTran();
  35. return res;
  36. }
  37. public static void Do(Action<Db> act)
  38. {
  39. Do(db =>
  40. {
  41. act(db);
  42. return 1;
  43. });
  44. }
  45. /// <summary>
  46. /// 设置默认链接
  47. /// </summary>
  48. /// <param name="key"></param>
  49. public static void SetDefaultDbContextType(string key)
  50. {
  51. DefaultDbContextType = key;
  52. }
  53. /// <summary>
  54. /// 默认链接
  55. /// </summary>
  56. public SqlSugarScope Default
  57. {
  58. get
  59. {
  60. if (DefaultDbContextType == null)
  61. throw new Exception("请先设置默认数据库,调用静态方法SetDefaultDbContextType()");
  62. return Context(DefaultDbContextType);
  63. }
  64. }
  65. /// <summary>
  66. /// 创建一个上下文
  67. /// </summary>
  68. /// <param name="type"></param>
  69. /// <returns></returns>
  70. public static SqlSugarScope CreateContext(ConnectionConfig config, string key)
  71. {
  72. var ctx = _contexts.FirstOrDefault(v => v.Key == key);
  73. if (ctx != null) return ctx.Client;
  74. ctx = new ContextList(key, new SqlSugarScope(new ConnectionConfig()
  75. {
  76. ConnectionString = config.ConnectionString,
  77. DbType = config.DbType,
  78. IsAutoCloseConnection = true
  79. }), config);
  80. _contexts.Add(ctx);
  81. return ctx.Client;
  82. }
  83. public SqlSugarScope Context(string key)
  84. {
  85. var ctx = _contexts.FirstOrDefault(v => v.Key == key);
  86. if (ctx == null) throw new Exception("没有对应的链接,请先调用创建");
  87. return ctx.Client;
  88. }
  89. }
  90. /// <summary>
  91. /// 链接
  92. /// </summary>
  93. public class ContextList
  94. {
  95. public ContextList(string key, SqlSugarScope client, ConnectionConfig connectionConfig)
  96. {
  97. Key = key;
  98. Client = client;
  99. ConnectionConfig = connectionConfig;
  100. }
  101. public string Key { get; set; }
  102. public SqlSugarScope Client { get; set; }
  103. public ConnectionConfig ConnectionConfig { get; set; }
  104. }
  105. }