Perfect AOP

This commit is contained in:
sunkaixuan 2017-09-19 00:07:49 +08:00
parent 0044acf123
commit 1eb6a448a0
5 changed files with 64 additions and 20 deletions

View File

@ -51,9 +51,10 @@ namespace SqlSugar
public virtual CommandType CommandType { get; set; }
public virtual bool IsEnableLogEvent { get; set; }
public virtual bool IsClearParameters { get; set; }
public virtual Action<string, string> LogEventStarting { get; set; }
public virtual Action<string, string> LogEventCompleted { get; set; }
public virtual Action<string, SugarParameter[]> LogEventStarting { get; set; }
public virtual Action<string, SugarParameter[]> LogEventCompleted { get; set; }
public virtual Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ProcessingEventStartingSQL { get; set; }
public virtual Action<Exception> ErrorEvent { get; set; }
#endregion
#region Connection
@ -241,6 +242,8 @@ namespace SqlSugar
}
catch (Exception ex)
{
if (ErrorEvent != null)
ErrorEvent(ex);
throw ex;
}
finally
@ -250,18 +253,27 @@ namespace SqlSugar
}
public virtual IDataReader GetDataReader(string sql, params SugarParameter[] parameters)
{
var isSp = this.CommandType == CommandType.StoredProcedure;
if (this.ProcessingEventStartingSQL != null)
ExecuteProcessingSQL(ref sql, parameters);
ExecuteBefore(sql, parameters);
IDbCommand sqlCommand = GetCommand(sql, parameters);
IDataReader sqlDataReader = sqlCommand.ExecuteReader(this.IsClose() ? CommandBehavior.CloseConnection : CommandBehavior.Default);
if (isSp)
DataReaderParameters = sqlCommand.Parameters;
if (this.IsClearParameters)
sqlCommand.Parameters.Clear();
ExecuteAfter(sql, parameters);
return sqlDataReader;
try
{
var isSp = this.CommandType == CommandType.StoredProcedure;
if (this.ProcessingEventStartingSQL != null)
ExecuteProcessingSQL(ref sql, parameters);
ExecuteBefore(sql, parameters);
IDbCommand sqlCommand = GetCommand(sql, parameters);
IDataReader sqlDataReader = sqlCommand.ExecuteReader(this.IsClose() ? CommandBehavior.CloseConnection : CommandBehavior.Default);
if (isSp)
DataReaderParameters = sqlCommand.Parameters;
if (this.IsClearParameters)
sqlCommand.Parameters.Clear();
ExecuteAfter(sql, parameters);
return sqlDataReader;
}
catch (Exception ex)
{
if (ErrorEvent != null)
ErrorEvent(ex);
throw ex;
}
}
public virtual DataSet GetDataSetAll(string sql, params SugarParameter[] parameters)
{
@ -282,6 +294,8 @@ namespace SqlSugar
}
catch (Exception ex)
{
if (ErrorEvent != null)
ErrorEvent(ex);
throw ex;
}
finally
@ -306,6 +320,8 @@ namespace SqlSugar
}
catch (Exception ex)
{
if (ErrorEvent != null)
ErrorEvent(ex);
throw ex;
}
finally
@ -578,7 +594,7 @@ namespace SqlSugar
{
if (this.IsEnableLogEvent)
{
Action<string, string> action = LogEventStarting;
Action<string, SugarParameter[]> action = LogEventStarting;
if (action != null)
{
if (parameters == null || parameters.Length == 0)
@ -587,7 +603,7 @@ namespace SqlSugar
}
else
{
action(sql, this.Context.Utilities.SerializeObject(parameters.Select(it => new { key = it.ParameterName, value = it.Value.ObjToString() })));
action(sql, parameters);
}
}
}
@ -606,7 +622,7 @@ namespace SqlSugar
}
if (this.IsEnableLogEvent)
{
Action<string, string> action = LogEventCompleted;
Action<string, SugarParameter[]> action = LogEventCompleted;
if (action != null)
{
if (parameters == null || parameters.Length == 0)
@ -615,7 +631,7 @@ namespace SqlSugar
}
else
{
action(sql, this.Context.Utilities.SerializeObject(parameters.Select(it => new { key = it.ParameterName, value = it.Value.ObjToString() })));
action(sql, parameters);
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SqlSugar
{
public class AopProvider
{
private AopProvider() { }
public AopProvider(SqlSugarClient context)
{
this.Context = context;
this.Context.Ado.IsEnableLogEvent = true;
}
private SqlSugarClient Context { get; set; }
public Action<Exception> OnError { set { this.Context.Ado.ErrorEvent = value; } }
public Action<string, SugarParameter[]> OnLogExecuting { set { this.Context.Ado.LogEventStarting = value; } }
public Action<string, SugarParameter[]> OnLogExecuted { set { this.Context.Ado.LogEventCompleted = value; } }
public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> OnExecutingChangeSql { set { this.Context.Ado.ProcessingEventStartingSQL = value; } }
}
}

View File

@ -23,9 +23,10 @@ namespace SqlSugar
IDataParameterCollection DataReaderParameters { get; set; }
CommandType CommandType { get; set; }
bool IsEnableLogEvent { get; set; }
Action<string, string> LogEventStarting { get; set; }
Action<string, string> LogEventCompleted { get; set; }
Action<string, SugarParameter []> LogEventStarting { get; set; }
Action<string, SugarParameter []> LogEventCompleted { get; set; }
Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ProcessingEventStartingSQL { get; set; }
Action<Exception> ErrorEvent { get; set; }
bool IsClearParameters { get; set; }
int CommandTimeOut { get; set; }
IDbBind DbBind { get; }

View File

@ -56,6 +56,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Abstract\AopProvider\AopProvider.cs" />
<Compile Include="Abstract\CodeFirstProvider\CodeFirstProvider.cs" />
<Compile Include="Abstract\AdoProvider\AdoAccessory.cs" />
<Compile Include="Abstract\DbBindProvider\DbBindAccessory.cs" />

View File

@ -62,6 +62,10 @@ namespace SqlSugar
}
#endregion
#region Aop Log Methods
public virtual AopProvider Aop { get { return new AopProvider(this.Context); } }
#endregion
#region Util Methods
[Obsolete("Use SqlSugarClient.Utilities")]
public virtual IRewritableMethods RewritableMethods