mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 13:34:32 +08:00
Update ScopedClient
This commit is contained in:
parent
d9b5783921
commit
7725923401
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Dynamic;
|
using System.Dynamic;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@ -33,16 +34,34 @@ namespace SqlSugar
|
|||||||
this.db = new SqlSugarClient(configs);
|
this.db = new SqlSugarClient(configs);
|
||||||
this.configAction = configAction;
|
this.configAction = configAction;
|
||||||
}
|
}
|
||||||
public ScopedClient(SqlSugarClient context,Action<SqlSugarClient> configAction)
|
//public ScopedClient(SqlSugarClient context,Action<SqlSugarClient> configAction)
|
||||||
{
|
//{
|
||||||
this.db = context;
|
// this.db = context;
|
||||||
this.configAction = configAction;
|
// this.configAction = configAction;
|
||||||
}
|
//}
|
||||||
public SqlSugarClient ScopedContext
|
public SqlSugarClient ScopedContext
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
|
SqlSugarClient result = null;
|
||||||
var key = db.GetHashCode().ToString();
|
var key = db.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);
|
SqlSugarClient result = CallContextAsync<SqlSugarClient>.GetData(key);
|
||||||
if (result == null)
|
if (result == null)
|
||||||
{
|
{
|
||||||
@ -53,9 +72,25 @@ namespace SqlSugar
|
|||||||
this.configAction(result);
|
this.configAction(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 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 MappingTableList MappingTables { get => ScopedContext.MappingTables; set => ScopedContext.MappingTables = value; }
|
||||||
public MappingColumnList MappingColumns { get => ScopedContext.MappingColumns; set => ScopedContext.MappingColumns=value; }
|
public MappingColumnList MappingColumns { get => ScopedContext.MappingColumns; set => ScopedContext.MappingColumns=value; }
|
||||||
public IgnoreColumnList IgnoreColumns { get => ScopedContext.IgnoreColumns; set => ScopedContext.IgnoreColumns=value; }
|
public IgnoreColumnList IgnoreColumns { get => ScopedContext.IgnoreColumns; set => ScopedContext.IgnoreColumns=value; }
|
||||||
|
@ -16,4 +16,13 @@ namespace SqlSugar
|
|||||||
public static T GetData(string name) =>
|
public static T GetData(string name) =>
|
||||||
state.TryGetValue(name, out AsyncLocal<T> data) ? data.Value : default(T);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user