Skip to content

Commit 5782d6e

Browse files
committed
- 增加 Aop.AuditDataReader 参数属性 PropertyInfo,实现自定义拦截;
1 parent a3cb3d3 commit 5782d6e

File tree

5 files changed

+131
-28
lines changed

5 files changed

+131
-28
lines changed

FreeSql/FreeSql.xml

Lines changed: 103 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql/Interface/IAop.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,11 @@ public enum AuditValueType { Update, Insert, InsertOrUpdate }
360360
#region AuditDataReader
361361
public class AuditDataReaderEventArgs : EventArgs
362362
{
363-
public AuditDataReaderEventArgs(DbDataReader dataReader, int index)
363+
public AuditDataReaderEventArgs(DbDataReader dataReader, int index, PropertyInfo property)
364364
{
365365
this.DataReader = dataReader;
366366
this.Index = index;
367+
this.Property = property;
367368
}
368369

369370
/// <summary>
@@ -375,6 +376,10 @@ public AuditDataReaderEventArgs(DbDataReader dataReader, int index)
375376
/// </summary>
376377
public int Index { get; }
377378
/// <summary>
379+
/// DataReader 对应的 PropertyInfo
380+
/// </summary>
381+
public PropertyInfo Property { get; }
382+
/// <summary>
378383
/// 获取 Index 对应的值,也可以设置拦截的新值
379384
/// </summary>
380385
public object Value

FreeSql/Internal/CommonExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ public object ReadAnonymous(ReadAnonymousTypeInfo parent, DbDataReader dr, ref i
570570
return Utils.GetDataReaderValue(parent.Property.PropertyType, null);
571571
return Utils.GetDataReaderValue(parent.CsType, null);
572572
}
573-
object objval = Utils.InternalDataReaderGetValue(_common, dr, ++index); // dr.GetValue(++index);
573+
object objval = Utils.InternalDataReaderGetValue(_common, dr, ++index, parent.Property); // dr.GetValue(++index);
574574
if (dbValue != null) dbValue.DbValue = objval == DBNull.Value ? null : objval;
575575
if (parent.CsType != parent.MapType)
576576
objval = Utils.GetDataReaderValue(parent.MapType, objval);

FreeSql/Internal/CommonProvider/SelectProvider/Select0ProviderReader.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ public GetAllFieldExpressionTreeInfo GetAllFieldExpressionTreeLevel2(bool isRere
600600
var name = dr.GetName(a);
601601
//expando[name] = row2.GetValue(a);
602602
if (expandodic.ContainsKey(name)) continue;
603-
expandodic.Add(name, Utils.InternalDataReaderGetValue(_commonUtils, dr, a));
603+
expandodic.Add(name, Utils.InternalDataReaderGetValue(_commonUtils, dr, a, null));
604604
}
605605
//expando = expandodic;
606606
return (T1)((object)expandodic);
@@ -682,8 +682,7 @@ public GetAllFieldExpressionTreeInfo GetAllFieldExpressionTreeLevel2(bool isRere
682682
var propGetSetMethod = prop.GetSetMethod(true);
683683
Expression readExpAssign = null; //加速缓存
684684
if (prop.PropertyType.IsArray) readExpAssign = Expression.New(Utils.RowInfo.Constructor,
685-
Utils.GetDataReaderValueBlockExpression(prop.PropertyType, Expression.Call(Utils.MethodDataReaderGetValue, new Expression[] { Expression.Constant(_commonUtils), rowExp, dataIndexExp })),
686-
//Expression.Call(Utils.MethodGetDataReaderValue, new Expression[] { Expression.Constant(prop.PropertyType), Expression.Call(rowExp, Utils.MethodDataReaderGetValue, dataIndexExp) }),
685+
Utils.GetDataReaderValueBlockExpression(prop.PropertyType, Expression.Call(Utils.MethodDataReaderGetValue, new Expression[] { Expression.Constant(_commonUtils), rowExp, dataIndexExp, Expression.Constant(prop) })),
687686
Expression.Add(dataIndexExp, Expression.Constant(1))
688687
);
689688
else
@@ -692,8 +691,7 @@ public GetAllFieldExpressionTreeInfo GetAllFieldExpressionTreeLevel2(bool isRere
692691
if (proptypeGeneric.IsNullableType()) proptypeGeneric = proptypeGeneric.GetGenericArguments().First();
693692
if (proptypeGeneric.IsEnum ||
694693
Utils.dicExecuteArrayRowReadClassOrTuple.ContainsKey(proptypeGeneric)) readExpAssign = Expression.New(Utils.RowInfo.Constructor,
695-
Utils.GetDataReaderValueBlockExpression(prop.PropertyType, Expression.Call(Utils.MethodDataReaderGetValue, new Expression[] { Expression.Constant(_commonUtils), rowExp, dataIndexExp })),
696-
//Expression.Call(Utils.MethodGetDataReaderValue, new Expression[] { Expression.Constant(prop.PropertyType), Expression.Call(rowExp, Utils.MethodDataReaderGetValue, dataIndexExp) }),
694+
Utils.GetDataReaderValueBlockExpression(prop.PropertyType, Expression.Call(Utils.MethodDataReaderGetValue, new Expression[] { Expression.Constant(_commonUtils), rowExp, dataIndexExp, Expression.Constant(prop) })),
697695
Expression.Add(dataIndexExp, Expression.Constant(1))
698696
);
699697
else
@@ -739,7 +737,8 @@ public GetAllFieldExpressionTreeInfo GetAllFieldExpressionTreeLevel2(bool isRere
739737
foreach (var col in tb.Table.Columns.Values)
740738
{
741739
var drvalType = col.Attribute.MapType.NullableTypeOrThis();
742-
var propGetSetMethod = tb.Table.Properties[col.CsName].GetSetMethod(true);
740+
var colprop = tb.Table.Properties[col.CsName];
741+
var propGetSetMethod = colprop.GetSetMethod(true);
743742
if (col.CsType == col.Attribute.MapType &&
744743
_orm.Aop.AuditDataReaderHandler == null &&
745744
_dicMethodDataReaderGetValue.TryGetValue(col.Attribute.MapType.NullableTypeOrThis(), out var drGetValueMethod))
@@ -755,7 +754,7 @@ public GetAllFieldExpressionTreeInfo GetAllFieldExpressionTreeLevel2(bool isRere
755754
{
756755
var drvalExpCatch = Utils.GetDataReaderValueBlockExpression(
757756
col.CsType,
758-
Expression.Call(Utils.MethodDataReaderGetValue, new Expression[] { Expression.Constant(_commonUtils), rowExp, Expression.Constant(colidx) })
757+
Expression.Call(Utils.MethodDataReaderGetValue, new Expression[] { Expression.Constant(_commonUtils), rowExp, Expression.Constant(colidx), Expression.Constant(colprop) })
759758
);
760759
blockExp.Add(Expression.TryCatch(
761760
Expression.Call(retExp, propGetSetMethod, drvalExp),
@@ -780,7 +779,7 @@ public GetAllFieldExpressionTreeInfo GetAllFieldExpressionTreeLevel2(bool isRere
780779
{
781780
var drvalExp = Utils.GetDataReaderValueBlockExpression(
782781
col.CsType,
783-
Expression.Call(Utils.MethodDataReaderGetValue, new Expression[] { Expression.Constant(_commonUtils), rowExp, Expression.Constant(colidx) })
782+
Expression.Call(Utils.MethodDataReaderGetValue, new Expression[] { Expression.Constant(_commonUtils), rowExp, Expression.Constant(colidx), Expression.Constant(colprop) })
784783
);
785784
blockExp.Add(Expression.Call(retExp, propGetSetMethod, Expression.Convert(drvalExp, col.CsType)));
786785
}

0 commit comments

Comments
 (0)