Code optimization

This commit is contained in:
sunkaixuna 2021-12-16 02:04:41 +08:00
parent c7d6d529a6
commit 5008b02117
3 changed files with 94 additions and 80 deletions

View File

@ -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
{
/// <summary>
/// Partial SqlSugarScope
/// </summary>
public partial class SqlSugarScope : ISqlSugarClient, ITenant
{
private List<ConnectionConfig> _configs;
private Action<SqlSugarClient> _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<SqlSugarClient>.GetData(key);
if (result == null)
{
List<ConnectionConfig> configList = GetCopyConfigs();
CallContextAsync<SqlSugarClient>.SetData(key, new SqlSugarClient(configList));
result = CallContextAsync<SqlSugarClient>.GetData(key);
if (this._configAction != null)
{
this._configAction(result);
}
}
return result;
}
private SqlSugarClient GetThreadContext(string key)
{
SqlSugarClient result = CallContextThread<SqlSugarClient>.GetData(key);
if (result == null)
{
List<ConnectionConfig> configList = GetCopyConfigs();
CallContextThread<SqlSugarClient>.SetData(key, new SqlSugarClient(configList));
result = CallContextThread<SqlSugarClient>.GetData(key);
if (this._configAction != null)
{
this._configAction(result);
}
}
return result;
}
private List<ConnectionConfig> 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();
}
}
}

View File

@ -93,6 +93,7 @@
<Compile Include="Abstract\FastestProvider\Private.cs" />
<Compile Include="Abstract\FastestProvider\Setting.cs" />
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
<Compile Include="Abstract\SugarProvider\SqlSugarCoreProvider.cs" />
<Compile Include="Interface\IFastBuilder.cs" />
<Compile Include="Interface\IFastest.cs" />
<Compile Include="OnlyNet\OracleFastBuilder.cs" />

View File

@ -9,10 +9,8 @@ using System.Threading.Tasks;
namespace SqlSugar
{
public class SqlSugarScope: ISqlSugarClient, ITenant
public partial class SqlSugarScope: ISqlSugarClient, ITenant
{
private List<ConnectionConfig> _configs;
private Action<SqlSugarClient> _configAction;
private SqlSugarScope()
{
@ -646,82 +644,5 @@ namespace SqlSugar
{
return ScopedContext.Fastest<T>();
}
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<SqlSugarClient>.GetData(key);
if (result == null)
{
List<ConnectionConfig> configList = GetCopyConfigs();
CallContextAsync<SqlSugarClient>.SetData(key, new SqlSugarClient(configList));
result = CallContextAsync<SqlSugarClient>.GetData(key);
if (this._configAction != null)
{
this._configAction(result);
}
}
return result;
}
private SqlSugarClient GetThreadContext(string key)
{
SqlSugarClient result = CallContextThread<SqlSugarClient>.GetData(key);
if (result == null)
{
List<ConnectionConfig> configList = GetCopyConfigs();
CallContextThread<SqlSugarClient>.SetData(key, new SqlSugarClient(configList));
result = CallContextThread<SqlSugarClient>.GetData(key);
if (this._configAction != null)
{
this._configAction(result);
}
}
return result;
}
private List<ConnectionConfig> 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();
}
}
}