mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 13:06:50 +08:00
Add db.Aop.DataExecuting
This commit is contained in:
parent
2d5f95c167
commit
31231b12f4
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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; } }
|
||||
}
|
||||
}
|
||||
|
@ -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>)
|
||||
|
@ -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))
|
||||
|
@ -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
|
||||
{
|
||||
|
34
Src/Asp.Net/SqlSugar/Enum/DataFilterType.cs
Normal file
34
Src/Asp.Net/SqlSugar/Enum/DataFilterType.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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" />
|
||||
|
Loading…
Reference in New Issue
Block a user