Update ScopedClient

This commit is contained in:
sunkaixuna 2021-07-10 01:08:48 +08:00
parent d9b5783921
commit 7725923401
2 changed files with 57 additions and 13 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Linq.Expressions;
using System.Text;
@ -33,29 +34,63 @@ namespace SqlSugar
this.db = new SqlSugarClient(configs);
this.configAction = configAction;
}
public ScopedClient(SqlSugarClient context,Action<SqlSugarClient> configAction)
{
this.db = context;
this.configAction = configAction;
}
//public ScopedClient(SqlSugarClient context,Action<SqlSugarClient> configAction)
//{
// this.db = context;
// this.configAction = configAction;
//}
public SqlSugarClient ScopedContext
{
get
{
SqlSugarClient result = null;
var key = db.GetHashCode().ToString();
SqlSugarClient result = CallContextAsync<SqlSugarClient>.GetData(key);
if (result == null)
StackTrace st = new StackTrace(true);
var methods = st.GetFrames();
var isAsync = UtilMethods.IsAnyAsyncMethod(methods);
if (isAsync)
{
CallContextAsync<SqlSugarClient>.SetData(key, new SqlSugarClient(db._allConfigs));
result = CallContextAsync<SqlSugarClient>.GetData(key);
if (this.configAction != null)
{
this.configAction(result);
}
result=GetAsyncContext(key);
}
else
{
result = GetThreadContext(key);
}
return result;
}
}
private SqlSugarClient GetAsyncContext(string key)
{
SqlSugarClient result = CallContextAsync<SqlSugarClient>.GetData(key);
if (result == null)
{
CallContextAsync<SqlSugarClient>.SetData(key, new SqlSugarClient(db._allConfigs));
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)
{
CallContextThread<SqlSugarClient>.SetData(key, new SqlSugarClient(db._allConfigs));
result = CallContextThread<SqlSugarClient>.GetData(key);
if (this.configAction != null)
{
this.configAction(result);
}
}
return result;
}
public MappingTableList MappingTables { get => ScopedContext.MappingTables; set => ScopedContext.MappingTables = value; }
public MappingColumnList MappingColumns { get => ScopedContext.MappingColumns; set => ScopedContext.MappingColumns=value; }
public IgnoreColumnList IgnoreColumns { get => ScopedContext.IgnoreColumns; set => ScopedContext.IgnoreColumns=value; }

View File

@ -16,4 +16,13 @@ namespace SqlSugar
public static T GetData(string name) =>
state.TryGetValue(name, out AsyncLocal<T> data) ? data.Value : default(T);
}
public class CallContextThread<T>
{
static ConcurrentDictionary<string, ThreadLocal<T>> state = new ConcurrentDictionary<string, ThreadLocal<T>>();
public static void SetData(string name, T data) =>
state.GetOrAdd(name, _ => new ThreadLocal<T>()).Value = data;
public static T GetData(string name) =>
state.TryGetValue(name, out ThreadLocal<T> data) ? data.Value : default(T);
}
}