Expression Trees 参数简化查询
ASP.NET MVC 引入了 ModelBinder 技术,让我们可以在 Action 中以强类型参数的形式接收 Request 中的数据,极大的方便了我们的编程,提高了生产力。在查询 Action 中,我们可以将 Expression Trees 用作参数,通过自定义的 ModelBinder 动态自动构建查询表达式树,进一步发挥 MVC 的威力,简化编码工作。
MVC 查询和存在的不足
下面是一个查询 Employee 的 Action,在 MVC 项目中经常可以见到:
public ActionResult Index(string firstName, string lastName, DateTime? birthday, bool? sex) {
var employees = repository.Query();
if (firstName.IsNotNullAndEmpty())
employees = employees.Where(e => e.FirstName.Contains(firstName));
if (firstName.IsNotNullAndEmpty())
employees = employees.Where(e => e.LastName.Contains(lastName));
if (birthday.HasValue)
employees = employees.Where(e => e.Birthday.Value.Date == birthday.Value.Date);
if (sex.HasValue)
employees = employees.Where(e => e.Sex == sex);
return View(employees);
}
得益于 MVC 的绑定技术,我们可以简单通过 Action 的参数来获取请求的值,很少再使用 Request["XXXX"] 的方式。
仔细观察,会发现上面这个 Action 中充斥着大量 if 判断,以致代码行数比较多,不是特别清晰。
public ActionResult Index2(string firstName, string lastName, DateTime? birthday, bool? sex) {
var employees = repository.Query()
.WhereIf(e => e.FirstName.Contains(firstName), firstName.IsNotNullAndEmpty())
.WhereIf(e => e.LastName.Contains(lastName), lastName.IsNotNullAndEmpty())
.WhereIf(e => e.Birthday.Value.Date == birthday.Value.Date, birthday.HasValue)
.WhereIf(e => e.Sex == sex, sex.HasValue);
return View("Index", employees);
}
代码相清晰了许多,我之前的几个 MVC 项目中也是这样处理的。
但时间一长,我逐步也发现了这种方式一些不足之处:
- 首先,网站中有很多类似的查询,如Customer、Order、Product 等等。而且大致也有点规律:字符串的一般模糊查询,时间日期类的一般按日期查询(忽略时间),其它类型则相等查询。不同 Model 查询的 Action 编码总有八、九分相似,但又不是简单的重复,却又难以重构。
- 需求变动,如增加一个查询条件,修改 View 是必须的,但也要修改 Action,增加一个参数,还要加一行 Where 或 WhereIf。简单变动却多处修改,烦人啊,而且这种需求变动又是比较频繁的,尤其是在项目初期。若能只修改 View 而不修改 Action 就爽了。
思考后,我决定使用 Expression Trees 作为查询 Action的参数来弥补这些不足。
使用 Expression<Func<T, bool>> 作为 Action 的参数
public ActionResult Index3(Expression<Func<Employee, bool>> predicate) {
var employees = repository.Query().Where(predicate);
return View("Index", employees);
}
将 Expression Trees 作为 Action 的唯一的参数(暂不考虑分页、排序等),将所有的查询条件都统一汇集至 predicate 参数。
所有的查询(不管是 Employee 还是 Customer)都使用如上代码。其它实体查询只需修改参数的类型,如 Customer 查询改为 Expression<Func<Customer, bool>> 。
如上修改代码后,直接运行会报错,因为 MVC 中默认的数据绑定器 DefaultModelBinder 不能正确绑定 Expression<Func<T, bool>> 类型的参数。
我们要新创一个新的 ModelBinder。
创建 QueryConditionExpressionModelBinder
需要一个新的 ModelBinder 来为 Expression<Func<T, bool>> 类型的参数赋值,且命名为 QueryConditionExpressionModelBinder。
QueryConditionExpressionModelBinder 要根据上下文来自动生成查询的 Expression Trees。主要关注的上下文有两点:首先是当前 Model 的类型,即 typeof(T);其次是 Request 提供的值,可通过 ValueProvider 获取。
下面给出一个粗略实现,仅用来说明这个思路是可行的:
public class QueryConditionExpressionModelBinder : IModelBinder {
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
var modelType = GetModelTypeFromExpressionType(bindingContext.ModelType);
if (modelType == null) return null;
var body = default(Expression);
var parameter = Expression.Parameter(modelType, modelType.Name);
foreach (var property in modelType.GetProperties()){
var queryValue = GetValueAndHandleModelState(property, bindingContext.ValueProvider, controllerContext.Controller);
if (queryValue == null) continue;
Expression proeprtyCondition = null;
if (property.PropertyType == typeof (string)){
if (!string.IsNullOrEmpty(queryValue as string)){
proeprtyCondition = parameter
.Property(property.Name)
.Call("Contains", Expression.Constant(queryValue));
}
}
else if (property.PropertyType == typeof (DateTime?)){
proeprtyCondition = parameter
.Property(property.Name)
.Property("Value")
.Property("Date")
.Equal(Expression.Constant(queryValue));
}
else{
proeprtyCondition = parameter
.Property(property.Name)
.Equal(Expression.Constant(queryValue));
}
if (proeprtyCondition != null)
body = body != null ? body.AndAlso(proeprtyCondition) : proeprtyCondition;
}
if (body == null) body = Expression.Constant(true);
return body.ToLambda(parameter);
}
/// <summary>
/// 获取 Expression<Func<TXXX, bool>> 中 TXXX 的类型
/// </summary>
private Type GetModelTypeFromExpressionType(Type lambdaExpressionType) {
if (lambdaExpressionType.GetGenericTypeDefinition() != typeof (Expression<>)) return null;
var funcType = lambdaExpressionType.GetGenericArguments()[];
if (funcType.GetGenericTypeDefinition() != typeof (Func<,>)) return null;
var funcTypeArgs = funcType.GetGenericArguments();
if (funcTypeArgs[] != typeof (bool)) return null;
return funcTypeArgs[];
}
/// <summary>
/// 获取属性的查询值并处理 Controller.ModelState
/// </summary>
private object GetValueAndHandleModelState(PropertyInfo property, IValueProvider valueProvider, ControllerBase controller) {
var result = valueProvider.GetValue(property.Name);
if (result == null) return null;
var modelState = new ModelState {Value = result};
controller.ViewData.ModelState.Add(property.Name, modelState);
object value = null;
try{
value = result.ConvertTo(property.PropertyType);
}
catch (Exception ex){
modelState.Errors.Add(ex);
}
return value;
}
}
如果不想在 Global.asax 文件中设置 Expression<Func<T, bool>> 的 ModelBinder, 可以借助用下面这个 Attribute 类:
public class QueryConditionBinderAttribute : CustomModelBinderAttribute {
public override IModelBinder GetBinder() {
return new QueryConditionExpressionModelBinder();
}
}
Index3 简单修改如下:
public ActionResult Index3([QueryConditionBinder]Expression<Func<Employee, bool>> predicate) { //... }
Expression Trees 参数简化查询的更多相关文章
- ASP.NET MVC:Expression Trees 作为参数简化查询
ASP.NET MVC 引入了 ModelBinder 技术,让我们可以在 Action 中以强类型参数的形式接收 Request 中的数据,极大的方便了我们的编程,提高了生产力.在查询 Action ...
- [C#] C# 知识回顾 - 表达式树 Expression Trees
C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...
- Expression Trees
Expression Trees 只是想简单说下表达式树 - Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译 ...
- C# 知识回顾 - 表达式树 Expression Trees
C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...
- 表达式树(Expression Trees)
[翻译]表达式树(Expression Trees) 原文地址:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/con ...
- Expression表达式树动态查询
在进行数据列表的查询中,我们通常会使用两种方式进行查询: linq查询 数据库sql语句查询 这样固然可以实现查询,本人之前也都是这么做的,因为查询的条件很少.使用linq,可以将所有的查询条件的属性 ...
- ADO.NET笔记——带参数的查询防止SQL注入攻击
相关知识: 把单引号替换成两个单引号,虽然能起到一定的防止SQL注入攻击的作用,但是更为有效的办法是把要拼接的内容做成“参数” SQLCommand支持带参数的查询,也就是说,可以在查询语句中指定参数 ...
- 带参数的查询防止SQL注入攻击
把单引号替换成两个单引号,虽然能起到一定的防止SQL注入攻击的作用,但是更为有效的办法是把要拼接的内容做成“参数” SQLCommand支持带参数的查询,也就是说,可以在查询语句中指定参数: 参数的设 ...
- odoo 11 实现多个字段对应一个查询参数的查询
在整理英语单词开发模块的过程中,有这样一个需求,就是我在查询界面里输入一个查询的值A,这个A可能是下面的任何一个值 1.一个英语单词 2.汉语文字 3.一个英语单词的部分 这里有两张表:engli ...
随机推荐
- 使用mongo-express管理mongodb数据库
前面的话 本文将详细介绍一款用nodejs开发的基于Web的mongodb数据库管理工具mongo-express 安装 首先,全局安装 mongo-express 包 npm install -g ...
- JarvisOJ Misc webshell分析
分析压缩包中的数据包文件并获取flag.flag为32位大写md5. 神仙们还是强啊,webshell主要看http流,再过滤只剩下post请求 可以使用 http.request.method == ...
- 开篇python
测试代码 #!/usr/bin/env python # -*- coding: UTF-8 -*- import os import sys print(os.getcwd) print(sys.v ...
- POJChallengeRound2 Guideposts 【单位根反演】【快速幂】
题目分析: 这题的目标是求$$ \sum_{i \in [0,n),k \mid i} \binom{n}{i}G^i $$ 这个形式很像单位根反演. 单位根反演一般用于求:$ \sum_{i \in ...
- magento 2 Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors.%
Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on h ...
- 【BZOJ5503】[GXOI/GZOI2019]宝牌一大堆(动态规划)
[BZOJ5503][GXOI/GZOI2019]宝牌一大堆(动态规划) 题面 BZOJ 洛谷 题解 首先特殊牌型直接特判. 然后剩下的部分可以直接\(dp\),直接把所有可以存的全部带进去大力\(d ...
- [M$]微软提供的ProcessExplorer等系统工具集合
https://docs.microsoft.com/en-us/sysinternals/downloads/index
- 动态SQL之、条件判断(转)
错误方式一: 在mybatis的动态sql语句中使用<if>标签可以判断sql中的条件是否成立. <select id="getPerson" resultTyp ...
- 剑指Offer_编程题_21
题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数. class Solution { public: void push(int value) { st.push(val ...
- Entity Framework入门教程(19)---EF中使用事务
EF中使用事务 这节介绍EF6中事务的使用.EF core中事务的使用方式和EF6中一模一样. 1.EF中的默认的事务 默认情况下,当我们执行一个SaveChanges()方法时就会新建了一个事务,然 ...