/// <summary> /// 根据查询表达式获取查询属性,转换为表字段 /// 忽略IsIgnore=true的属性 /// </summary> /// <param name="select">查询表达式</param> /// <param name="unSelect">排除查询表达式</param> /// <returns></returns> public static List <PiMap> GetPiMapList <T>(Expression select = null, Expression unSelect = null) { List <PiMap> result = new List <PiMap>(); List <string> inPiList = null; List <string> exPiList = null; DtoMapping dtoMapping = GetDtoMapping <T>(); if (select != null) { inPiList = ExpressionHelper.GetPiList((Expression <Func <T, object> >)select); for (int i = 0; i < inPiList.Count; i++) { if (!dtoMapping.PiMapDic.Keys.Contains(inPiList[i])) { continue; //throw new Exception("属性:" + inPiList[i] + "未包含在目标对象" + typeof(T).Name + "中."); } if (dtoMapping.PiMapDic[inPiList[i]].IsIgnore) { //如果是忽略字段 continue; } result.Add(dtoMapping.PiMapDic[inPiList[i]]); } } else if (unSelect != null) { exPiList = ExpressionHelper.GetPiList((Expression <Func <T, object> >)unSelect); foreach (KeyValuePair <string, PiMap> piMapItem in dtoMapping.PiMapDic) { if (exPiList.Contains(piMapItem.Key)) { //如果包含在排除列表,则跳过该属性 continue; } if (piMapItem.Value.IsIgnore) { //如果是忽略字段 continue; } result.Add(piMapItem.Value); } } else { //排除忽略字段 result = dtoMapping.PiMapDic.Where(piMap => !piMap.Value.IsIgnore).Select(piMap => piMap.Value).ToList(); } return(result); }
/// <summary> /// 获取Dto的DtoDbMapping,获取第一次后会放入一个缓存列表中 /// </summary> public static DtoMapping GetDtoMapping <T>() { Type t = typeof(T); if (!dtoMappingCache.ContainsKey(t)) { lock (lockForDtoMappingObj) { //获取写入锁后再次判断 if (!dtoMappingCache.ContainsKey(t)) { DtoMapping mapping = new DtoMapping(); SetDtoMappingDic <T>(mapping); mapping.EntityType = t; mapping.TableAttr = GetDtoTableAttr <T>(); dtoMappingCache.TryAdd(t, mapping); } } } return((DtoMapping)dtoMappingCache[t]); }
/// <summary> /// lambda表达式转换sql /// 支持的方法: /// Contains(支持List.Contains字段与字段Contains字符串), /// StartsWith,EndsWith,=,!=,>,>=,小于,小于等于 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="select"></param> /// <param name="query"></param> /// <param name="orderByClauseList"></param> /// <param name="pager"></param> /// <param name="unSelect"></param> /// <returns></returns> private void FillFilter <T>( Expression <Func <T, bool> > query = null, Expression <Func <T, object> > select = null, List <OrderByClause> orderByClauseList = null, Pager pager = null, Expression <Func <T, object> > unSelect = null) where T : class, new() { dtoDbMapping = DtoMappingHelper.GetDtoMapping <T>(); #region 处理查询条件 if (query != null) { Expression exp = query.Body as Expression; HandleFilterExpression(exp); } #endregion #region 处理排序 if (orderByClauseList != null && orderByClauseList.Count > 0) { for (int i = 0; i < orderByClauseList.Count; i++) { filter.AddOrderBy(orderByClauseList[i].FieldName, orderByClauseList[i].Order); } } #endregion #region 处理分页 if (pager != null) { filter.InitPage(pager.PageIndex, pager.PageSize); } #endregion #region 处理查询属性 filter.Select = select; filter.UnSelect = unSelect; #endregion }
/// <summary> /// 通过解析获得Dto的对象的参数,Key:为类的属性名 /// </summary> /// <returns>返回Dto参数</returns> public static void SetDtoMappingDic <T>(DtoMapping mapping) { Dictionary <string, FieldAttribute> fieldDic = new Dictionary <string, FieldAttribute>(); Dictionary <string, PropertyInfo> piDic = new Dictionary <string, PropertyInfo>(); Type type = typeof(T); PropertyInfo[] piList = type.GetProperties(); foreach (PropertyInfo pi in piList) { FieldAttribute attr = GetCustomAttribute <FieldAttribute>(pi); NoMappingAttribute noMappingAttr = GetCustomAttribute <NoMappingAttribute>(pi); if (attr != null) { //如果列名没有赋值,则将列名定义和属性名一样的值 if (string.IsNullOrEmpty(attr.Name)) { attr.Name = pi.Name; attr.PiName = pi.Name; } } else { //如果实体没定义Field信息,则自动添加 attr = new FieldAttribute() { Name = pi.Name, PiName = pi.Name, }; } if (!pi.CanRead || !pi.CanWrite) { //如果只读或只写,则该字段IsIgnore属性设置为True attr.IsIgnore = true; } if (noMappingAttr != null) { //如果发现NoMapping,则该字段IsIgnore属性设置为True attr.IsIgnore = true; } PiMap piMap = new PiMap(pi, attr); mapping.PiMapDic.Add(piMap.PiName, piMap); } }