Update Demo

This commit is contained in:
sunkaixuan 2019-05-05 16:56:03 +08:00
parent 56996fc04d
commit 621f42725f
4 changed files with 59 additions and 6 deletions

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;
using SqlSugar; using SqlSugar;
namespace OrmTest namespace OrmTest
{ {
@ -10,9 +11,50 @@ namespace OrmTest
public static void Init() 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() private static void DistributedTransactionExample()
{ {
Console.WriteLine(""); Console.WriteLine("");
@ -68,5 +110,8 @@ namespace OrmTest
Console.WriteLine("#### Distributed TransactionExample End ####"); Console.WriteLine("#### Distributed TransactionExample End ####");
} }
} }
} }

View File

@ -53,7 +53,7 @@ namespace SqlSugar
public SugarDebugger Debugger { get; set; } public SugarDebugger Debugger { get; set; }
[JsonIgnore] [JsonIgnore]
public AopEvents AopEvents = new AopEvents(); public AopEvents AopEvents { get;set; }
} }
public class AopEvents public class AopEvents
{ {

View File

@ -24,7 +24,7 @@ namespace SqlSugar
} }
public ConnectionConfig CurrentConnectionConfig { get; set; } public ConnectionConfig CurrentConnectionConfig { get; set; }
public Dictionary<string, object> TempItems { get; set; } public Dictionary<string, object> TempItems { get { if (_TempItems == null) { _TempItems = new Dictionary<string, object>(); } return _TempItems; } set=>_TempItems=value; }
public bool IsSystemTablesConfig { get { return this.CurrentConnectionConfig.InitKeyType == InitKeyType.SystemTable; } } public bool IsSystemTablesConfig { get { return this.CurrentConnectionConfig.InitKeyType == InitKeyType.SystemTable; } }
public Guid ContextID { get; set; } public Guid ContextID { get; set; }
internal bool IsAsyncMethod { get; set; } internal bool IsAsyncMethod { get; set; }
@ -37,6 +37,7 @@ namespace SqlSugar
#endregion #endregion
#region Fields #region Fields
public Dictionary<string, object> _TempItems;
public QueueList _Queues; public QueueList _Queues;
protected ISqlBuilder _SqlBuilder; protected ISqlBuilder _SqlBuilder;
protected ISqlSugarClient _Context { get; set; } protected ISqlSugarClient _Context { get; set; }

View File

@ -71,7 +71,7 @@ namespace SqlSugar
public QueueList Queues { get => this.Context.Queues; set => this.Context.Queues = value; } public QueueList Queues { get => this.Context.Queues; set => this.Context.Queues = value; }
public Dictionary<string, object> TempItems { get => this.Context.TempItems??new Dictionary<string, object>(); set => this.Context.TempItems = value; } public Dictionary<string, object> TempItems { get => this.Context.TempItems; set => this.Context.TempItems = value; }
public IContextMethods Utilities { get => this.Context.Utilities; set => this.Context.Utilities = value; } public IContextMethods Utilities { get => this.Context.Utilities; set => this.Context.Utilities = value; }
public IAdo Ado => this.Context.Ado; public IAdo Ado => this.Context.Ado;
@ -672,7 +672,14 @@ namespace SqlSugar
private void InitContext(ConnectionConfig config) private void InitContext(ConnectionConfig config)
{ {
var aopIsNull = config.AopEvents == null;
if (aopIsNull)
{
config.AopEvents = new AopEvents();
}
_Context = new SqlSugarContext(config); _Context = new SqlSugarContext(config);
if (!aopIsNull)
_Context.Ado.IsEnableLogEvent = true;
this.CurrentConnectionConfig = config; this.CurrentConnectionConfig = config;
_ThreadId = Thread.CurrentThread.ManagedThreadId.ToString(); _ThreadId = Thread.CurrentThread.ManagedThreadId.ToString();
if (_MappingColumns == null) if (_MappingColumns == null)