using FreeSql.DataAnnotations;
using FreeSql.DatabaseModel;
using FreeSql.Internal.Model;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
namespace FreeSql
{
public interface IAop
{
///
/// 可自定义解析表达式
///
event EventHandler ParseExpression;
EventHandler ParseExpressionHandler { get; }
///
/// 自定义实体的配置,方便和多个 ORM 共同使用
///
event EventHandler ConfigEntity;
EventHandler ConfigEntityHandler { get; }
///
/// 自定义实体的属性配置,方便和多个 ORM 共同使用
///
event EventHandler ConfigEntityProperty;
EventHandler ConfigEntityPropertyHandler { get; }
///
/// 增删查改,执行命令之前触发
///
event EventHandler CurdBefore;
EventHandler CurdBeforeHandler { get; }
///
/// 增删查改,执行命令完成后触发
///
event EventHandler CurdAfter;
EventHandler CurdAfterHandler { get; }
///
/// CodeFirst迁移,执行之前触发
///
event EventHandler SyncStructureBefore;
EventHandler SyncStructureBeforeHandler { get; }
///
/// CodeFirst迁移,执行完成触发
///
event EventHandler SyncStructureAfter;
EventHandler SyncStructureAfterHandler { get; }
///
/// Insert/Update自动值处理
///
event EventHandler AuditValue;
EventHandler AuditValueHandler { get; }
///
/// ADO.NET DataReader 拦截
///
event EventHandler AuditDataReader;
EventHandler AuditDataReaderHandler { get; }
///
/// 监视数据库命令对象(执行前,调试)
///
event EventHandler CommandBefore;
EventHandler CommandBeforeHandler { get; }
///
/// 监视数据库命令对象(执行后,用于监视执行性能)
///
event EventHandler CommandAfter;
EventHandler CommandAfterHandler { get; }
///
/// 跟踪开始
///
event EventHandler TraceBefore;
EventHandler TraceBeforeHandler { get; }
///
/// 跟踪结束
///
event EventHandler TraceAfter;
EventHandler TraceAfterHandler { get; }
}
}
namespace FreeSql.Aop
{
#region ParseExpression
public class ParseExpressionEventArgs : EventArgs
{
public ParseExpressionEventArgs(Expression expression, Func freeParse)
{
this.Expression = expression;
this.FreeParse = freeParse;
}
///
/// 内置解析功能,可辅助您进行解析
///
public Func FreeParse { get; }
///
/// 需要您解析的表达式
///
public Expression Expression { get; }
///
/// 解析后的内容
///
public string Result { get; set; }
}
#endregion
#region ConfigEntity/Property
public class ConfigEntityEventArgs : EventArgs
{
public ConfigEntityEventArgs(Type entityType)
{
this.EntityType = entityType;
this.ModifyResult = new TableAttribute();
this.ModifyIndexResult = new List();
}
///
/// 实体类型
///
public Type EntityType { get; }
///
/// 实体配置
///
public TableAttribute ModifyResult { get; }
///
/// 索引配置
///
public List ModifyIndexResult { get; }
}
public class ConfigEntityPropertyEventArgs : EventArgs
{
public ConfigEntityPropertyEventArgs(Type entityType, PropertyInfo property)
{
this.EntityType = entityType;
this.Property = property;
this.ModifyResult = new ColumnAttribute();
}
///
/// 实体类型
///
public Type EntityType { get; }
///
/// 实体的属性
///
public PropertyInfo Property { get; }
///
/// 实体的属性配置
///
public ColumnAttribute ModifyResult { get; }
}
#endregion
#region CurdBefore/After
public class CurdBeforeEventArgs : EventArgs
{
public CurdBeforeEventArgs(Type entityType, TableInfo table, CurdType curdType, string sql, DbParameter[] dbParms) :
this(Guid.NewGuid(), new Stopwatch(), entityType, table, curdType, sql, dbParms, new Dictionary())
{
this.Stopwatch.Start();
}
protected CurdBeforeEventArgs(Guid identifier, Stopwatch stopwatch, Type entityType, TableInfo table, CurdType curdType, string sql, DbParameter[] dbParms, Dictionary states)
{
this.Identifier = identifier;
this.Stopwatch = stopwatch;
this.EntityType = entityType;
this.Table = table;
this.CurdType = curdType;
this.Sql = sql;
this.DbParms = dbParms;
this.States = states;
}
///
/// 标识符,可将 CurdBefore 与 CurdAfter 进行匹配
///
public Guid Identifier { get; protected set; }
protected Stopwatch Stopwatch { get; }
internal Stopwatch StopwatchInternal => Stopwatch;
///
/// 操作类型
///
public CurdType CurdType { get; }
///
/// 实体类型
///
public Type EntityType { get; }
///
/// 实体类型的元数据
///
public TableInfo Table { get; }
///
/// 执行的 SQL
///
public string Sql { get; }
///
/// 参数化命令
///
public DbParameter[] DbParms { get; }
///
/// 状态数据,可与 CurdAfter 共享
///
public Dictionary States { get; protected set; }
}
public enum CurdType { Select, Delete, Update, Insert, InsertOrUpdate }
public class CurdAfterEventArgs : CurdBeforeEventArgs
{
public CurdAfterEventArgs(CurdBeforeEventArgs before, Exception exception, object executeResult) :
base(before.Identifier, before.StopwatchInternal, before.EntityType, before.Table, before.CurdType, before.Sql, before.DbParms, before.States)
{
this.Exception = exception;
this.ExecuteResult = executeResult;
this.Stopwatch.Stop();
}
///
/// 发生的错误
///
public Exception Exception { get; }
///
/// 执行SQL命令,返回的结果
///
public object ExecuteResult { get; }
///
/// 耗时(单位:Ticks)
///
public long ElapsedTicks => this.Stopwatch.ElapsedTicks;
///
/// 耗时(单位:毫秒)
///
public long ElapsedMilliseconds => this.Stopwatch.ElapsedMilliseconds;
}
#endregion
#region SyncStructureBefore/After
public class SyncStructureBeforeEventArgs : EventArgs
{
public SyncStructureBeforeEventArgs(Type[] entityTypes) :
this(Guid.NewGuid(), new Stopwatch(), entityTypes, new Dictionary())
{
this.Stopwatch.Start();
}
protected SyncStructureBeforeEventArgs(Guid identifier, Stopwatch stopwatch, Type[] entityTypes, Dictionary states)
{
this.Identifier = identifier;
this.Stopwatch = stopwatch;
this.EntityTypes = entityTypes;
this.States = states;
}
///
/// 标识符,可将 SyncStructureBeforeEventArgs 与 SyncStructureAfterEventArgs 进行匹配
///
public Guid Identifier { get; protected set; }
protected Stopwatch Stopwatch { get; }
internal Stopwatch StopwatchInternal => Stopwatch;
///
/// 实体类型
///
public Type[] EntityTypes { get; }
///
/// 状态数据,可与 SyncStructureAfter 共享
///
public Dictionary States { get; protected set; }
}
public class SyncStructureAfterEventArgs : SyncStructureBeforeEventArgs
{
public SyncStructureAfterEventArgs(SyncStructureBeforeEventArgs before, string sql, Exception exception) :
base(before.Identifier, before.StopwatchInternal, before.EntityTypes, before.States)
{
this.Sql = sql;
this.Exception = exception;
this.Stopwatch.Stop();
}
///
/// 执行的 SQL
///
public string Sql { get; }
///
/// 发生的错误
///
public Exception Exception { get; }
///
/// 耗时(单位:Ticks)
///
public long ElapsedTicks => this.Stopwatch.ElapsedTicks;
///
/// 耗时(单位:毫秒)
///
public long ElapsedMilliseconds => this.Stopwatch.ElapsedMilliseconds;
}
#endregion
#region AuditValue
public class AuditValueEventArgs : EventArgs
{
public AuditValueEventArgs(AuditValueType autoValueType, ColumnInfo column, PropertyInfo property, object value)
{
this.AuditValueType = autoValueType;
this.Column = column;
this.Property = property;
this._value = value;
}
///
/// 类型
///
public AuditValueType AuditValueType { get; }
///
/// 属性列的元数据
///
public ColumnInfo Column { get; }
///
/// 反射的属性信息
///
public PropertyInfo Property { get; }
///
/// 获取实体的属性值,也可以设置实体的属性新值
///
public object Value
{
get => _value;
set
{
_value = value;
this.ValueIsChanged = true;
}
}
private object _value;
public bool ValueIsChanged { get; private set; }
}
public enum AuditValueType { Update, Insert, InsertOrUpdate }
#endregion
#region AuditDataReader
public class AuditDataReaderEventArgs : EventArgs
{
public AuditDataReaderEventArgs(DbDataReader dataReader, int index)
{
this.DataReader = dataReader;
this.Index = index;
}
///
/// ADO.NET 数据流读取对象
///
public DbDataReader DataReader { get; }
///
/// DataReader 对应的 Index 位置
///
public int Index { get; }
///
/// 获取 Index 对应的值,也可以设置拦截的新值
///
public object Value
{
get
{
if (_valueIsGeted == false)
{
_value = DataReader.GetValue(Index);
_valueIsGeted = true;
}
return _value;
}
set
{
_value = value;
ValueIsChanged = true;
_valueIsGeted = true;
}
}
private object _value;
internal bool _valueIsGeted;
public bool ValueIsChanged { get; private set; }
}
#endregion
#region CommandBefore/After
public class CommandBeforeEventArgs : EventArgs
{
public CommandBeforeEventArgs(DbCommand command) :
this(Guid.NewGuid(), new Stopwatch(), command, new Dictionary())
{
this.Stopwatch.Start();
}
protected CommandBeforeEventArgs(Guid identifier, Stopwatch stopwatch, DbCommand command, Dictionary states)
{
this.Identifier = identifier;
this.Stopwatch = stopwatch;
this.Command = command;
this.States = states;
}
///
/// 标识符,可将 CommandBefore 与 CommandAfter 进行匹配
///
public Guid Identifier { get; protected set; }
protected Stopwatch Stopwatch { get; }
internal Stopwatch StopwatchInternal => Stopwatch;
public DbCommand Command { get; }
///
/// 状态数据,可与 CommandAfter 共享
///
public Dictionary States { get; protected set; }
}
public class CommandAfterEventArgs : CommandBeforeEventArgs
{
public CommandAfterEventArgs(CommandBeforeEventArgs before, Exception exception, string log) :
base(before.Identifier, before.StopwatchInternal, before.Command, before.States)
{
this.Exception = exception;
this.Log = log;
this.Stopwatch.Stop();
}
///
/// 发生的错误
///
public Exception Exception { get; }
///
/// 执行SQL命令,返回的结果
///
public string Log { get; }
///
/// 耗时(单位:Ticks)
///
public long ElapsedTicks => this.Stopwatch.ElapsedTicks;
///
/// 耗时(单位:毫秒)
///
public long ElapsedMilliseconds => this.Stopwatch.ElapsedMilliseconds;
}
#endregion
#region TraceBefore/After
public class TraceBeforeEventArgs : EventArgs
{
public TraceBeforeEventArgs(string operation, object value) :
this(Guid.NewGuid(), new Stopwatch(), operation, value, new Dictionary())
{
this.Stopwatch.Start();
}
protected TraceBeforeEventArgs(Guid identifier, Stopwatch stopwatch, string operation, object value, Dictionary states)
{
this.Identifier = identifier;
this.Stopwatch = stopwatch;
this.Operation = operation;
this.Value = value;
this.States = states;
}
///
/// 标识符,可将 TraceBeforeEventArgs 与 TraceAfterEventArgs 进行匹配
///
public Guid Identifier { get; protected set; }
protected Stopwatch Stopwatch { get; }
internal Stopwatch StopwatchInternal => Stopwatch;
public string Operation { get; }
public object Value { get; }
///
/// 状态数据,可与 TraceAfter 共享
///
public Dictionary States { get; protected set; }
}
public class TraceAfterEventArgs : TraceBeforeEventArgs
{
public TraceAfterEventArgs(TraceBeforeEventArgs before, string remark, Exception exception) :
base(before.Identifier, before.StopwatchInternal, before.Operation, before.Value, before.States)
{
this.Remark = remark;
this.Exception = exception;
this.Stopwatch.Stop();
}
///
/// 备注
///
public string Remark { get; }
///
/// 发生的错误
///
public Exception Exception { get; }
///
/// 耗时(单位:Ticks)
///
public long ElapsedTicks => this.Stopwatch.ElapsedTicks;
///
/// 耗时(单位:毫秒)
///
public long ElapsedMilliseconds => this.Stopwatch.ElapsedMilliseconds;
}
#endregion
}