Skip to content

Commit

Permalink
- 增加 Aop.AuditDataReader 参数属性 PropertyInfo,实现自定义拦截;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Oct 18, 2023
1 parent a3cb3d3 commit 5782d6e
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 28 deletions.
103 changes: 103 additions & 0 deletions FreeSql/FreeSql.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion FreeSql/Interface/IAop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,11 @@ public enum AuditValueType { Update, Insert, InsertOrUpdate }
#region AuditDataReader
public class AuditDataReaderEventArgs : EventArgs
{
public AuditDataReaderEventArgs(DbDataReader dataReader, int index)
public AuditDataReaderEventArgs(DbDataReader dataReader, int index, PropertyInfo property)
{
this.DataReader = dataReader;
this.Index = index;
this.Property = property;
}

/// <summary>
Expand All @@ -375,6 +376,10 @@ public AuditDataReaderEventArgs(DbDataReader dataReader, int index)
/// </summary>
public int Index { get; }
/// <summary>
/// DataReader 对应的 PropertyInfo
/// </summary>
public PropertyInfo Property { get; }
/// <summary>
/// 获取 Index 对应的值,也可以设置拦截的新值
/// </summary>
public object Value
Expand Down
2 changes: 1 addition & 1 deletion FreeSql/Internal/CommonExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ public object ReadAnonymous(ReadAnonymousTypeInfo parent, DbDataReader dr, ref i
return Utils.GetDataReaderValue(parent.Property.PropertyType, null);
return Utils.GetDataReaderValue(parent.CsType, null);
}
object objval = Utils.InternalDataReaderGetValue(_common, dr, ++index); // dr.GetValue(++index);
object objval = Utils.InternalDataReaderGetValue(_common, dr, ++index, parent.Property); // dr.GetValue(++index);
if (dbValue != null) dbValue.DbValue = objval == DBNull.Value ? null : objval;
if (parent.CsType != parent.MapType)
objval = Utils.GetDataReaderValue(parent.MapType, objval);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ public GetAllFieldExpressionTreeInfo GetAllFieldExpressionTreeLevel2(bool isRere
var name = dr.GetName(a);
//expando[name] = row2.GetValue(a);
if (expandodic.ContainsKey(name)) continue;
expandodic.Add(name, Utils.InternalDataReaderGetValue(_commonUtils, dr, a));
expandodic.Add(name, Utils.InternalDataReaderGetValue(_commonUtils, dr, a, null));
}
//expando = expandodic;
return (T1)((object)expandodic);
Expand Down Expand Up @@ -682,8 +682,7 @@ public GetAllFieldExpressionTreeInfo GetAllFieldExpressionTreeLevel2(bool isRere
var propGetSetMethod = prop.GetSetMethod(true);
Expression readExpAssign = null; //加速缓存
if (prop.PropertyType.IsArray) readExpAssign = Expression.New(Utils.RowInfo.Constructor,
Utils.GetDataReaderValueBlockExpression(prop.PropertyType, Expression.Call(Utils.MethodDataReaderGetValue, new Expression[] { Expression.Constant(_commonUtils), rowExp, dataIndexExp })),
//Expression.Call(Utils.MethodGetDataReaderValue, new Expression[] { Expression.Constant(prop.PropertyType), Expression.Call(rowExp, Utils.MethodDataReaderGetValue, dataIndexExp) }),
Utils.GetDataReaderValueBlockExpression(prop.PropertyType, Expression.Call(Utils.MethodDataReaderGetValue, new Expression[] { Expression.Constant(_commonUtils), rowExp, dataIndexExp, Expression.Constant(prop) })),
Expression.Add(dataIndexExp, Expression.Constant(1))
);
else
Expand All @@ -692,8 +691,7 @@ public GetAllFieldExpressionTreeInfo GetAllFieldExpressionTreeLevel2(bool isRere
if (proptypeGeneric.IsNullableType()) proptypeGeneric = proptypeGeneric.GetGenericArguments().First();
if (proptypeGeneric.IsEnum ||
Utils.dicExecuteArrayRowReadClassOrTuple.ContainsKey(proptypeGeneric)) readExpAssign = Expression.New(Utils.RowInfo.Constructor,
Utils.GetDataReaderValueBlockExpression(prop.PropertyType, Expression.Call(Utils.MethodDataReaderGetValue, new Expression[] { Expression.Constant(_commonUtils), rowExp, dataIndexExp })),
//Expression.Call(Utils.MethodGetDataReaderValue, new Expression[] { Expression.Constant(prop.PropertyType), Expression.Call(rowExp, Utils.MethodDataReaderGetValue, dataIndexExp) }),
Utils.GetDataReaderValueBlockExpression(prop.PropertyType, Expression.Call(Utils.MethodDataReaderGetValue, new Expression[] { Expression.Constant(_commonUtils), rowExp, dataIndexExp, Expression.Constant(prop) })),
Expression.Add(dataIndexExp, Expression.Constant(1))
);
else
Expand Down Expand Up @@ -739,7 +737,8 @@ public GetAllFieldExpressionTreeInfo GetAllFieldExpressionTreeLevel2(bool isRere
foreach (var col in tb.Table.Columns.Values)
{
var drvalType = col.Attribute.MapType.NullableTypeOrThis();
var propGetSetMethod = tb.Table.Properties[col.CsName].GetSetMethod(true);
var colprop = tb.Table.Properties[col.CsName];
var propGetSetMethod = colprop.GetSetMethod(true);
if (col.CsType == col.Attribute.MapType &&
_orm.Aop.AuditDataReaderHandler == null &&
_dicMethodDataReaderGetValue.TryGetValue(col.Attribute.MapType.NullableTypeOrThis(), out var drGetValueMethod))
Expand All @@ -755,7 +754,7 @@ public GetAllFieldExpressionTreeInfo GetAllFieldExpressionTreeLevel2(bool isRere
{
var drvalExpCatch = Utils.GetDataReaderValueBlockExpression(
col.CsType,
Expression.Call(Utils.MethodDataReaderGetValue, new Expression[] { Expression.Constant(_commonUtils), rowExp, Expression.Constant(colidx) })
Expression.Call(Utils.MethodDataReaderGetValue, new Expression[] { Expression.Constant(_commonUtils), rowExp, Expression.Constant(colidx), Expression.Constant(colprop) })
);
blockExp.Add(Expression.TryCatch(
Expression.Call(retExp, propGetSetMethod, drvalExp),
Expand All @@ -780,7 +779,7 @@ public GetAllFieldExpressionTreeInfo GetAllFieldExpressionTreeLevel2(bool isRere
{
var drvalExp = Utils.GetDataReaderValueBlockExpression(
col.CsType,
Expression.Call(Utils.MethodDataReaderGetValue, new Expression[] { Expression.Constant(_commonUtils), rowExp, Expression.Constant(colidx) })
Expression.Call(Utils.MethodDataReaderGetValue, new Expression[] { Expression.Constant(_commonUtils), rowExp, Expression.Constant(colidx), Expression.Constant(colprop) })
);
blockExp.Add(Expression.Call(retExp, propGetSetMethod, Expression.Convert(drvalExp, col.CsType)));
}
Expand Down
Loading

0 comments on commit 5782d6e

Please sign in to comment.