static void TestGetValueCache(int count = 1000000) { var user = new UserVO(); var propertyInfo = user.GetType().GetProperty("UserName"); SpeedTest.ConsoleTime("手动取值", count, () => { var a = user.UserName; }); SpeedTest.ConsoleTime("表达式树取值", count, () => { var a = PropertyGetCacheManger.Cache(propertyInfo, user); }); SpeedTest.ConsoleTime("反射取值", count, () => { var a = propertyInfo.GetValue(user, null); }); }
/// <summary> /// 检测实体类值状况 /// </summary> /// <param name="dicError">返回错误消息,key:属性名称;vakue:错误消息</param> /// <param name="entity">要检测的实体</param> public static bool Check <TEntity>(this TEntity entity, out Dictionary <string, List <string> > dicError) where TEntity : IVerification { dicError = new Dictionary <string, List <string> >(); var map = SetMapCacheManger.Cache(typeof(TEntity)); foreach (var kic in map.MapList) { var lstError = new List <string>(); var value = PropertyGetCacheManger.Cache(kic.Key, entity); // 是否必填 if (kic.Value.Required != null && !kic.Value.Required.IsValid(value)) { lstError.Add(kic.Value.Required.ErrorMessage); } //if (value == null) { continue; } // 字符串长度判断 if (kic.Value.StringLength != null && !kic.Value.StringLength.IsValid(value)) { lstError.Add(kic.Value.StringLength.ErrorMessage); } // 值的长度 if (kic.Value.Range != null && !kic.Value.Range.IsValid(value)) { lstError.Add(kic.Value.Range.ErrorMessage); } // 正则 if (kic.Value.RegularExpression != null && !kic.Value.RegularExpression.IsValid(value)) { lstError.Add(kic.Value.RegularExpression.ErrorMessage); } if (lstError.Count > 0) { dicError.Add(kic.Key.Name, lstError); } } return(dicError.Count == 0); }
/// <summary> /// 查找对象属性值 /// </summary> /// <typeparam name="TEntity">实体类</typeparam> /// <typeparam name="T">返回值类型</typeparam> /// <param name="entity">当前实体类</param> /// <param name="propertyName">属性名</param> /// <param name="defValue">默认值</param> public static T GetValue <TEntity, T>(TEntity entity, string propertyName, T defValue = default(T)) where TEntity : class { if (entity == null) { return(defValue); } foreach (var property in entity.GetType().GetProperties()) { if (property.Name != propertyName) { continue; } if (!property.CanRead) { return(defValue); } return(ConvertType(PropertyGetCacheManger.Cache(property, entity), defValue)); } return(defValue); }
/// <summary> /// Update将实体类的赋值,转成表达式树 /// </summary> /// <typeparam name="TEntity">实体类型</typeparam> /// <param name="entity">被赋值的实体</param> internal void AssignUpdate <TEntity>(TEntity entity) where TEntity : class, new() { if (entity == null) { return; } var oParameter = Expression.Parameter(entity.GetType(), "o"); var lstAssign = new List <Expression>(); // 迭代实体赋值情况 foreach (var kic in SetMap.PhysicsMap.MapList.Where(o => o.Value.Field.IsMap && !o.Value.Field.IsFun && o.Value.Field.InsertStatusType == StatusType.CanWrite)) { var obj = PropertyGetCacheManger.Cache(kic.Key, entity); if (obj == null) { continue; } var member = Expression.MakeMemberAccess(oParameter, kic.Key); // 主键、只读条件、主键状态下,转为条件状态 if ((kic.Value.Field.UpdateStatusType == StatusType.ReadCondition || kic.Value.Field.IsPrimaryKey)) { // 当前条件已存在该值时,跳过 if (new GetMemberVisitor().Visit(ExpWhere).Any(o => o.Member == kic.Key)) { continue; } var expCondiction = ExpressionHelper.CreateBinaryExpression <TEntity>(obj, member); ExpWhere = ExpressionHelper.MergeAndAlsoExpression((Expression <Func <TEntity, bool> >)ExpWhere, expCondiction); } else { var ass = Expression.Assign(member, Expression.Convert(Expression.Constant(obj), kic.Key.PropertyType)); lstAssign.Add(ass); } } ExpAssign = ExpressionHelper.MergeBlockExpression(lstAssign.ToArray()); }
/// <summary> /// 插入(支持延迟加载)会自动赋值标识字段 /// </summary> /// <param name="entity"></param> public int Insert(TEntity entity) { Check.NotNull(entity, "插入操作时,参数不能为空!"); // 更新本地缓存 var lst = Cache.ToList(); // 标识字段是否有值 var indexHaveValue = _set.SetMap.PhysicsMap.DbGeneratedFields.Key != null && PropertyGetCacheManger.Cache(_set.SetMap.PhysicsMap.DbGeneratedFields.Key, entity) != null; _set.Insert(entity, !indexHaveValue); lst.Add(entity); _dataCache.Update(lst); return(1); }