Browse Source

Add db.Aop.DataExecuted

SqlSugar5
sunkaixuan 2 years ago
parent
commit
2ea420bc7c
  1. 1
      Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs
  2. 23
      Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs
  3. 2
      Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs
  4. 19
      Src/Asp.Net/SqlSugar/Enum/DataFilterType.cs
  5. 1
      Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs

1
Src/Asp.Net/SqlSugar/Abstract/AopProvider/AopProvider.cs

@ -20,5 +20,6 @@ namespace SqlSugar
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; } }
public virtual Action<object, DataAfterModel> DataExecuted { set { this.Context.CurrentConnectionConfig.AopEvents.DataExecuted = value; } }
}
}

23
Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindAccessory.cs

@ -16,6 +16,7 @@ namespace SqlSugar
string types = null;
var fieldNames = GetDataReaderNames(dataReader,ref types);
string cacheKey = GetCacheKey(type,fieldNames) + types;
var dataAfterFunc = context.CurrentConnectionConfig?.AopEvents?.DataExecuted;
IDataReaderEntityBuilder<T> entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () =>
{
var cacheResult = new IDataReaderEntityBuilder<T>(context, dataReader,fieldNames).CreateBuilder(type);
@ -29,6 +30,7 @@ namespace SqlSugar
{
result.Add(entytyList.Build(dataReader));
}
ExecuteDataAfterFun(context, dataAfterFunc, result);
}
catch (Exception ex)
{
@ -36,12 +38,14 @@ namespace SqlSugar
}
return result;
}
protected async Task<List<T>> GetEntityListAsync<T>(SqlSugarProvider context, IDataReader dataReader)
{
Type type = typeof(T);
string types = null;
var fieldNames = GetDataReaderNames(dataReader,ref types);
string cacheKey = GetCacheKey(type, fieldNames)+types;
var dataAfterFunc = context.CurrentConnectionConfig?.AopEvents?.DataExecuted;
IDataReaderEntityBuilder<T> entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () =>
{
var cacheResult = new IDataReaderEntityBuilder<T>(context, dataReader, fieldNames).CreateBuilder(type);
@ -55,6 +59,7 @@ namespace SqlSugar
{
result.Add(entytyList.Build(dataReader));
}
ExecuteDataAfterFun(context, dataAfterFunc, result);
}
catch (Exception ex)
{
@ -62,8 +67,24 @@ namespace SqlSugar
}
return result;
}
private static void ExecuteDataAfterFun<T>(SqlSugarProvider context, Action<object, DataAfterModel> dataAfterFunc, List<T> result)
{
if (dataAfterFunc != null)
{
var entity = context.EntityMaintenance.GetEntityInfo<T>();
foreach (var item in result)
{
dataAfterFunc(item, new DataAfterModel()
{
EntityColumnInfos = entity.Columns,
Entity = entity,
EntityValue = item
});
}
}
}
private string GetCacheKey(Type type,List<string> keys)
private string GetCacheKey(Type type,List<string> keys)
{
StringBuilder sb = new StringBuilder("DataReaderToList.");
sb.Append(type.FullName);

2
Src/Asp.Net/SqlSugar/Entities/ConnectionConfig.cs

@ -88,7 +88,7 @@ namespace SqlSugar
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 Action<object, DataAfterModel> DataExecuted { get; set; }
}
public class ConfigureExternalServices
{

19
Src/Asp.Net/SqlSugar/Enum/DataFilterType.cs

@ -31,4 +31,23 @@ namespace SqlSugar
this.EntityColumnInfo.PropertyInfo.SetValue(EntityValue, value);
}
}
public class DataAfterModel
{
public List<EntityColumnInfo> EntityColumnInfos { get; set; }
public object EntityValue { get; set; }
public EntityInfo Entity { get; set; }
public object GetValue(string propertyName)
{
var propety=EntityColumnInfos.FirstOrDefault(it => it.PropertyName == propertyName);
Check.ExceptionEasy(propety==null,$"Aop.DataExecuted error . { Entity.EntityName} no property {propertyName}.", $"Aop.DataExecuted 出错 {Entity.EntityName}不存在属性{propertyName}");
return propety.PropertyInfo.GetValue(EntityValue);
}
public void SetValue(string propertyName,object value)
{
var propety = EntityColumnInfos.FirstOrDefault(it => it.PropertyName == propertyName);
Check.ExceptionEasy(propety == null, $"Aop.DataExecuted error . { Entity.EntityName} no property {propertyName}.", $"Aop.DataExecuted 出错 {Entity.EntityName}不存在属性{propertyName}");
propety.PropertyInfo.SetValue(EntityValue,value);
}
}
}

1
Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs

@ -87,6 +87,7 @@ namespace SqlSugar
OnExecutingChangeSql=it.AopEvents?.OnExecutingChangeSql,
OnLogExecuted=it.AopEvents?.OnLogExecuted,
OnLogExecuting= it.AopEvents?.OnLogExecuting,
DataExecuted = it.AopEvents?.DataExecuted,
},
ConfigId = it.ConfigId,
ConfigureExternalServices =it.ConfigureExternalServices==null?null:new ConfigureExternalServices() {

Loading…
Cancel
Save