首页 > Python基础教程 >
-
C#教程之ENTITY FRAMEWORK 高性能 泛型缓存+动态LAMB
前言:自学CSharp挺长时间的了,这是我第一编博客,跟大家分享一下。如有不足地方请多多包涵,也欢迎大家提出更好的意见,下面开始进入正题。
一、泛型缓存
1.概念:1、泛型(泛型也是一种推断类型,从而实现不同类型可以使用同一个类或者方法、属性,大大的减少代码的冗余) 2、缓存(暂时存储)
2.应用场景(通俗易懂的理解):场景1、执行同参数的泛型方法时为了提高性能减少对方法体执行的次数,就可以使用泛型缓存之前执行过的返回值。
还有其他应用场景不一 一 举例,需要认真体会。
3.代码执行流程图
调用缓存方法的截图
完整的参考代码
/// <summary>
/// Lambda缓存
/// </summary>
/// <typeparam name="TSource">源类型</typeparam>
/// <typeparam name="TParameters">参数类型</typeparam>
public class LambdaCache<TSource, TParameters> where TSource : class where TParameters : class
{
private static Dictionary<string, Func<TSource, bool>> MapLambda { get; set; }
private static int CaceCount { get; set; }
static LambdaCache()
{
MapLambda = new Dictionary<string, Func<TSource, bool>>();
CaceCount = 20;
}
/// <summary>
/// 获取缓存动态Lambda
/// </summary>
/// <param name="parameters">参数对象</param>
/// <returns></returns>
public static Func<TSource, bool> GetDynamicLambda(TParameters parameters)
{
string key = string.Empty;
Dictionary<string, string> keyValues = GetPropertiesValue(parameters, out key);
if (!MapLambda.ContainsKey(key))
{
if (MapLambda.Count == CaceCount)
{
MapLambda.Clear();
}
MapLambda.Add(key, ExpressionExt.DynamicLambda<TSource>(keyValues));
}
return MapLambda[key];
}
/// <summary>
/// 获取对象属性值
/// </summary>
/// <param name="obj">对象</param>
/// <returns></returns>
private static Dictionary<string, string> GetPropertiesValue(object obj, out string key)
{
PropertyInfo[] properties = obj.GetType().GetProperties();
Dictionary<string, string> keyValues = new Dictionary<string, string>();
string value = string.Empty;
foreach (var item in properties)
{
var data = item?.GetValue(obj, null);
if (data is DateTime == false && !string.IsNullOrEmpty(data?.ToString()))
{
keyValues.Add(item.Name, data.ToString());
value += data;
}
}
if (string.IsNullOrEmpty(value))
{
value = obj.GetType().Name;//如果不选择任何条件则值为Null 考虑到参数实体名称不一致所以直接初始化为实体名称
}
key = value;
return keyValues;
}
}
/// <summary>
/// 动态Lambda
/// </summary>
/// <typeparam name="TSource">源类型</typeparam>
/// <param name="keyValues">参数键值对</param>
/// <returns></returns>
public static Func<TSource, bool> DynamicLambda<TSource>(Dictionary<string, string> keyValues)
{
//定义Lambda参数,列如常写的“x=>”
ParameterExpression parameter = Expression.Parameter(typeof(TSource), "x");
List<Expression> expressions = new List<Expression>();
foreach (var item in keyValues)
{
//定义lamada的属性成员
MemberExpression member = Expression.PropertyOrField(parameter, item.Key);
//定义筛选的操作
Expression expression = Expression.Equal(member, Expression.Constant(item.Value, member.Type));
expressions.Add(expression);
}
if (expressions.Count == 0)
{
return (TSource t1) => { return true; };//构造委托Lambda
}
Expression whereExpression = null;
//通过循环拼接 Lambda 完整表达式
foreach (var item in expressions)
{
if (whereExpression == null)
{
whereExpression = item;
}
else
{
whereExpression = Expression.And(whereExpression, item);
}
}
return Expression.Lambda<Func<TSource, bool>>(whereExpression, parameter).Compile();
}