ASP.NET MVC:Expression Trees 作为参数简化查询

 

引言

ASP.NET MVC 引入了 ModelBinder 技术,让我们可以在 Action 中以强类型参数的形式接收 Request 中的数据,极大的方便了我们的编程,提高了生产力。在查询 Action 中,我们可以将 Expression Trees 用作参数,通过自定义的 ModelBinder 动态自动构建查询表达式树,进一步发挥 MVC 的威力,简化编码工作。

先给出本文中使用的 Model:

1 2 3 4 5 6 7 8
public class Employee {     public int ID { get; set; }     public string FirstName { get; set; }     public string LastName { get; set; }     public bool Sex { get; set; }     public DateTime? Birthday { get; set; }     public string Remark { get; set; } }

MVC 查询和存在的不足

下面是一个查询 Employee 的 Action,在 MVC 项目中经常可以见到:

1 2 3 4 5 6 7 8 9 10 11 12
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 判断,以致代码行数比较多,不是特别清晰。可以借助本人《c# 扩展方法奇思妙用基础篇 六:WhereIf 扩展》一文中的扩展方法予以简化:

1 2 3 4 5 6 7 8
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 项目中也是这样处理的。

但时间一长,我逐步也发现了这种方式一些不足之处:

  1. 首先,网站中有很多类似的查询,如Customer、Order、Product 等等。而且大致也有点规律:字符串的一般模糊查询,时间日期类的一般按日期查询(忽略时间),其它类型则相等查询。不同 Model 查询的 Action 编码总有八、九分相似,但又不是简单的重复,却又难以重构
  2. 需求变动,如增加一个查询条件,修改 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 获取。

下面给出一个粗略实现,仅用来说明这个思路是可行的:

 1 public class QueryConditionExpressionModelBinder : IModelBinder {
 2     public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
 3         var modelType = GetModelTypeFromExpressionType(bindingContext.ModelType);
 4         if (modelType == nullreturn null;
 5 
 6         var body = default(Expression);
 7         var parameter = Expression.Parameter(modelType, modelType.Name);
 8 
 9         foreach (var property in modelType.GetProperties()){
10             var queryValue = GetValueAndHandleModelState(property, bindingContext.ValueProvider, controllerContext.Controller);
11             if (queryValue == nullcontinue;
12 
13             Expression proeprtyCondition = null;
14             if (property.PropertyType == typeof (string)){
15                 if (!string.IsNullOrEmpty(queryValue as string)){
16                     proeprtyCondition = parameter
17                         .Property(property.Name)
18                         .Call("Contains", Expression.Constant(queryValue));
19                 }
20             }
21             else if (property.PropertyType == typeof (DateTime?)){
22                 proeprtyCondition = parameter
23                     .Property(property.Name)
24                     .Property("Value")
25                     .Property("Date")
26                     .Equal(Expression.Constant(queryValue));
27             }
28             else{
29                 proeprtyCondition = parameter
30                     .Property(property.Name)
31                     .Equal(Expression.Constant(queryValue));
32             }
33             if (proeprtyCondition != null)
34                 body = body != null ? body.AndAlso(proeprtyCondition) : proeprtyCondition;
35         }
36         if (body == null) body = Expression.Constant(true);
37         return body.ToLambda(parameter);
38     }
39     /// <summary>
40     /// 获取 Expression<Func<TXXX, bool>> 中 TXXX 的类型
41     /// </summary>
42     private Type GetModelTypeFromExpressionType(Type lambdaExpressionType) {
43 
44         if (lambdaExpressionType.GetGenericTypeDefinition() != typeof (Expression<>)) return null;
45 
46         var funcType = lambdaExpressionType.GetGenericArguments()[0];
47         if (funcType.GetGenericTypeDefinition() != typeof (Func<,>)) return null;
48 
49         var funcTypeArgs = funcType.GetGenericArguments();
50         if (funcTypeArgs[1] != typeof (bool)) return null;
51         return funcTypeArgs[0];
52     }
53     /// <summary>
54     /// 获取属性的查询值并处理 Controller.ModelState 
55     /// </summary>
56     private object GetValueAndHandleModelState(PropertyInfo property, IValueProvider valueProvider, ControllerBase controller) {
57         var result = valueProvider.GetValue(property.Name);
58         if (result == nullreturn null;
59 
60         var modelState = new ModelState {Value = result};
61         controller.ViewData.ModelState.Add(property.Name, modelState);
62 
63         object value = null;
64         try{
65             value = result.ConvertTo(property.PropertyType);
66         }
67         catch (Exception ex){
68             modelState.Errors.Add(ex);
69         }
70         return value;
71     }
72 }

了解这段代码,需要 MVC 和 Expression Trees 的一些知识。这段代码还用到了 Expression 扩展方法,参见:《c# 扩展方法奇思妙用基础篇九:Expression 扩展》。

如果不想在 Global.asax 文件中设置 Expression<Func<T, bool>> 的 ModelBinder, 可以借助用下面这个 Attribute 类:

1 public class QueryConditionBinderAttribute : CustomModelBinderAttribute {     public override IModelBinder GetBinder() {         return new QueryConditionExpressionModelBinder();     } }
1
public ActionResult Index3([QueryConditionBinder]Expression<Func<Employee, bool>> predicate) { //... }
Index3 简单修改如下:

下面是一个调试截图,绑定正常。

image

再次说明:本部分代码仅用来说明思路可行,用了大量的硬编码。

我也正在准备编写一个更加灵活 QueryConditionExpressionModelBinder,来应对复杂的查询(如时间范围、值大于、小于等、以及限制对某些属性的查询),目前也有了一个大体的思路,初步完成后在之后的博文中和大家分享下。如果你有好的思路,不妨写在回复中。

 

源码下载:MvcQuery.rar (VS2010 MVC3 项目,1758KB)

在线演示:http://asp-net-mvc-expression-trees-as-action-parameter.ldp.me

原文地址:https://www.cnblogs.com/goody9807/p/2317928.html