zoukankan      html  css  js  c++  java
  • 将不确定变为确定~表达式树是否可以有个集合,条件过滤有了新方向

    回到目录

    对于我之前项目中的统一条件过滤采用了dictinary来实现的,优点就是方法签名统一了,缺点不用说,就是字典的键容易写错,感觉一进入.net3.5之后,一切都要和Expression联系在一起,我们在创建一个Expression(表达式树)时,可以使用lambda表达式去创建,很容易:

    1 Expression<Func<string, bool>> predicate= name=>name=="zzl";

    可以看到,它其它由一个委托组成,输入参数是个字符,输出是个布尔值,在LINQ中这种技术被广泛的使用在扩展方法中,如Where扩展方法:

     1  public static IQueryable<TSource> Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
     2         {
     3             if (source == null)
     4             {
     5                 throw System.Linq.Error.ArgumentNull("source");
     6             }
     7             if (predicate == null)
     8             {
     9                 throw System.Linq.Error.ArgumentNull("predicate");
    10             }
    11             return source.Provider.CreateQuery<TSource>(Expression.Call(null, ((MethodInfo) MethodBase.GetCurrentMethod()).MakeGenericMethod(new Type[] { typeof(TSource) }), new Expression[] { source.Expression, Expression.Quote(predicate) }));
    12         }

    无可厚非,表达式树的出现,lambda表达式的支持,是.net开发人员的福音,也是.net3.5中更大的亮点了。

    说正文了,以前我的写统一的参数签名时,使用的是dictionary,代码中可以是这样的

    1         Common.VPredication vp = new Common.VPredication();
    2             Common.PagingParam pp = new Common.PagingParam(page ?? 1, VConfig.WebConstConfig.PageSize);
    3 
    4             vp.AddItem("userId", userID);
    5             if (_gid != 0)
    6                 vp.AddItem("gid", _gid);

    统一的方法签名:

    1  Common.Page.PagedList<Entity.Res_Item> GetList(Common.VPredication vp, Common.PagingParam pp)

    而有了表达式树的集合后,完成可以把过滤条件写在它里面,这样构建条件变成了这样:

    1        PredicateList<UserBases> zzl = new PredicateList<UserBases>();
    2             zzl.Add(i => i.Name.Contains("zzl"));
    3             zzl.Add(i => i.UserID == 1);
    4             GetModel(zzl).ForEach(i => Console.WriteLine(i.UserID + i.Name));

    而方法签名仍然是很统一,只是变成了表达式树的样子,有点强类型的味道,呵呵:

    1 List<UserBases> GetModel(PredicateList<UserBases> param)

    而PredicateList类型的原代码,我也公开一下吧,呵呵,大家一共分享:

     1    /// <summary>
     2     /// 功能:条件过滤类
     3     /// 作者:张占岭
     4     /// 日期:2012-6-7
     5     /// </summary>
     6     public class PredicateList<TEntity> : IEnumerable<Expression<Func<TEntity, bool>>> where TEntity : class
     7     {
     8         List<Expression<Func<TEntity, bool>>> expressionList;
     9         public PredicateList()
    10         {
    11             expressionList = new List<Expression<Func<TEntity, bool>>>();
    12         }
    13         /// <summary>
    14         /// 添加到集合
    15         /// </summary>
    16         /// <param name="predicate"></param>
    17         public void Add(Expression<Func<TEntity, bool>> predicate)
    18         {
    19             expressionList.Add(predicate);
    20         }
    21         /// <summary>
    22         /// 从集合中移除
    23         /// </summary>
    24         /// <param name="predicate"></param>
    25         public void Remove(Expression<Func<TEntity, bool>> predicate)
    26         {
    27             expressionList.Remove(predicate);
    28         }
    29         #region IEnumerable 成员
    30 
    31         public IEnumerator GetEnumerator()
    32         {
    33             return expressionList.GetEnumerator();
    34         }
    35 
    36         #endregion
    37 
    38         #region IEnumerable<Expression<Func<TEntity>>> 成员
    39 
    40         IEnumerator<Expression<Func<TEntity, bool>>> IEnumerable<Expression<Func<TEntity, bool>>>.GetEnumerator()
    41         {
    42             return expressionList.GetEnumerator();
    43         }
    44 
    45         #endregion
    46     }

     回到目录

  • 相关阅读:
    Java中String类两种实例化的区别(转)
    Linux内核如何装载和启动一个可执行程序(转)
    CentOS 7 下安装jdk1.8(转)
    MySQL中死锁(转)
    CTF取证方法大汇总,建议收藏!
    实战经验|大神战队都在i春秋教你打CTF
    CTF丨从零开始搭建WEB Docker靶场
    业务逻辑漏洞探索之敏感信息泄露
    实战经验丨业务逻辑漏洞探索之活动类漏洞
    漏洞经验分享丨Java审计之XXE(下)
  • 原文地址:https://www.cnblogs.com/lori/p/2540200.html
Copyright © 2011-2022 走看看