diff --git a/Src/Asp.Net/SqlServerTest/Demo/Demo1_SqlSugarClient.cs b/Src/Asp.Net/SqlServerTest/Demo/Demo1_SqlSugarClient.cs index 25aaa43e5..31cec4bcc 100644 --- a/Src/Asp.Net/SqlServerTest/Demo/Demo1_SqlSugarClient.cs +++ b/Src/Asp.Net/SqlServerTest/Demo/Demo1_SqlSugarClient.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading.Tasks; using SqlSugar; namespace OrmTest { @@ -10,9 +11,50 @@ namespace OrmTest public static void Init() { - DistributedTransactionExample(); + SingletonPattern();//单例 + DistributedTransactionExample();//分布式事务 } + private static void SingletonPattern() + { + Console.WriteLine(""); + Console.WriteLine("#### Singleton Pattern Start ####"); + Console.WriteLine("Db_Id:" + singleDb.ContextID); + Console.WriteLine("Db_Id:" + singleDb.ContextID); + var task = new Task(() => + { + Console.WriteLine("Task DbId:" + singleDb.ContextID); + new Task(() => + { + Console.WriteLine("_Task_Task DbId:" + singleDb.ContextID); + Console.WriteLine("_Task_Task DbId:" + singleDb.ContextID); + + }).Start(); + Console.WriteLine("Task DbId:" + singleDb.ContextID); + }); + task.Start(); + task.Wait(); + System.Threading.Thread.Sleep(500); + Console.WriteLine(string.Join(",", singleDb.TempItems.Keys)); + + Console.WriteLine("#### Singleton Pattern end ####"); + } + + static SqlSugarClient singleDb = new SqlSugarClient( + new ConnectionConfig() + { + ConfigId = 1, + DbType = DbType.SqlServer, + ConnectionString = Config.ConnectionString, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection=true, + AopEvents = new AopEvents() + { + OnLogExecuting = (sql, p) => { Console.WriteLine(sql); } + } + }); + + private static void DistributedTransactionExample() { Console.WriteLine(""); @@ -68,5 +110,8 @@ namespace OrmTest Console.WriteLine("#### Distributed TransactionExample End ####"); } + + + } } diff --git a/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs b/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs index 83ca1cc21..532d26574 100644 --- a/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs +++ b/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs @@ -51,9 +51,9 @@ namespace SqlSugar /// Used for debugging errors or BUG,Used for debugging, which has an impact on Performance /// public SugarDebugger Debugger { get; set; } - + [JsonIgnore] - public AopEvents AopEvents = new AopEvents(); + public AopEvents AopEvents { get;set; } } public class AopEvents { diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs b/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs index dc0c71ba7..2448a6092 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/SqlSugarAccessory.cs @@ -24,7 +24,7 @@ namespace SqlSugar } public ConnectionConfig CurrentConnectionConfig { get; set; } - public Dictionary TempItems { get; set; } + public Dictionary TempItems { get { if (_TempItems == null) { _TempItems = new Dictionary(); } return _TempItems; } set=>_TempItems=value; } public bool IsSystemTablesConfig { get { return this.CurrentConnectionConfig.InitKeyType == InitKeyType.SystemTable; } } public Guid ContextID { get; set; } internal bool IsAsyncMethod { get; set; } @@ -36,7 +36,8 @@ namespace SqlSugar #endregion - #region Fields + #region Fields + public Dictionary _TempItems; public QueueList _Queues; protected ISqlBuilder _SqlBuilder; protected ISqlSugarClient _Context { get; set; } diff --git a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs index 549ad8e7f..3d45cd431 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs @@ -71,7 +71,7 @@ namespace SqlSugar public QueueList Queues { get => this.Context.Queues; set => this.Context.Queues = value; } - public Dictionary TempItems { get => this.Context.TempItems??new Dictionary(); set => this.Context.TempItems = value; } + public Dictionary TempItems { get => this.Context.TempItems; set => this.Context.TempItems = value; } public IContextMethods Utilities { get => this.Context.Utilities; set => this.Context.Utilities = value; } public IAdo Ado => this.Context.Ado; @@ -672,7 +672,14 @@ namespace SqlSugar private void InitContext(ConnectionConfig config) { + var aopIsNull = config.AopEvents == null; + if (aopIsNull) + { + config.AopEvents = new AopEvents(); + } _Context = new SqlSugarContext(config); + if (!aopIsNull) + _Context.Ado.IsEnableLogEvent = true; this.CurrentConnectionConfig = config; _ThreadId = Thread.CurrentThread.ManagedThreadId.ToString(); if (_MappingColumns == null)