diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UAopTest.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UAopTest.cs index 4a672e0a6..f974a6e49 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/UAopTest.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UAopTest.cs @@ -24,6 +24,38 @@ namespace OrmTest x=SqlSugar.SqlFunc.Subqueryable().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().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"); + } } } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs index 22f62f05c..a405ac1e9 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs @@ -19,5 +19,6 @@ namespace SqlSugar public Action OnLogExecuting { set { this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuting= value; } } public Action OnLogExecuted { set { this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuted = value; } } public Func> OnExecutingChangeSql { set { this.Context.CurrentConnectionConfig.AopEvents.OnExecutingChangeSql = value; } } + public virtual Action DataExecuting { set { this.Context.CurrentConnectionConfig.AopEvents.DataExecuting = value; } } } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs index b1fd19583..6b12fe3ac 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableProvider.cs @@ -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 insertItem) { foreach (var column in item as Dictionary) diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index d9ae2722c..9428a7554 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -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)) diff --git a/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs b/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs index 419689bd4..a7719d6d9 100644 --- a/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs +++ b/Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs @@ -65,6 +65,8 @@ namespace SqlSugar public Action OnLogExecuting { get; set; } public Action OnLogExecuted { get; set; } public Func> OnExecutingChangeSql { get; set; } + public Action DataExecuting { get; set; } + } public class ConfigureExternalServices { diff --git a/Src/Asp.Net/SqlSugar/Enum/DataFilterType.cs b/Src/Asp.Net/SqlSugar/Enum/DataFilterType.cs new file mode 100644 index 000000000..c35c486d6 --- /dev/null +++ b/Src/Asp.Net/SqlSugar/Enum/DataFilterType.cs @@ -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); + } + } +} diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj index d1ea05a5e..76d5d851f 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj @@ -96,6 +96,7 @@ +