SqlSugarServiceCollectionExt.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using Microsoft.Extensions.Configuration;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using SqlSugar;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. namespace Wms.Screen.SqlSugar
  8. {
  9. public static class SqlSugarServiceCollectionExt
  10. {
  11. public static IServiceCollection AddSqlSugarClient<T>(this IServiceCollection services, Action<ConnectionConfig> configAction, ServiceLifetime lifetime = ServiceLifetime.Scoped) where T : SqlSugarClient
  12. {
  13. switch (lifetime)
  14. {
  15. case ServiceLifetime.Singleton:
  16. services.AddSingleton(serviceProvider =>
  17. {
  18. var config = new ConnectionConfig()
  19. {
  20. ConfigureExternalServices = new ConfigureExternalServices
  21. {
  22. SqlFuncServices = SqlSugarConfig.GetLambda()
  23. }
  24. };
  25. configAction.Invoke(config);
  26. //var log = serviceProvider.GetRequiredService<ILogUtil>();
  27. var db = new SqlSugarClient(config)
  28. {
  29. MappingTables = SqlSugarConfig.listTable
  30. };
  31. var configuration = serviceProvider.GetRequiredService<IConfiguration>();
  32. string flag = configuration["Log:sqllog"];
  33. if (string.IsNullOrWhiteSpace(flag))
  34. {
  35. flag = "false";
  36. }
  37. if (flag.Equals("true", StringComparison.OrdinalIgnoreCase))
  38. {
  39. db.Ado.IsEnableLogEvent = true;
  40. //SQL执行前事件
  41. db.Aop.OnLogExecuting = (sql, pars) =>
  42. {
  43. foreach (var item in pars)
  44. {
  45. sql = sql.Replace(item.ParameterName.ToString(), $"'{item.Value?.ToString()}'");
  46. }
  47. //log.Info($"执行前SQL: {sql}");
  48. //log.Info(db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
  49. };
  50. //SQL执行完事件
  51. db.Aop.OnLogExecuted = (sql, pars) =>
  52. {
  53. foreach (var item in pars)
  54. {
  55. sql = sql.Replace(item.ParameterName.ToString(), $"'{item.Value?.ToString()}'");
  56. }
  57. //log.Info($"执行后SQL: {sql}");
  58. };
  59. db.Aop.OnError = (exp) =>//执行SQL 错误事件
  60. {
  61. //log.Debug(exp, exp.Sql);
  62. };
  63. }
  64. else
  65. {
  66. db.Ado.IsEnableLogEvent = false;
  67. }
  68. return (T)db;
  69. });
  70. break;
  71. case ServiceLifetime.Scoped:
  72. services.AddScoped(serviceProvider =>
  73. {
  74. var configS = new ConnectionConfig()
  75. {
  76. ConfigureExternalServices = new ConfigureExternalServices
  77. {
  78. SqlFuncServices = SqlSugarConfig.GetLambda()
  79. }
  80. };
  81. configAction.Invoke(configS);
  82. var db = new SqlSugarClient(configS)
  83. {
  84. MappingTables = SqlSugarConfig.listTable
  85. };
  86. //var log = serviceProvider.GetRequiredService<ILogUtil>();
  87. var configuration = serviceProvider.GetRequiredService<IConfiguration>();
  88. string flag = configuration["Log:sqllog"];
  89. if (string.IsNullOrWhiteSpace(flag))
  90. {
  91. flag = "false";
  92. }
  93. if (flag.Equals("true", StringComparison.OrdinalIgnoreCase))
  94. {
  95. db.Ado.IsEnableLogEvent = true;
  96. //SQL执行前事件
  97. db.Aop.OnLogExecuting = (sql, pars) =>
  98. {
  99. foreach (var item in pars)
  100. {
  101. sql = sql.Replace(item.ParameterName.ToString(), $"'{item.Value?.ToString()}'");
  102. }
  103. //log.Info($"执行前SQL: {sql}");
  104. //log.Info(db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
  105. };
  106. //SQL执行完事件
  107. db.Aop.OnLogExecuted = (sql, pars) =>
  108. {
  109. foreach (var item in pars)
  110. {
  111. sql = sql.Replace(item.ParameterName.ToString(), $"'{item.Value?.ToString()}'");
  112. }
  113. //log.Info($"执行后SQL: {sql}");
  114. };
  115. db.Aop.OnError = (exp) =>//执行SQL 错误事件
  116. {
  117. //log.Debug(exp, exp.Sql);
  118. };
  119. }
  120. else
  121. {
  122. db.Ado.IsEnableLogEvent = false;
  123. }
  124. return (T)db;
  125. });
  126. break;
  127. case ServiceLifetime.Transient:
  128. services.AddTransient(serviceProvider =>
  129. {
  130. var configT = new ConnectionConfig()
  131. {
  132. ConfigureExternalServices = new ConfigureExternalServices
  133. {
  134. SqlFuncServices = SqlSugarConfig.GetLambda()
  135. }
  136. };
  137. configAction.Invoke(configT);
  138. var db = new SqlSugarClient(configT)
  139. {
  140. MappingTables = SqlSugarConfig.listTable
  141. };
  142. //var log = serviceProvider.GetRequiredService<ILogUtil>();
  143. var configuration = serviceProvider.GetRequiredService<IConfiguration>();
  144. string flag = configuration["Log:sqllog"];
  145. if (string.IsNullOrWhiteSpace(flag))
  146. {
  147. flag = "false";
  148. }
  149. if (flag.Equals("true", StringComparison.OrdinalIgnoreCase))
  150. {
  151. db.Ado.IsEnableLogEvent = true;
  152. //SQL执行前事件
  153. db.Aop.OnLogExecuting = (sql, pars) =>
  154. {
  155. foreach (var item in pars)
  156. {
  157. sql = sql.Replace(item.ParameterName.ToString(), $"'{item.Value?.ToString()}'");
  158. }
  159. //log.Info($"执行前SQL: {sql}");
  160. //log.Info(db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
  161. };
  162. //SQL执行完事件
  163. db.Aop.OnLogExecuted = (sql, pars) =>
  164. {
  165. foreach (var item in pars)
  166. {
  167. sql = sql.Replace(item.ParameterName.ToString(), $"'{item.Value?.ToString()}'");
  168. }
  169. //log.Info($"执行后SQL: {sql}");
  170. };
  171. db.Aop.OnError = (exp) =>//执行SQL 错误事件
  172. {
  173. //log.Debug(exp, exp.Sql);
  174. };
  175. }
  176. else
  177. {
  178. db.Ado.IsEnableLogEvent = false;
  179. }
  180. return (T)db;
  181. });
  182. break;
  183. }
  184. return services;
  185. }
  186. }
  187. }