diff --git a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarCoreProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarCoreProvider.cs new file mode 100644 index 000000000..90829c47f --- /dev/null +++ b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarCoreProvider.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + /// + /// Partial SqlSugarScope + /// + public partial class SqlSugarScope : ISqlSugarClient, ITenant + { + + private List _configs; + private Action _configAction; + + private SqlSugarClient GetContext() + { + SqlSugarClient result = null; + var key = _configs.GetHashCode().ToString(); + StackTrace st = new StackTrace(true); + var methods = st.GetFrames(); + var isAsync = UtilMethods.IsAnyAsyncMethod(methods); + if (isAsync) + { + result = GetAsyncContext(key); + } + else + { + result = GetThreadContext(key); + } + return result; + } + private SqlSugarClient GetAsyncContext(string key) + { + SqlSugarClient result = CallContextAsync.GetData(key); + if (result == null) + { + List configList = GetCopyConfigs(); + CallContextAsync.SetData(key, new SqlSugarClient(configList)); + result = CallContextAsync.GetData(key); + if (this._configAction != null) + { + this._configAction(result); + } + } + + return result; + } + private SqlSugarClient GetThreadContext(string key) + { + SqlSugarClient result = CallContextThread.GetData(key); + if (result == null) + { + List configList = GetCopyConfigs(); + CallContextThread.SetData(key, new SqlSugarClient(configList)); + result = CallContextThread.GetData(key); + if (this._configAction != null) + { + this._configAction(result); + } + } + return result; + } + private List GetCopyConfigs() + { + return _configs.Select(it => new ConnectionConfig() + { + AopEvents = it.AopEvents, + ConfigId = it.ConfigId, + ConfigureExternalServices = it.ConfigureExternalServices, + ConnectionString = it.ConnectionString, + DbType = it.DbType, + IndexSuffix = it.IndexSuffix, + InitKeyType = it.InitKeyType, + IsAutoCloseConnection = it.IsAutoCloseConnection, + LanguageType = it.LanguageType, + MoreSettings = it.MoreSettings == null ? null : new ConnMoreSettings() + { + DefaultCacheDurationInSeconds = it.MoreSettings.DefaultCacheDurationInSeconds, + DisableNvarchar = it.MoreSettings.DisableNvarchar, + PgSqlIsAutoToLower = it.MoreSettings.PgSqlIsAutoToLower, + IsAutoRemoveDataCache = it.MoreSettings.IsAutoRemoveDataCache, + IsWithNoLockQuery = it.MoreSettings.IsWithNoLockQuery + }, + SlaveConnectionConfigs = it.SlaveConnectionConfigs + }).ToList(); + } + } +} diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj index f9de8ac50..e1b7544e8 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj @@ -93,6 +93,7 @@ + diff --git a/Src/Asp.Net/SqlSugar/SqlSugarScope.cs b/Src/Asp.Net/SqlSugar/SqlSugarScope.cs index 4557c5122..52a0762b7 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugarScope.cs +++ b/Src/Asp.Net/SqlSugar/SqlSugarScope.cs @@ -9,10 +9,8 @@ using System.Threading.Tasks; namespace SqlSugar { - public class SqlSugarScope: ISqlSugarClient, ITenant + public partial class SqlSugarScope: ISqlSugarClient, ITenant { - private List _configs; - private Action _configAction; private SqlSugarScope() { @@ -646,82 +644,5 @@ namespace SqlSugar { return ScopedContext.Fastest(); } - - - private SqlSugarClient GetContext() - { - SqlSugarClient result = null; - var key = _configs.GetHashCode().ToString(); - StackTrace st = new StackTrace(true); - var methods = st.GetFrames(); - var isAsync = UtilMethods.IsAnyAsyncMethod(methods); - if (isAsync) - { - result = GetAsyncContext(key); - } - else - { - result = GetThreadContext(key); - } - return result; - } - - private SqlSugarClient GetAsyncContext(string key) - { - SqlSugarClient result = CallContextAsync.GetData(key); - if (result == null) - { - List configList = GetCopyConfigs(); - CallContextAsync.SetData(key, new SqlSugarClient(configList)); - result = CallContextAsync.GetData(key); - if (this._configAction != null) - { - this._configAction(result); - } - } - - return result; - } - - private SqlSugarClient GetThreadContext(string key) - { - SqlSugarClient result = CallContextThread.GetData(key); - if (result == null) - { - List configList = GetCopyConfigs(); - CallContextThread.SetData(key, new SqlSugarClient(configList)); - result = CallContextThread.GetData(key); - if (this._configAction != null) - { - this._configAction(result); - } - } - return result; - } - - private List GetCopyConfigs() - { - return _configs.Select(it => new ConnectionConfig() - { - AopEvents = it.AopEvents, - ConfigId = it.ConfigId, - ConfigureExternalServices = it.ConfigureExternalServices, - ConnectionString = it.ConnectionString, - DbType = it.DbType, - IndexSuffix = it.IndexSuffix, - InitKeyType = it.InitKeyType, - IsAutoCloseConnection = it.IsAutoCloseConnection, - LanguageType = it.LanguageType, - MoreSettings = it.MoreSettings == null ? null : new ConnMoreSettings() - { - DefaultCacheDurationInSeconds = it.MoreSettings.DefaultCacheDurationInSeconds, - DisableNvarchar = it.MoreSettings.DisableNvarchar, - PgSqlIsAutoToLower = it.MoreSettings.PgSqlIsAutoToLower, - IsAutoRemoveDataCache = it.MoreSettings.IsAutoRemoveDataCache, - IsWithNoLockQuery = it.MoreSettings.IsWithNoLockQuery - }, - SlaveConnectionConfigs = it.SlaveConnectionConfigs - }).ToList(); - } } }