对If...Where的封装——语法糖WhereIf

  语法糖(Syntactic sugar),也译为糖衣语法,是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方便程序员使用。通常来说使用语法糖能够增加程序的可读性,从而减少程序代码出错的机会。——摘自百度百科:语法糖
  
  对If...Where的封装——语法糖WhereIf(如果读者已经知晓,请自行跳过)
  
  在做条件查询的时候,我们可能经常要写这样的代码:
  
  复制代码
  
  List<User> Query(User queryModel)
  
  {
  
  //定义一个演示数据集
  
  List<User> userList = new List<User>
  
  {
  
  new User{ UserName = "燕双鹰",  Phone = "10369852103",  Role = "正派" ,  Sex = true},
  
  new User{ UserName = "沈七七",  Phone = "14785203630",  Role = "反派",   Sex = true},
  
  new User{ UserName = "步鹰",    Phone = "14702021596",  Role = "反派",   Sex = true},
  
  new User{ UserName = "小玲",    Phone = "19469874106",  Role = "正派",   Sex = false},
  
  new User{ UserName = "赵一平",  Phone = "18502369740",  Role = "反派",   Sex = true}
  
  };
  
  var data = userList.AsQueryable();//转为IQueryable类型
  
  //条件过滤
  
  if (!string.IsNullOrEmpty(queryModel.UserName))
  
  {
  
  data = data.Where(u => u.UserName == queryModel.UserName);
  
  }
  
  if (!string.IsNullOrEmpty(queryModel.Phone))
  
  {
  
  data = data.Where(u => u.Phone == queryModel.Phone);
  
  }
  
  if (!string.IsNullOrEmpty(queryModel.Role))
  
  {
  
  data = data.Where(u => u.Role == queryModel.Role);
  
  }
  
  if (queryModel.Sex != null)
  
  {
  
  data = data.Where(u => u.Sex == queryModel.Sex);
  
  }
  
  return data.ToList();
  
  }
  
  复制代码
  
  当传入的参数不为空时,才执行查询。很明显,这里大量的If-Where语句是极为简单,且不断重复出现的代码(逻辑),可以进行封装以简化操作,以简化代码。
  
  创建泛型扩展方法WhereIf,代码如下:
  
  复制代码
  
  public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool condition, Expression<Func<T, bool>> predicate)
  
  {
  
  return condition
  
  ? query.Where(predicate)
  
  : query;
  
  <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/ www.hnxinhe.cn.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>allure-demo</groupId>
    <artifactId>allure-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <allure.version>2.10.0</allure.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <aspectj.version>www.365soke.com 1.9.2</aspectj.baihuiyulegw.com version>
        <suiteXmlFile>src/test/resources/suite/test-moudle/testng.xml</suiteXmlFile>
    </properties>

    <dependencies>

        <dependency>
            <groupId>selenium-server-standalone</groupId>
            <artifactId>selenium-server-standalone</artifactId>
            <version>3.9.1<www.chenhaiyulp.cn/ /version>
        </dependency>

        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-testng</artifactId>
            <version>${allure.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.testng<www.chenhaiyulp.cn/ /groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
        </dependency>

        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-java-commons</artifactId>
            <version>2.10.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin<www.yunyouuyL.com  /artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8<www.yunyougj.cn  /source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <argLine>
  
  复制代码
  
  该方法实现了对If-Where的封装,使用方法如下:
  
  复制代码
  
  List<User> Query(User queryModel)
  
  {
  
  //定义一个演示数据集
  
  List<User> userList = new List<User>
  
  {
  
  new User{ UserName = "燕双鹰",  Phone = "10369852103",  Role = "正派" ,  Sex = true},
  
  new User{ UserName = "沈七七",  Phone = "14785203630",  Role = "反派",   Sex = true},
  
  new User{ UserName = "步鹰",    Phone = "14702021596",  Role = "反派",   Sex = true},
  
  new User{ UserName = "小玲",    Phone = "19469874106",  Role = "正派",   Sex = false},
  
  new User{ UserName = "赵一平",  Phone = "18502369740",  Role = "反派",   Sex = true}
  
  };
  
  var data = userList.AsQueryable()
  
  .WhereIf(!string.IsNullOrEmpty(queryModel.UserName), u => u.UserName == queryModel.UserName)
  
  .WhereIf(!string.IsNullOrEmpty(queryModel.Phone), u => u.Phone == queryModel.Phone)
  
  .WhereIf(!string.IsNullOrEmpty(queryModel.Role), u => u.Role == queryModel.Role)
  
  .WhereIf(queryModel.Sex != null, u => u.Sex == queryModel.Sex);
  
  return data.ToList();
  
  }
  
  复制代码
  
  之前超过8行代码的查询代码,被精简到4行,代码行数减少超过一半,可读性大幅提高,由于只是简单的封装,运行效率几乎不变。(减少大量代码,提高可读性,功能不变,效率不变,有优无缺,因此强烈建议WhereIf来代替传统的If-Where操作。)
  
  新的问题来了,If语句还存在一个条件不满足的情况:else,WhereIf方法只封装了IfWhere,却没有封装If-Whrere-else-Where语句,如果遇到如下的查询要求,要怎么做呢?
  
  复制代码
  
  if (!string.IsNullOrEmpty(queryModel.UserName))
  
  {
  
  data = data.Where(u => u.UserName == queryModel.UserName);
  
  }
  
  else
  
  {
  
  data = data.Where(u => u.UserName == "燕双鹰");//如果查询条件为空,就查询燕双鹰的姓名
  
  }
  
  复制代码
  
  有三个办法可以解决这个问题:
  
  第一个办法,是修改WhereIf方法,增加else-Where的逻辑,使其支持If-Whrere-else-Where的逻辑:
  
  public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool condition, Expression<Func<T, bool>> truePredicate, Expression<Func<T, bool>> falsePredicate = null)
  
  => condition ? query.Where(truePredicate) : falsePredicate == null ? query : query.Where(falsePredicate);
  
  这样的做的缺点也是明显的:在参数condition为false时,会进行第二次逻辑判断,缺点是减低效率,优点是代码简洁。(当然,多一个逻辑判断也减低不了多少效率)
  
  第二个方法,避免第二次逻辑判断的方式是进行方法重载,也就是写两个WhereIf方法,在新增的这个WhereIf方法中,参数falsePredicate不再设置为可空参数:
  
  public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool condition, Expression<Func<T, bool>> truePredicate, Expression<Func<T, bool>> falsePredicate)
  
  => condition ? query.Where(truePredicate) :  query.Where(falsePredicate);
  
  优点是可以在不影响效率的情况下支持If-Whrere-else-Where逻辑,因为两个WhereIf方法的逻辑是差不多的,缺点是又写了简单重复的代码,不简洁。(当然,仅仅是定义它的时候不简洁,调用时候简洁程度和方法一,是一样的)
  
  第三个方法,完全不修改WhereIf方法, 仅仅在调用的时候,通过对参数condition进行取反操作,来达到目的:
  
  var data2 = data.WhereIf(!string.IsNullOrEmpty(queryModel.UserName), u => u.UserName == queryModel.UserName)
  
  .WhereIf(string.IsNullOrEmpty(queryModel.UserName), u => u.UserName == "燕双鹰");
  
  优点:方法定义最简单,缺点:在遇到If-Whrere-else-Where逻辑时,会增加代码量。
  
  具体选择哪一种,请读者自行斟酌,如果有更好的实现方法,就留言讨论分享出来吧^_^
  
  对for循环的封装,语法糖For
  
  实际开发中,很多时候对for循环的使用,仅仅是将一个操作,循环指定的次数,而且其中没有break、continue这些提前终止循环的逻辑。这种简单重复的逻辑可以进行提取封装。
  
  复制代码
  
  public static void For(int count, Action<int> action)
  
  {
  
  for (int i = 0; i < count; i++)
  
  {
  
  action.Invoke(i);
  
  }
  
  }
  
  复制代码
  
  这里使用了C#的内置泛型委托Action,发挥的作用就是将方法作为参数去传递。参数count表示循环总次数,Action的参数int,表示正在进行的循环次数,从0开始,读者可以根据需要改成从1开始(这里从1开始好,还是从0开始好,待定)。
  
  调用:
  
  SyntacticSugar.For(1, p => { int a = p + 8; data2.Remove(data2[a]); });
  
  如果认为这样调用麻烦,可以在参数count前加this,使之变为扩展方法,以简化调

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值