mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-01 10:10:16 +08:00
Add db.Aop.DataExecuted
This commit is contained in:
parent
ba451b478b
commit
2ea420bc7c
@ -20,5 +20,6 @@ namespace SqlSugar
|
|||||||
public Action<string, SugarParameter[]> OnLogExecuted { set { this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuted = 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 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, DataFilterModel> DataExecuting { set { this.Context.CurrentConnectionConfig.AopEvents.DataExecuting = value; } }
|
||||||
|
public virtual Action<object, DataAfterModel> DataExecuted { set { this.Context.CurrentConnectionConfig.AopEvents.DataExecuted = value; } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ namespace SqlSugar
|
|||||||
string types = null;
|
string types = null;
|
||||||
var fieldNames = GetDataReaderNames(dataReader,ref types);
|
var fieldNames = GetDataReaderNames(dataReader,ref types);
|
||||||
string cacheKey = GetCacheKey(type,fieldNames) + types;
|
string cacheKey = GetCacheKey(type,fieldNames) + types;
|
||||||
|
var dataAfterFunc = context.CurrentConnectionConfig?.AopEvents?.DataExecuted;
|
||||||
IDataReaderEntityBuilder<T> entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () =>
|
IDataReaderEntityBuilder<T> entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () =>
|
||||||
{
|
{
|
||||||
var cacheResult = new IDataReaderEntityBuilder<T>(context, dataReader,fieldNames).CreateBuilder(type);
|
var cacheResult = new IDataReaderEntityBuilder<T>(context, dataReader,fieldNames).CreateBuilder(type);
|
||||||
@ -29,6 +30,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
result.Add(entytyList.Build(dataReader));
|
result.Add(entytyList.Build(dataReader));
|
||||||
}
|
}
|
||||||
|
ExecuteDataAfterFun(context, dataAfterFunc, result);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -36,12 +38,14 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async Task<List<T>> GetEntityListAsync<T>(SqlSugarProvider context, IDataReader dataReader)
|
protected async Task<List<T>> GetEntityListAsync<T>(SqlSugarProvider context, IDataReader dataReader)
|
||||||
{
|
{
|
||||||
Type type = typeof(T);
|
Type type = typeof(T);
|
||||||
string types = null;
|
string types = null;
|
||||||
var fieldNames = GetDataReaderNames(dataReader,ref types);
|
var fieldNames = GetDataReaderNames(dataReader,ref types);
|
||||||
string cacheKey = GetCacheKey(type, fieldNames)+types;
|
string cacheKey = GetCacheKey(type, fieldNames)+types;
|
||||||
|
var dataAfterFunc = context.CurrentConnectionConfig?.AopEvents?.DataExecuted;
|
||||||
IDataReaderEntityBuilder<T> entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () =>
|
IDataReaderEntityBuilder<T> entytyList = context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey, () =>
|
||||||
{
|
{
|
||||||
var cacheResult = new IDataReaderEntityBuilder<T>(context, dataReader, fieldNames).CreateBuilder(type);
|
var cacheResult = new IDataReaderEntityBuilder<T>(context, dataReader, fieldNames).CreateBuilder(type);
|
||||||
@ -55,6 +59,7 @@ namespace SqlSugar
|
|||||||
{
|
{
|
||||||
result.Add(entytyList.Build(dataReader));
|
result.Add(entytyList.Build(dataReader));
|
||||||
}
|
}
|
||||||
|
ExecuteDataAfterFun(context, dataAfterFunc, result);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -62,8 +67,24 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
return result;
|
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.");
|
StringBuilder sb = new StringBuilder("DataReaderToList.");
|
||||||
sb.Append(type.FullName);
|
sb.Append(type.FullName);
|
||||||
|
@ -88,7 +88,7 @@ namespace SqlSugar
|
|||||||
public Action<string, SugarParameter[]> OnLogExecuted { get; set; }
|
public Action<string, SugarParameter[]> OnLogExecuted { get; set; }
|
||||||
public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> OnExecutingChangeSql { get; set; }
|
public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> OnExecutingChangeSql { get; set; }
|
||||||
public Action<object, DataFilterModel> DataExecuting { get; set; }
|
public Action<object, DataFilterModel> DataExecuting { get; set; }
|
||||||
|
public Action<object, DataAfterModel> DataExecuted { get; set; }
|
||||||
}
|
}
|
||||||
public class ConfigureExternalServices
|
public class ConfigureExternalServices
|
||||||
{
|
{
|
||||||
|
@ -31,4 +31,23 @@ namespace SqlSugar
|
|||||||
this.EntityColumnInfo.PropertyInfo.SetValue(EntityValue, value);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,6 +87,7 @@ namespace SqlSugar
|
|||||||
OnExecutingChangeSql=it.AopEvents?.OnExecutingChangeSql,
|
OnExecutingChangeSql=it.AopEvents?.OnExecutingChangeSql,
|
||||||
OnLogExecuted=it.AopEvents?.OnLogExecuted,
|
OnLogExecuted=it.AopEvents?.OnLogExecuted,
|
||||||
OnLogExecuting= it.AopEvents?.OnLogExecuting,
|
OnLogExecuting= it.AopEvents?.OnLogExecuting,
|
||||||
|
DataExecuted = it.AopEvents?.DataExecuted,
|
||||||
},
|
},
|
||||||
ConfigId = it.ConfigId,
|
ConfigId = it.ConfigId,
|
||||||
ConfigureExternalServices =it.ConfigureExternalServices==null?null:new ConfigureExternalServices() {
|
ConfigureExternalServices =it.ConfigureExternalServices==null?null:new ConfigureExternalServices() {
|
||||||
|
Loading…
Reference in New Issue
Block a user