123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Wms.Screen.SqlSugar
- {
- public static class SqlSugarServiceCollectionExt
- {
- public static IServiceCollection AddSqlSugarClient<T>(this IServiceCollection services, Action<ConnectionConfig> configAction, ServiceLifetime lifetime = ServiceLifetime.Scoped) where T : SqlSugarClient
- {
- switch (lifetime)
- {
- case ServiceLifetime.Singleton:
- services.AddSingleton(serviceProvider =>
- {
- var config = new ConnectionConfig()
- {
- ConfigureExternalServices = new ConfigureExternalServices
- {
- SqlFuncServices = SqlSugarConfig.GetLambda()
- }
- };
- configAction.Invoke(config);
- //var log = serviceProvider.GetRequiredService<ILogUtil>();
- var db = new SqlSugarClient(config)
- {
- MappingTables = SqlSugarConfig.listTable
- };
- var configuration = serviceProvider.GetRequiredService<IConfiguration>();
- string flag = configuration["Log:sqllog"];
- if (string.IsNullOrWhiteSpace(flag))
- {
- flag = "false";
- }
- if (flag.Equals("true", StringComparison.OrdinalIgnoreCase))
- {
- db.Ado.IsEnableLogEvent = true;
- //SQL执行前事件
- db.Aop.OnLogExecuting = (sql, pars) =>
- {
- foreach (var item in pars)
- {
- sql = sql.Replace(item.ParameterName.ToString(), $"'{item.Value?.ToString()}'");
- }
- //log.Info($"执行前SQL: {sql}");
- //log.Info(db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
- };
- //SQL执行完事件
- db.Aop.OnLogExecuted = (sql, pars) =>
- {
- foreach (var item in pars)
- {
- sql = sql.Replace(item.ParameterName.ToString(), $"'{item.Value?.ToString()}'");
- }
- //log.Info($"执行后SQL: {sql}");
- };
- db.Aop.OnError = (exp) =>//执行SQL 错误事件
- {
- //log.Debug(exp, exp.Sql);
- };
- }
- else
- {
- db.Ado.IsEnableLogEvent = false;
- }
- return (T)db;
- });
- break;
- case ServiceLifetime.Scoped:
- services.AddScoped(serviceProvider =>
- {
- var configS = new ConnectionConfig()
- {
- ConfigureExternalServices = new ConfigureExternalServices
- {
- SqlFuncServices = SqlSugarConfig.GetLambda()
- }
- };
- configAction.Invoke(configS);
- var db = new SqlSugarClient(configS)
- {
- MappingTables = SqlSugarConfig.listTable
- };
- //var log = serviceProvider.GetRequiredService<ILogUtil>();
- var configuration = serviceProvider.GetRequiredService<IConfiguration>();
- string flag = configuration["Log:sqllog"];
- if (string.IsNullOrWhiteSpace(flag))
- {
- flag = "false";
- }
- if (flag.Equals("true", StringComparison.OrdinalIgnoreCase))
- {
- db.Ado.IsEnableLogEvent = true;
- //SQL执行前事件
- db.Aop.OnLogExecuting = (sql, pars) =>
- {
- foreach (var item in pars)
- {
- sql = sql.Replace(item.ParameterName.ToString(), $"'{item.Value?.ToString()}'");
- }
- //log.Info($"执行前SQL: {sql}");
- //log.Info(db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
- };
- //SQL执行完事件
- db.Aop.OnLogExecuted = (sql, pars) =>
- {
- foreach (var item in pars)
- {
- sql = sql.Replace(item.ParameterName.ToString(), $"'{item.Value?.ToString()}'");
- }
- //log.Info($"执行后SQL: {sql}");
- };
- db.Aop.OnError = (exp) =>//执行SQL 错误事件
- {
- //log.Debug(exp, exp.Sql);
- };
- }
- else
- {
- db.Ado.IsEnableLogEvent = false;
- }
- return (T)db;
- });
- break;
- case ServiceLifetime.Transient:
- services.AddTransient(serviceProvider =>
- {
- var configT = new ConnectionConfig()
- {
- ConfigureExternalServices = new ConfigureExternalServices
- {
- SqlFuncServices = SqlSugarConfig.GetLambda()
- }
- };
- configAction.Invoke(configT);
- var db = new SqlSugarClient(configT)
- {
- MappingTables = SqlSugarConfig.listTable
- };
- //var log = serviceProvider.GetRequiredService<ILogUtil>();
- var configuration = serviceProvider.GetRequiredService<IConfiguration>();
- string flag = configuration["Log:sqllog"];
- if (string.IsNullOrWhiteSpace(flag))
- {
- flag = "false";
- }
- if (flag.Equals("true", StringComparison.OrdinalIgnoreCase))
- {
- db.Ado.IsEnableLogEvent = true;
- //SQL执行前事件
- db.Aop.OnLogExecuting = (sql, pars) =>
- {
- foreach (var item in pars)
- {
- sql = sql.Replace(item.ParameterName.ToString(), $"'{item.Value?.ToString()}'");
- }
- //log.Info($"执行前SQL: {sql}");
- //log.Info(db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
- };
- //SQL执行完事件
- db.Aop.OnLogExecuted = (sql, pars) =>
- {
- foreach (var item in pars)
- {
- sql = sql.Replace(item.ParameterName.ToString(), $"'{item.Value?.ToString()}'");
- }
- //log.Info($"执行后SQL: {sql}");
- };
- db.Aop.OnError = (exp) =>//执行SQL 错误事件
- {
- //log.Debug(exp, exp.Sql);
- };
- }
- else
- {
- db.Ado.IsEnableLogEvent = false;
- }
- return (T)db;
- });
- break;
- }
- return services;
- }
- }
- }
|