Add db.Aop.DataExecuting

This commit is contained in:
sunkaixuna 2021-08-01 14:11:51 +08:00
parent 2d5f95c167
commit 31231b12f4
7 changed files with 98 additions and 0 deletions

View File

@ -24,6 +24,38 @@ namespace OrmTest
x=SqlSugar.SqlFunc.Subqueryable<Order>().Select(s=>s.Id)
}).ToList();
db.CurrentConnectionConfig.ConfigureExternalServices = new SqlSugar.ConfigureExternalServices();
db.Aop.DataExecuting = (value, entityInfo) =>
{
if (entityInfo.PropertyName == "Price"&&entityInfo.OperationType==SqlSugar.DataFilterType.InsertByObject)
{
entityInfo.SetValue(1);
}
if (entityInfo.PropertyName == "CreateTime" && entityInfo.OperationType == SqlSugar.DataFilterType.InsertByObject)
{
entityInfo.SetValue(DateTime.Now);
}
if (entityInfo.PropertyName == "Price" && entityInfo.OperationType == SqlSugar.DataFilterType.UpdateByObject)
{
entityInfo.SetValue(-1);
}
};
var id= db.Insertable(new Order()
{
CustomId = 1,
Name = "a"
}).ExecuteReturnIdentity();
var data = db.Queryable<Order>().InSingle(id);
if (data.Price != 1)
{
throw new Exception("Unit Aop error");
}
db.Updateable(data).ExecuteCommand();
if (data.Price != -1)
{
throw new Exception("Unit Aop error");
}
}
}
}

View File

@ -19,5 +19,6 @@ namespace SqlSugar
public Action<string, SugarParameter[]> OnLogExecuting { set { this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuting= value; } }
public Action<string, SugarParameter[]> OnLogExecuted { set { this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuted = value; } }
public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> OnExecutingChangeSql { set { this.Context.CurrentConnectionConfig.AopEvents.OnExecutingChangeSql = value; } }
public virtual Action<object, DataFilterModel> DataExecuting { set { this.Context.CurrentConnectionConfig.AopEvents.DataExecuting = value; } }
}
}

View File

@ -498,12 +498,26 @@ namespace SqlSugar
}
else
{
DataAop(item);
SetInsertItemByEntity(i, item, insertItem);
}
this.InsertBuilder.DbColumnInfoList.AddRange(insertItem);
++i;
}
}
private void DataAop(T item)
{
var dataEvent=this.Context.CurrentConnectionConfig.AopEvents?.DataExecuting;
if (dataEvent != null && item != null)
{
foreach (var columnInfo in this.EntityInfo.Columns)
{
dataEvent(columnInfo.PropertyInfo.GetValue(item, null), new DataFilterModel() { OperationType = DataFilterType.InsertByObject,EntityValue=item, EntityColumnInfo = columnInfo });
}
}
}
private void SetInsertItemByDic(int i, T item, List<DbColumnInfo> insertItem)
{
foreach (var column in item as Dictionary<string, object>)

View File

@ -440,12 +440,26 @@ namespace SqlSugar
}
else
{
DataAop(item);
SetUpdateItemByEntity(i, item, updateItem);
}
++i;
}
this.columns = this.UpdateBuilder.DbColumnInfoList;
}
private void DataAop(T item)
{
var dataEvent = this.Context.CurrentConnectionConfig.AopEvents?.DataExecuting;
if (dataEvent != null && item != null)
{
foreach (var columnInfo in this.EntityInfo.Columns)
{
dataEvent(columnInfo.PropertyInfo.GetValue(item, null), new DataFilterModel() { OperationType = DataFilterType.UpdateByObject, EntityValue = item, EntityColumnInfo = columnInfo });
}
}
}
private void CheckTranscodeing(bool checkIsJson = true)
{
if (this.EntityInfo.Columns.Any(it => it.IsTranscoding))

View File

@ -65,6 +65,8 @@ namespace SqlSugar
public Action<string, SugarParameter[]> OnLogExecuting { get; set; }
public Action<string, SugarParameter[]> OnLogExecuted { get; set; }
public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> OnExecutingChangeSql { get; set; }
public Action<object, DataFilterModel> DataExecuting { get; set; }
}
public class ConfigureExternalServices
{

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public enum DataFilterType
{
UpdateByObject = 0,
InsertByObject = 1
}
public class DataFilterModel
{
public DataFilterType OperationType { get; set; }
public EntityColumnInfo EntityColumnInfo { get; set; }
public object EntityValue { get; set; }
public string PropertyName { get { return EntityColumnInfo.PropertyInfo.Name; } }
public string EntityName { get { return EntityColumnInfo.EntityName; } }
public void SetValue(object value)
{
var type = EntityColumnInfo.PropertyInfo.PropertyType;
if (value != null && value.GetType() != type)
{
value = UtilMethods.ChangeType2(value, type);
}
this.EntityColumnInfo.PropertyInfo.SetValue(EntityValue, value);
}
}
}

View File

@ -96,6 +96,7 @@
<Compile Include="Entities\SingleColumnsEntity.cs" />
<Compile Include="Entities\StackTraceInfo.cs" />
<Compile Include="Entities\SubInsertTree.cs" />
<Compile Include="Enum\DataFilterType.cs" />
<Compile Include="Enum\ReportableDateType.cs" />
<Compile Include="ExpressionsToSql\Common\MapperExpression.cs" />
<Compile Include="ExpressionsToSql\DbMethods\SqlFuncExtendsion.cs" />