mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-03-18 07:26:37 +08:00
refactor: ai+人工重构优化 framework
This commit is contained in:
@@ -4,26 +4,52 @@ using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.SqlSugarCore;
|
||||
|
||||
/// <summary>
|
||||
/// SqlSugar数据库上下文创建上下文
|
||||
/// </summary>
|
||||
public class SqlSugarDbContextCreationContext
|
||||
{
|
||||
public static SqlSugarDbContextCreationContext Current => _current.Value;
|
||||
private static readonly AsyncLocal<SqlSugarDbContextCreationContext> _current = new AsyncLocal<SqlSugarDbContextCreationContext>();
|
||||
private static readonly AsyncLocal<SqlSugarDbContextCreationContext> CurrentContextHolder =
|
||||
new AsyncLocal<SqlSugarDbContextCreationContext>();
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前上下文
|
||||
/// </summary>
|
||||
public static SqlSugarDbContextCreationContext Current => CurrentContextHolder.Value!;
|
||||
|
||||
/// <summary>
|
||||
/// 连接字符串名称
|
||||
/// </summary>
|
||||
public string ConnectionStringName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 连接字符串
|
||||
/// </summary>
|
||||
public string ConnectionString { get; }
|
||||
|
||||
public DbConnection ExistingConnection { get; internal set; }
|
||||
/// <summary>
|
||||
/// 现有数据库连接
|
||||
/// </summary>
|
||||
public DbConnection? ExistingConnection { get; internal set; }
|
||||
|
||||
public SqlSugarDbContextCreationContext(string connectionStringName, string connectionString)
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
public SqlSugarDbContextCreationContext(
|
||||
string connectionStringName,
|
||||
string connectionString)
|
||||
{
|
||||
ConnectionStringName = connectionStringName;
|
||||
ConnectionString = connectionString;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用指定的上下文
|
||||
/// </summary>
|
||||
public static IDisposable Use(SqlSugarDbContextCreationContext context)
|
||||
{
|
||||
var previousValue = Current;
|
||||
_current.Value = context;
|
||||
return new DisposeAction(() => _current.Value = previousValue);
|
||||
var previousContext = Current;
|
||||
CurrentContextHolder.Value = context;
|
||||
return new DisposeAction(() => CurrentContextHolder.Value = previousContext);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user