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

View File

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

View File

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