diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Extensions/SqlsugarExtensions.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Extensions/SqlsugarExtensions.cs index 1df6a04d..2de7da2f 100644 --- a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Extensions/SqlsugarExtensions.cs +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/Extensions/SqlsugarExtensions.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; +using Autofac.Core; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using SqlSugar; using System; @@ -16,8 +17,17 @@ using DbType = SqlSugar.DbType; namespace Yi.Framework.Core.Sqlsugar.Extensions { + /// + /// 这一块,需要做成上下文对象,会进行重构 + /// public static class SqlsugarExtensions { + public static void AddDbSqlsugarContextServer(this IServiceCollection services) + { + services.AddTransient(x => x.GetRequiredService().SqlSugarClient); + services.AddTransient(); + } + public static void AddSqlsugarServer(this IServiceCollection services, Action? action = null) { var dbConnOptions = Appsettings.app("DbConnOptions"); diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/SqlSugarDbContext.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/SqlSugarDbContext.cs new file mode 100644 index 00000000..b15a509e --- /dev/null +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Sqlsugar/SqlSugarDbContext.cs @@ -0,0 +1,152 @@ +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Core.CurrentUsers; +using Yi.Framework.Core.Model; +using Yi.Framework.Core.Sqlsugar.Const; +using Yi.Framework.Core.Sqlsugar.Options; +using Yi.Framework.Data.Auditing; +using Yi.Framework.Data.Entities; + +namespace Yi.Framework.Core.Sqlsugar +{ + public class SqlSugarDbContext + { + /// + /// SqlSugar 客户端 + /// + public ISqlSugarClient SqlSugarClient { get; set; } + + protected ICurrentUser _currentUser; + + protected ILogger _logger; + + protected IOptions _options; + + public SqlSugarDbContext(IOptions options, ICurrentUser currentUser, ILogger logger) + { + _currentUser = currentUser; + _logger = logger; + _options= options; + var dbConnOptions = options.Value; + #region 组装options + if (dbConnOptions.DbType is null) + { + throw new ArgumentException(SqlsugarConst.DbType配置为空); + } + var slavaConFig = new List(); + if (dbConnOptions.EnabledReadWrite) + { + if (dbConnOptions.ReadUrl is null) + { + throw new ArgumentException(SqlsugarConst.读写分离为空); + } + + var readCon = dbConnOptions.ReadUrl; + + readCon.ForEach(s => + { + //如果是动态saas分库,这里的连接串都不能写死,需要动态添加,这里只配置共享库的连接 + slavaConFig.Add(new SlaveConnectionConfig() { ConnectionString = s }); + }); + } + #endregion + ISqlSugarClient sqlSugar = new SqlSugarClient(new ConnectionConfig() + { + //准备添加分表分库 + DbType = dbConnOptions.DbType ?? DbType.Sqlite, + ConnectionString = dbConnOptions.Url, + IsAutoCloseConnection = true, + MoreSettings = new ConnMoreSettings() + { + DisableNvarchar = true + }, + SlaveConnectionConfigs = slavaConFig, + //设置codefirst非空值判断 + ConfigureExternalServices = new ConfigureExternalServices + { + EntityService = (c, p) => + { + //高版C#写法 支持string?和string + if (new NullabilityInfoContext() + .Create(c).WriteState is NullabilityState.Nullable) + { + p.IsNullable = true; + } + } + } + }, + db => + { + + db.Aop.DataExecuting = (oldValue, entityInfo) => + { + + switch (entityInfo.OperationType) + { + case DataFilterType.UpdateByObject: + + if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.LastModificationTime))) + { + entityInfo.SetValue(DateTime.Now); + } + if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.LastModifierId))) + { + if (_currentUser != null) + { + entityInfo.SetValue(_currentUser.Id); + } + } + break; + case DataFilterType.InsertByObject: + if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.CreationTime))) + { + entityInfo.SetValue(DateTime.Now); + } + if (entityInfo.PropertyName.Equals(nameof(IAuditedObject.CreatorId))) + { + if (_currentUser != null) + { + entityInfo.SetValue(_currentUser.Id); + } + } + + //插入时,需要租户id,先预留 + if (entityInfo.PropertyName.Equals(nameof(IMultiTenant.TenantId))) + { + //if (this.CurrentTenant is not null) + //{ + // entityInfo.SetValue(this.CurrentTenant.Id); + //} + } + break; + } + }; + db.Aop.OnLogExecuting = (s, p) => + { + StringBuilder sb = new StringBuilder(); + sb.Append("执行SQL:" + s.ToString()); + foreach (var i in p) + { + sb.Append($"\r\n参数:{i.ParameterName},参数值:{i.Value}"); + } + sb.Append($"\r\n 完整SQL:{UtilMethods.GetSqlString(DbType.MySql, s, p)}"); + logger?.LogDebug(sb.ToString()); + }; + //扩展 + this.OnSqlSugarClientConfig(db); + }); + } + + //上下文对象扩展 + protected virtual void OnSqlSugarClientConfig(ISqlSugarClient sqlSugarClient) + { + } + } +} diff --git a/Yi.Framework.Net6/test/ReadMe.txt b/Yi.Framework.Net6/test/ReadMe.txt new file mode 100644 index 00000000..e69de29b