From dcf07e67935997b5a2c567ffed60691ea4d36c6d Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 6 Sep 2017 10:52:00 +0800 Subject: [PATCH] Code optimization --- .../Abstract/DeleteProvider/DeleteableProvider.cs | 6 +----- .../InsertableProvider/InsertableProvider.cs | 6 +----- .../QueryableProvider/QueryableProvider.cs | 10 ++-------- .../Abstract/UpdateProvider/UpdateableProvider.cs | 6 +----- .../SqlSugar/Infrastructure/RewritableMethods.cs | 14 ++++++++++++++ .../SqlSugar/Interface/IRewritableMethods.cs | 1 + Src/Asp.Net/SqlSugar/SqlSugarClient.cs | 10 +--------- 7 files changed, 21 insertions(+), 32 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs index cc4cd86ef..db3a182a5 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs @@ -249,12 +249,8 @@ namespace SqlSugar } private IDeleteable CopyDeleteable() { - var asyncContext = this.Context.CopyContext(this.Context.Utilities.TranslateCopy(this.Context.CurrentConnectionConfig)); + var asyncContext = this.Context.Utilities.CopyCurrentContext(this.Context,true); asyncContext.CurrentConnectionConfig.IsAutoCloseConnection = true; - asyncContext.Ado.IsEnableLogEvent = this.Context.Ado.IsEnableLogEvent; - asyncContext.Ado.LogEventStarting = this.Context.Ado.LogEventStarting; - asyncContext.Ado.LogEventCompleted = this.Context.Ado.LogEventCompleted; - asyncContext.Ado.ProcessingEventStartingSQL = this.Context.Ado.ProcessingEventStartingSQL; var asyncDeleteable = asyncContext.Deleteable(); var asyncDeleteBuilder = asyncDeleteable.DeleteBuilder; diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index bdf484856..e337c0b3b 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -307,12 +307,8 @@ namespace SqlSugar } private IInsertable CopyInsertable() { - var asyncContext = this.Context.CopyContext(this.Context.Utilities.TranslateCopy(this.Context.CurrentConnectionConfig)); + var asyncContext = this.Context.Utilities.CopyCurrentContext(this.Context,true); asyncContext.CurrentConnectionConfig.IsAutoCloseConnection = true; - asyncContext.Ado.IsEnableLogEvent = this.Context.Ado.IsEnableLogEvent; - asyncContext.Ado.LogEventStarting = this.Context.Ado.LogEventStarting; - asyncContext.Ado.LogEventCompleted = this.Context.Ado.LogEventCompleted; - asyncContext.Ado.ProcessingEventStartingSQL = this.Context.Ado.ProcessingEventStartingSQL; var asyncInsertable = asyncContext.Insertable(this.InsertObjs); var asyncInsertableBuilder = asyncInsertable.InsertBuilder; diff --git a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index df88d1b41..10be30f9a 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -1024,9 +1024,7 @@ namespace SqlSugar foreach (var item in result) { var contextProperty = item.GetType().GetProperty("Context"); - ConnectionConfig config = new ConnectionConfig(); - config = this.Context.CurrentConnectionConfig; - SqlSugarClient newClient = this.Context.CopyContext(config); + SqlSugarClient newClient = this.Context.Utilities.CopyCurrentContext(this.Context); contextProperty.SetValue(item, newClient, null); } } @@ -1034,12 +1032,8 @@ namespace SqlSugar } private ISugarQueryable CopyQueryable() { - var asyncContext = this.Context.CopyContext(this.Context.Utilities.TranslateCopy(this.Context.CurrentConnectionConfig)); + var asyncContext = this.Context.Utilities.CopyCurrentContext(this.Context,true); asyncContext.CurrentConnectionConfig.IsAutoCloseConnection = true; - asyncContext.Ado.IsEnableLogEvent = this.Context.Ado.IsEnableLogEvent; - asyncContext.Ado.LogEventStarting = this.Context.Ado.LogEventStarting; - asyncContext.Ado.LogEventCompleted = this.Context.Ado.LogEventCompleted; - asyncContext.Ado.ProcessingEventStartingSQL = this.Context.Ado.ProcessingEventStartingSQL; var asyncQueryable = asyncContext.Queryable().Select(string.Empty); var asyncQueryableBuilder = asyncQueryable.QueryBuilder; diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index f10a0d209..21c1d12fb 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -311,12 +311,8 @@ namespace SqlSugar } private IUpdateable CopyUpdateable() { - var asyncContext = this.Context.CopyContext(this.Context.Utilities.TranslateCopy(this.Context.CurrentConnectionConfig)); + var asyncContext = this.Context.Utilities.CopyCurrentContext(this.Context,true); asyncContext.CurrentConnectionConfig.IsAutoCloseConnection = true; - asyncContext.Ado.IsEnableLogEvent = this.Context.Ado.IsEnableLogEvent; - asyncContext.Ado.LogEventStarting = this.Context.Ado.LogEventStarting; - asyncContext.Ado.LogEventCompleted = this.Context.Ado.LogEventCompleted; - asyncContext.Ado.ProcessingEventStartingSQL = this.Context.Ado.ProcessingEventStartingSQL; var asyncUpdateable = asyncContext.Updateable(this.UpdateObjs); var asyncUpdateableBuilder = asyncUpdateable.UpdateBuilder; diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/RewritableMethods.cs b/Src/Asp.Net/SqlSugar/Infrastructure/RewritableMethods.cs index 36b171ca4..bdc364d0c 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/RewritableMethods.cs +++ b/Src/Asp.Net/SqlSugar/Infrastructure/RewritableMethods.cs @@ -222,6 +222,20 @@ namespace SqlSugar return DeserializeObject(jsonString); } } + public SqlSugarClient CopyCurrentContext(SqlSugarClient context,bool isCopyEvents=false) + { + var newClient = new SqlSugarClient(this.TranslateCopy(context.CurrentConnectionConfig)); + newClient.MappingColumns = this.TranslateCopy(context.MappingColumns); + newClient.MappingTables = this.TranslateCopy(context.MappingTables); + newClient.IgnoreColumns = this.TranslateCopy(context.IgnoreColumns); + if (isCopyEvents) { + newClient.Ado.IsEnableLogEvent = context.Ado.IsEnableLogEvent; + newClient.Ado.LogEventStarting = context.Ado.LogEventStarting; + newClient.Ado.LogEventCompleted = context.Ado.LogEventCompleted; + newClient.Ado.ProcessingEventStartingSQL = context.Ado.ProcessingEventStartingSQL; + } + return newClient; + } #endregion #region DataTable diff --git a/Src/Asp.Net/SqlSugar/Interface/IRewritableMethods.cs b/Src/Asp.Net/SqlSugar/Interface/IRewritableMethods.cs index 85e54911a..97faac7b5 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IRewritableMethods.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IRewritableMethods.cs @@ -16,6 +16,7 @@ namespace SqlSugar string SerializeObject(object value); T DeserializeObject(string value); T TranslateCopy(T sourceObject); + SqlSugarClient CopyCurrentContext(SqlSugarClient context, bool isCopyEvents = false); dynamic DataTableToDynamic(DataTable table); ICacheManager GetCacheInstance(); void RemoveCacheAll(); diff --git a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs index cfc3f7c6c..6d2d758fe 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs @@ -50,7 +50,7 @@ namespace SqlSugar } #endregion - #region Rewritable Methods + #region Util Methods [Obsolete("Use SqlSugarClient.Utilities")] public virtual IRewritableMethods RewritableMethods { @@ -546,14 +546,6 @@ namespace SqlSugar this.Ado.Dispose(); } } - internal SqlSugarClient CopyContext(ConnectionConfig config) - { - var newClient = new SqlSugarClient(config); - newClient.MappingColumns = this.Context.MappingColumns; - newClient.MappingTables = this.Context.MappingTables; - newClient.IgnoreColumns = this.Context.IgnoreColumns; - return newClient; - } #endregion } }