Code optimization

This commit is contained in:
sunkaixuan 2017-09-06 10:52:00 +08:00
parent b1dd6c96e5
commit dcf07e6793
7 changed files with 21 additions and 32 deletions

View File

@ -249,12 +249,8 @@ namespace SqlSugar
} }
private IDeleteable<T> CopyDeleteable() { private IDeleteable<T> 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.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<T>(); var asyncDeleteable = asyncContext.Deleteable<T>();
var asyncDeleteBuilder = asyncDeleteable.DeleteBuilder; var asyncDeleteBuilder = asyncDeleteable.DeleteBuilder;

View File

@ -307,12 +307,8 @@ namespace SqlSugar
} }
private IInsertable<T> CopyInsertable() private IInsertable<T> 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.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<T>(this.InsertObjs); var asyncInsertable = asyncContext.Insertable<T>(this.InsertObjs);
var asyncInsertableBuilder = asyncInsertable.InsertBuilder; var asyncInsertableBuilder = asyncInsertable.InsertBuilder;

View File

@ -1024,9 +1024,7 @@ namespace SqlSugar
foreach (var item in result) foreach (var item in result)
{ {
var contextProperty = item.GetType().GetProperty("Context"); var contextProperty = item.GetType().GetProperty("Context");
ConnectionConfig config = new ConnectionConfig(); SqlSugarClient newClient = this.Context.Utilities.CopyCurrentContext(this.Context);
config = this.Context.CurrentConnectionConfig;
SqlSugarClient newClient = this.Context.CopyContext(config);
contextProperty.SetValue(item, newClient, null); contextProperty.SetValue(item, newClient, null);
} }
} }
@ -1034,12 +1032,8 @@ namespace SqlSugar
} }
private ISugarQueryable<T> CopyQueryable() private ISugarQueryable<T> 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.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<ExpandoObject>().Select<T>(string.Empty); var asyncQueryable = asyncContext.Queryable<ExpandoObject>().Select<T>(string.Empty);
var asyncQueryableBuilder = asyncQueryable.QueryBuilder; var asyncQueryableBuilder = asyncQueryable.QueryBuilder;

View File

@ -311,12 +311,8 @@ namespace SqlSugar
} }
private IUpdateable<T> CopyUpdateable() private IUpdateable<T> 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.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<T>(this.UpdateObjs); var asyncUpdateable = asyncContext.Updateable<T>(this.UpdateObjs);
var asyncUpdateableBuilder = asyncUpdateable.UpdateBuilder; var asyncUpdateableBuilder = asyncUpdateable.UpdateBuilder;

View File

@ -222,6 +222,20 @@ namespace SqlSugar
return DeserializeObject<T>(jsonString); return DeserializeObject<T>(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 #endregion
#region DataTable #region DataTable

View File

@ -16,6 +16,7 @@ namespace SqlSugar
string SerializeObject(object value); string SerializeObject(object value);
T DeserializeObject<T>(string value); T DeserializeObject<T>(string value);
T TranslateCopy<T>(T sourceObject); T TranslateCopy<T>(T sourceObject);
SqlSugarClient CopyCurrentContext(SqlSugarClient context, bool isCopyEvents = false);
dynamic DataTableToDynamic(DataTable table); dynamic DataTableToDynamic(DataTable table);
ICacheManager<T> GetCacheInstance<T>(); ICacheManager<T> GetCacheInstance<T>();
void RemoveCacheAll(); void RemoveCacheAll();

View File

@ -50,7 +50,7 @@ namespace SqlSugar
} }
#endregion #endregion
#region Rewritable Methods #region Util Methods
[Obsolete("Use SqlSugarClient.Utilities")] [Obsolete("Use SqlSugarClient.Utilities")]
public virtual IRewritableMethods RewritableMethods public virtual IRewritableMethods RewritableMethods
{ {
@ -546,14 +546,6 @@ namespace SqlSugar
this.Ado.Dispose(); 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 #endregion
} }
} }