只要十步,你就可以应用表达式树来优化动态调用(一)

只要十步,你就可以应用表达式树来优化动态调用(一)动态调用是在 .net 开发是时常遇到的一种需求,即在只知道方法名或者属性名等情况下动态的调用方法或者属性。

大家好,欢迎来到IT知识分享网。

表达式树是 .net 中一系列非常好用的类型。在一些场景中使用表达式树可以获得更好的性能和更佳的扩展性。本篇我们将通过构建一个 “模型验证器” 来理解和应用表达式树在构建动态调用方面的优势。

Newbe.Claptrap 是一个用于轻松应对并发问题的分布式开发框架。如果您是首次阅读本系列文章。建议可以先从本文末尾的入门文章开始了解。

开篇摘要

前不久,我们发布了《如何使用 dotTrace 来诊断 netcore 应用的性能问题》,经过网友投票之后,网友们表示对其中表达式树的内容很感兴趣,因此本篇我们将展开讲讲。

动态调用是在 .net 开发是时常遇到的一种需求,即在只知道方法名或者属性名等情况下动态的调用方法或者属性。最广为人知的一种实现方式就是使用 “反射” 来实现这样的需求。当然也有一些高性能场景会使用 Emit 来完成这个需求。

本文将介绍 “使用表达式树” 来实现这种场景,因为这个方法相较于 “反射” 将拥有更好的性能和扩展性,相较于 Emit 又更容易掌握。

我们将使用一个具体的场景来逐步使用表达式来实现动态调用。

在该场景中,我们将构建一个模型验证器,这非常类似于 aspnet mvc 中 ModelState 的需求场景。

不是一篇简单的入门文章,初次涉足该内容的读者,建议在空闲时,在手边有 IDE 可以顺便操作时边看边做。同时,也不必在意样例中出现的细节方法,只需要了解其中的大意,能够依样画瓢即可,掌握大意之后再深入了解也不迟。

为了缩短篇幅,文章中的样例代码会将没有修改的部分隐去,想要获取完整的测试代码,请打开文章末尾的代码仓库进行拉取。

为什么要用表达式树,为什么可以用表达式树?

首先需要确认的事情有两个:

  1. 使用表达式树取代反射是否有更好的性能?
  2. 使用表达式树进行动态调用是否有很大的性能损失?

有问题,做实验。我们采用两个单元测试来验证以上两个问题。

调用一个对象的方法:

using System; using System.Diagnostics; using System.Linq.Expressions; using System.Reflection; using FluentAssertions; using NUnit.Framework; namespace Newbe.ExpressionsTests { public class X01CallMethodTest { private const int Count = 1_000_000; private const int Diff = 100; [SetUp] public void Init() { _methodInfo = typeof(Claptrap).GetMethod(nameof(Claptrap.LevelUp)); Debug.Assert(_methodInfo != null, nameof(_methodInfo) + " != null"); var instance = Expression.Parameter(typeof(Claptrap), "c"); var levelP = Expression.Parameter(typeof(int), "l"); var callExpression = Expression.Call(instance, _methodInfo, levelP); var lambdaExpression = Expression.Lambda<Action<Claptrap, int>>(callExpression, instance, levelP); // lambdaExpression should be as (Claptrap c,int l) => { c.LevelUp(l); } _func = lambdaExpression.Compile(); } [Test] public void RunReflection() { var claptrap = new Claptrap(); for (int i = 0; i < Count; i++) { _methodInfo.Invoke(claptrap, new[] {(object) Diff}); } claptrap.Level.Should().Be(Count * Diff); } [Test] public void RunExpression() { var claptrap = new Claptrap(); for (int i = 0; i < Count; i++) { _func.Invoke(claptrap, Diff); } claptrap.Level.Should().Be(Count * Diff); } [Test] public void Directly() { var claptrap = new Claptrap(); for (int i = 0; i < Count; i++) { claptrap.LevelUp(Diff); } claptrap.Level.Should().Be(Count * Diff); } private MethodInfo _methodInfo; private Action<Claptrap, int> _func; public class Claptrap { public int Level { get; set; } public void LevelUp(int diff) { Level += diff; } } } }

以上测试中,我们对第三种调用方式一百万次调用,并记录每个测试所花费的时间。可以得到类似以下的结果:

MethodTimeRunReflection 217ms

RunExpression 20ms

Directly 19ms

可以得出以下结论:

  1. 使用表达式树创建委托进行动态调用可以得到和直接调用近乎相同的性能。
  2. 使用表达式树创建委托进行动态调用所消耗的时间约为十分之一。

所以如果仅仅从性能上考虑,应该使用表达式树,也可以是用表达式树。

不过这是在一百万调用下体现出现的时间,对于单次调用而言其实就是纳秒级别的区别,其实无足轻重。

但其实表达式树不仅仅在性能上相较于反射更优,其更强大的扩展性其实采用最为重要的特性。

此处还有一个对属性进行操作的测试,此处将测试代码和结果罗列如下:

using System; using System.Diagnostics; using System.Linq.Expressions; using System.Reflection; using FluentAssertions; using NUnit.Framework; namespace Newbe.ExpressionsTests { public class X02PropertyTest { private const int Count = 1_000_000; private const int Diff = 100; [SetUp] public void Init() { _propertyInfo = typeof(Claptrap).GetProperty(nameof(Claptrap.Level)); Debug.Assert(_propertyInfo != null, nameof(_propertyInfo) + " != null"); var instance = Expression.Parameter(typeof(Claptrap), "c"); var levelProperty = Expression.Property(instance, _propertyInfo); var levelP = Expression.Parameter(typeof(int), "l"); var addAssignExpression = Expression.AddAssign(levelProperty, levelP); var lambdaExpression = Expression.Lambda<Action<Claptrap, int>>(addAssignExpression, instance, levelP); // lambdaExpression should be as (Claptrap c,int l) => { c.Level += l; } _func = lambdaExpression.Compile(); } [Test] public void RunReflection() { var claptrap = new Claptrap(); for (int i = 0; i < Count; i++) { var value = (int) _propertyInfo.GetValue(claptrap); _propertyInfo.SetValue(claptrap, value + Diff); } claptrap.Level.Should().Be(Count * Diff); } [Test] public void RunExpression() { var claptrap = new Claptrap(); for (int i = 0; i < Count; i++) { _func.Invoke(claptrap, Diff); } claptrap.Level.Should().Be(Count * Diff); } [Test] public void Directly() { var claptrap = new Claptrap(); for (int i = 0; i < Count; i++) { claptrap.Level += Diff; } claptrap.Level.Should().Be(Count * Diff); } private PropertyInfo _propertyInfo; private Action<Claptrap, int> _func; public class Claptrap { public int Level { get; set; } } } }

耗时情况:

MethodTimeRunReflection 373ms

RunExpression 19ms

Directly 18ms

由于反射多了一份装拆箱的消耗,所以比起前一个测试样例显得更慢了,使用委托是没有这种消耗的。

第〇步,需求演示

先通过一个测试来了解我们要创建的 “模型验证器” 究竟是一个什么样的需求。

using System.ComponentModel.DataAnnotations; using FluentAssertions; using NUnit.Framework; namespace Newbe.ExpressionsTests { /// <summary> /// Validate data by static method /// </summary> public class X03PropertyValidationTest00 { private const int Count = 10_000; [Test] public void Run() { for (int i = 0; i < Count; i++) { // test 1 { var input = new CreateClaptrapInput(); var (isOk, errorMessage) = Validate(input); isOk.Should().BeFalse(); errorMessage.Should().Be("missing Name"); } // test 2 { var input = new CreateClaptrapInput { Name = "1" }; var (isOk, errorMessage) = Validate(input); isOk.Should().BeFalse(); errorMessage.Should().Be("Length of Name should be great than 3"); } // test 3 { var input = new CreateClaptrapInput { Name = "yueluo is the only one dalao" }; var (isOk, errorMessage) = Validate(input); isOk.Should().BeTrue(); errorMessage.Should().BeNullOrEmpty(); } } } public static ValidateResult Validate(CreateClaptrapInput input) { return ValidateCore(input, 3); } public static ValidateResult ValidateCore(CreateClaptrapInput input, int minLength) { if (string.IsNullOrEmpty(input.Name)) { return ValidateResult.Error("missing Name"); } if (input.Name.Length < minLength) { return ValidateResult.Error(#34;Length of Name should be great than {minLength}"); } return ValidateResult.Ok(); } public class CreateClaptrapInput { [Required] [MinLength(3)] public string Name { get; set; } } public struct ValidateResult { public bool IsOk { get; set; } public string ErrorMessage { get; set; } public void Deconstruct(out bool isOk, out string errorMessage) { isOk = IsOk; errorMessage = ErrorMessage; } public static ValidateResult Ok() { return new ValidateResult { IsOk = true }; } public static ValidateResult Error(string errorMessage) { return new ValidateResult { IsOk = false, ErrorMessage = errorMessage }; } } } }

从上而下,以上代码的要点:

  1. 主测试方法中,包含有三个基本的测试用例,并且每个都将执行一万次。后续所有的步骤都将会使用这样的测试用例。
  2. Validate 方法是被测试的包装方法,后续将会调用该方法的实现以验证效果。
  3. ValidateCore 是 “模型验证器” 的一个演示实现。从代码中可以看出该方法对 CreateClaptrapInput 对象进行的验证,并且得到验证结果。但是该方法的缺点也非常明显,这是一种典型的 “写死”。后续我们将通过一系列改造。使得我们的 “模型验证器” 更加的通用,并且,很重要的,保持和这个 “写死” 的方法一样的高效!
  4. ValidateResult 是验证器输出的结果。后续将不断重复的用到该结果。

第一步,调用静态方法

首先我们构建第一个表达式树,该表达式树将直接使用上一节中的静态方法 ValidateCore。

using System; using System.ComponentModel.DataAnnotations; using System.Diagnostics; using System.Linq.Expressions; using FluentAssertions; using NUnit.Framework; namespace Newbe.ExpressionsTests { /// <summary> /// Validate date by func created with Expression /// </summary> public class X03PropertyValidationTest01 { private const int Count = 10_000; private static Func<CreateClaptrapInput, int, ValidateResult> _func; [SetUp] public void Init() { try { var method = typeof(X03PropertyValidationTest01).GetMethod(nameof(ValidateCore)); Debug.Assert(method != null, nameof(method) + " != null"); var pExp = Expression.Parameter(typeof(CreateClaptrapInput)); var minLengthPExp = Expression.Parameter(typeof(int)); var body = Expression.Call(method, pExp, minLengthPExp); var expression = Expression.Lambda<Func<CreateClaptrapInput, int, ValidateResult>>(body, pExp, minLengthPExp); _func = expression.Compile(); } catch (Exception e) { Console.WriteLine(e); throw; } } [Test] public void Run() { // see code in demo repo } public static ValidateResult Validate(CreateClaptrapInput input) { return _func.Invoke(input, 3); } public static ValidateResult ValidateCore(CreateClaptrapInput input, int minLength) { if (string.IsNullOrEmpty(input.Name)) { return ValidateResult.Error("missing Name"); } if (input.Name.Length < minLength) { return ValidateResult.Error(#34;Length of Name should be great than {minLength}"); } return ValidateResult.Ok(); } } }

从上而下,以上代码的要点:

  1. 增加了一个单元测试的初始化方法,在单元测试启动时创建的一个表达式树将其编译为委托保存在静态字段 _func 中。
  2. 省略了主测试方法 Run 中的代码,以便读者阅读时减少篇幅。实际代码没有变化,后续将不再重复说明。可以在代码演示仓库中查看。
  3. 修改了 Validate 方法的实现,不再直接调用 ValidateCore ,而调用 _func 来进行验证。
  4. 运行该测试,开发者可以发现,其消耗的时间和上一步直接调用的耗时,几乎一样,没有额外消耗。
  5. 这里提供了一种最为简单的使用表达式进行动态调用的思路,如果可以写出一个静态方法(例如:ValidateCore)来表示动态调用的过程。那么我们只要使用类似于 Init 中的构建过程来构建表达式和委托即可。
  6. 开发者可以试着为 ValidateCore 增加第三个参数 name 以便拼接在错误信息中,从而了解如果构建这种简单的表达式。

第二步,组合表达式

虽然前一步,我们将直接调用转变了动态调用,但由于 ValidateCore 还是写死的,因此还需要进一步修改。

本步骤,我们将会把 ValidateCore 中写死的三个 return 路径拆分为不同的方法,然后再采用表达式拼接在一起。

如果我们实现了,那么我们就有条件将更多的方法拼接在一起,实现一定程度的扩展。

注意:演示代码将瞬间边长,不必感受太大压力,可以辅助后面的代码要点说明进行查看。

using System; using System.ComponentModel.DataAnnotations; using System.Diagnostics; using System.Linq.Expressions; using FluentAssertions; using NUnit.Framework; // ReSharper disable InvalidXmlDocComment namespace Newbe.ExpressionsTests { /// <summary> /// Block Expression /// </summary> public class X03PropertyValidationTest02 { private const int Count = 10_000; private static Func<CreateClaptrapInput, int, ValidateResult> _func; [SetUp] public void Init() { try { var finalExpression = CreateCore(); _func = finalExpression.Compile(); Expression<Func<CreateClaptrapInput, int, ValidateResult>> CreateCore() { // exp for input var inputExp = Expression.Parameter(typeof(CreateClaptrapInput), "input"); var minLengthPExp = Expression.Parameter(typeof(int), "minLength"); // exp for output var resultExp = Expression.Variable(typeof(ValidateResult), "result"); // exp for return statement var returnLabel = Expression.Label(typeof(ValidateResult)); // build whole block var body = Expression.Block( new[] {resultExp}, CreateDefaultResult(), CreateValidateNameRequiredExpression(), CreateValidateNameMinLengthExpression(), Expression.Label(returnLabel, resultExp)); // build lambda from body var final = Expression.Lambda<Func<CreateClaptrapInput, int, ValidateResult>>( body, inputExp, minLengthPExp); return final; Expression CreateDefaultResult() { var okMethod = typeof(ValidateResult).GetMethod(nameof(ValidateResult.Ok)); Debug.Assert(okMethod != null, nameof(okMethod) + " != null"); var methodCallExpression = Expression.Call(okMethod); var re = Expression.Assign(resultExp, methodCallExpression); /** * final as: * result = ValidateResult.Ok() */ return re; } Expression CreateValidateNameRequiredExpression() { var requireMethod = typeof(X03PropertyValidationTest02).GetMethod(nameof(ValidateNameRequired)); var isOkProperty = typeof(ValidateResult).GetProperty(nameof(ValidateResult.IsOk)); Debug.Assert(requireMethod != null, nameof(requireMethod) + " != null"); Debug.Assert(isOkProperty != null, nameof(isOkProperty) + " != null"); var requiredMethodExp = Expression.Call(requireMethod, inputExp); var assignExp = Expression.Assign(resultExp, requiredMethodExp); var resultIsOkPropertyExp = Expression.Property(resultExp, isOkProperty); var conditionExp = Expression.IsFalse(resultIsOkPropertyExp); var ifThenExp = Expression.IfThen(conditionExp, Expression.Return(returnLabel, resultExp)); var re = Expression.Block( new[] {resultExp}, assignExp, ifThenExp); /** * final as: * result = ValidateNameRequired(input); * if (!result.IsOk) * { * return result; * } */ return re; } Expression CreateValidateNameMinLengthExpression() { var minLengthMethod = typeof(X03PropertyValidationTest02).GetMethod(nameof(ValidateNameMinLength)); var isOkProperty = typeof(ValidateResult).GetProperty(nameof(ValidateResult.IsOk)); Debug.Assert(minLengthMethod != null, nameof(minLengthMethod) + " != null"); Debug.Assert(isOkProperty != null, nameof(isOkProperty) + " != null"); var requiredMethodExp = Expression.Call(minLengthMethod, inputExp, minLengthPExp); var assignExp = Expression.Assign(resultExp, requiredMethodExp); var resultIsOkPropertyExp = Expression.Property(resultExp, isOkProperty); var conditionExp = Expression.IsFalse(resultIsOkPropertyExp); var ifThenExp = Expression.IfThen(conditionExp, Expression.Return(returnLabel, resultExp)); var re = Expression.Block( new[] {resultExp}, assignExp, ifThenExp); /** * final as: * result = ValidateNameMinLength(input, minLength); * if (!result.IsOk) * { * return result; * } */ return re; } } } catch (Exception e) { Console.WriteLine(e); throw; } } [Test] public void Run() { // see code in demo repo } public static ValidateResult Validate(CreateClaptrapInput input) { return _func.Invoke(input, 3); } public static ValidateResult ValidateNameRequired(CreateClaptrapInput input) { return string.IsNullOrEmpty(input.Name) ? ValidateResult.Error("missing Name") : ValidateResult.Ok(); } public static ValidateResult ValidateNameMinLength(CreateClaptrapInput input, int minLength) { return input.Name.Length < minLength ? ValidateResult.Error(#34;Length of Name should be great than {minLength}") : ValidateResult.Ok(); } } }

代码要点:

  1. ValidateCore 方法被拆分为了 ValidateNameRequired 和 ValidateNameMinLength 两个方法,分别验证 Name 的 Required 和 MinLength。
  2. Init 方法中使用了 local function 从而实现了方法 “先使用后定义” 的效果。读者可以自上而下阅读,从顶层开始了解整个方法的逻辑。
  3. Init 整体的逻辑就是通过表达式将 ValidateNameRequired 和 ValidateNameMinLength 重新组合成一个形如 ValidateCore 的委托 Func<CreateClaptrapInput, int, ValidateResult>。
  4. Expression.Parameter 用于标明委托表达式的参数部分。
  5. Expression.Variable 用于标明一个变量,就是一个普通的变量。类似于代码中的 var a。
  6. Expression.Label 用于标明一个特定的位置。在该样例中,主要用于标定 return 语句的位置。熟悉 goto 语法的开发者知道, goto 的时候需要使用 label 来标记想要 goto 的地方。而实际上,return 就是一种特殊的 goto。所以想要在多个语句块中 return 也同样需要标记后才能 return。
  7. Expression.Block 可以将多个表达式顺序组合在一起。可以理解为按顺序写代码。这里我们将 CreateDefaultResult、CreateValidateNameRequiredExpression、CreateValidateNameMinLengthExpression 和 Label 表达式组合在一起。效果就类似于把这些代码按顺序拼接在一起。
  8. CreateValidateNameRequiredExpression 和 CreateValidateNameMinLengthExpression 的结构非常类似,因为想要生成的结果表达式非常类似。
  9. 不必太在意 CreateValidateNameRequiredExpression 和 CreateValidateNameMinLengthExpression 当中的细节。可以在本样例全部阅读完之后再尝试了解更多的 Expression.XXX 方法。
  10. 经过这样的修改之后,我们就实现了扩展。假设现在需要对 Name 增加一个 MaxLength 不得超过 16 的验证。只需要增加一个 ValidateNameMaxLength 的静态方法,添加一个 CreateValidateNameMaxLengthExpression 的方法,并且加入到 Block 中即可。读者可以尝试动手操作一波实现这个效果。

第三步,读取属性

我们来改造 ValidateNameRequired 和 ValidateNameMinLength 两个方法。因为现在这两个方法接收的是 CreateClaptrapInput 作为参数,内部的逻辑也被写死为验证 Name,这很不优秀。

我们将改造这两个方法,使其传入 string name 表示验证的属性名称,string value 表示验证的属性值。这样我们就可以将这两个验证方法用于不限于 Name 的更多属性。

using System; using System.ComponentModel.DataAnnotations; using System.Diagnostics; using System.Linq.Expressions; using FluentAssertions; using NUnit.Framework; // ReSharper disable InvalidXmlDocComment namespace Newbe.ExpressionsTests { /// <summary> /// Property Expression /// </summary> public class X03PropertyValidationTest03 { private const int Count = 10_000; private static Func<CreateClaptrapInput, int, ValidateResult> _func; [SetUp] public void Init() { try { var finalExpression = CreateCore(); _func = finalExpression.Compile(); Expression<Func<CreateClaptrapInput, int, ValidateResult>> CreateCore() { // exp for input var inputExp = Expression.Parameter(typeof(CreateClaptrapInput), "input"); var nameProp = typeof(CreateClaptrapInput).GetProperty(nameof(CreateClaptrapInput.Name)); Debug.Assert(nameProp != null, nameof(nameProp) + " != null"); var namePropExp = Expression.Property(inputExp, nameProp); var nameNameExp = Expression.Constant(nameProp.Name); var minLengthPExp = Expression.Parameter(typeof(int), "minLength"); // exp for output var resultExp = Expression.Variable(typeof(ValidateResult), "result"); // exp for return statement var returnLabel = Expression.Label(typeof(ValidateResult)); // build whole block var body = Expression.Block( new[] {resultExp}, CreateDefaultResult(), CreateValidateNameRequiredExpression(), CreateValidateNameMinLengthExpression(), Expression.Label(returnLabel, resultExp)); // build lambda from body var final = Expression.Lambda<Func<CreateClaptrapInput, int, ValidateResult>>( body, inputExp, minLengthPExp); return final; Expression CreateDefaultResult() { var okMethod = typeof(ValidateResult).GetMethod(nameof(ValidateResult.Ok)); Debug.Assert(okMethod != null, nameof(okMethod) + " != null"); var methodCallExpression = Expression.Call(okMethod); var re = Expression.Assign(resultExp, methodCallExpression); /** * final as: * result = ValidateResult.Ok() */ return re; } Expression CreateValidateNameRequiredExpression() { var requireMethod = typeof(X03PropertyValidationTest03).GetMethod(nameof(ValidateStringRequired)); var isOkProperty = typeof(ValidateResult).GetProperty(nameof(ValidateResult.IsOk)); Debug.Assert(requireMethod != null, nameof(requireMethod) + " != null"); Debug.Assert(isOkProperty != null, nameof(isOkProperty) + " != null"); var requiredMethodExp = Expression.Call(requireMethod, nameNameExp, namePropExp); var assignExp = Expression.Assign(resultExp, requiredMethodExp); var resultIsOkPropertyExp = Expression.Property(resultExp, isOkProperty); var conditionExp = Expression.IsFalse(resultIsOkPropertyExp); var ifThenExp = Expression.IfThen(conditionExp, Expression.Return(returnLabel, resultExp)); var re = Expression.Block( new[] {resultExp}, assignExp, ifThenExp); /** * final as: * result = ValidateNameRequired("Name", input.Name); * if (!result.IsOk) * { * return result; * } */ return re; } Expression CreateValidateNameMinLengthExpression() { var minLengthMethod = typeof(X03PropertyValidationTest03).GetMethod(nameof(ValidateStringMinLength)); var isOkProperty = typeof(ValidateResult).GetProperty(nameof(ValidateResult.IsOk)); Debug.Assert(minLengthMethod != null, nameof(minLengthMethod) + " != null"); Debug.Assert(isOkProperty != null, nameof(isOkProperty) + " != null"); var requiredMethodExp = Expression.Call(minLengthMethod, nameNameExp, namePropExp, minLengthPExp); var assignExp = Expression.Assign(resultExp, requiredMethodExp); var resultIsOkPropertyExp = Expression.Property(resultExp, isOkProperty); var conditionExp = Expression.IsFalse(resultIsOkPropertyExp); var ifThenExp = Expression.IfThen(conditionExp, Expression.Return(returnLabel, resultExp)); var re = Expression.Block( new[] {resultExp}, assignExp, ifThenExp); /** * final as: * result = ValidateNameMinLength("Name", input.Name, minLength); * if (!result.IsOk) * { * return result; * } */ return re; } } } catch (Exception e) { Console.WriteLine(e); throw; } } [Test] public void Run() { // see code in demo repo } public static ValidateResult Validate(CreateClaptrapInput input) { return _func.Invoke(input, 3); } public static ValidateResult ValidateStringRequired(string name, string value) { return string.IsNullOrEmpty(value) ? ValidateResult.Error(#34;missing {name}") : ValidateResult.Ok(); } public static ValidateResult ValidateStringMinLength(string name, string value, int minLength) { return value.Length < minLength ? ValidateResult.Error(#34;Length of {name} should be great than {minLength}") : ValidateResult.Ok(); } } }

代码要点:

  1. 正如前文所述,我们修改了 ValidateNameRequired ,并重命名为 ValidateStringRequired。 ValidateNameMinLength -> ValidateStringMinLength。
  2. 修改了 CreateValidateNameRequiredExpression 和 CreateValidateNameMinLengthExpression,因为静态方法的参数发生了变化。
  3. 通过这样的改造,我们便可以将两个静态方法用于更多的属性验证。读者可以尝试增加一个 NickName 属性。并且进行相同的验证。

第四步,支持多个属性验证

接下来,我们通过将验证 CreateClaptrapInput 所有的 string 属性。

using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Diagnostics; using System.Linq; using System.Linq.Expressions; using System.Reflection; using FluentAssertions; using NUnit.Framework; // ReSharper disable InvalidXmlDocComment namespace Newbe.ExpressionsTests { /// <summary> /// Reflect Properties /// </summary> public class X03PropertyValidationTest04 { private const int Count = 10_000; private static Func<CreateClaptrapInput, int, ValidateResult> _func; [SetUp] public void Init() { try { var finalExpression = CreateCore(); _func = finalExpression.Compile(); Expression<Func<CreateClaptrapInput, int, ValidateResult>> CreateCore() { // exp for input var inputExp = Expression.Parameter(typeof(CreateClaptrapInput), "input"); var minLengthPExp = Expression.Parameter(typeof(int), "minLength"); // exp for output var resultExp = Expression.Variable(typeof(ValidateResult), "result"); // exp for return statement var returnLabel = Expression.Label(typeof(ValidateResult)); var innerExps = new List<Expression> {CreateDefaultResult()}; var stringProps = typeof(CreateClaptrapInput) .GetProperties() .Where(x => x.PropertyType == typeof(string)); foreach (var propertyInfo in stringProps) { innerExps.Add(CreateValidateStringRequiredExpression(propertyInfo)); innerExps.Add(CreateValidateStringMinLengthExpression(propertyInfo)); } innerExps.Add(Expression.Label(returnLabel, resultExp)); // build whole block var body = Expression.Block( new[] {resultExp}, innerExps); // build lambda from body var final = Expression.Lambda<Func<CreateClaptrapInput, int, ValidateResult>>( body, inputExp, minLengthPExp); return final; Expression CreateDefaultResult() { var okMethod = typeof(ValidateResult).GetMethod(nameof(ValidateResult.Ok)); Debug.Assert(okMethod != null, nameof(okMethod) + " != null"); var methodCallExpression = Expression.Call(okMethod); var re = Expression.Assign(resultExp, methodCallExpression); /** * final as: * result = ValidateResult.Ok() */ return re; } Expression CreateValidateStringRequiredExpression(PropertyInfo propertyInfo) { var requireMethod = typeof(X03PropertyValidationTest04).GetMethod(nameof(ValidateStringRequired)); var isOkProperty = typeof(ValidateResult).GetProperty(nameof(ValidateResult.IsOk)); Debug.Assert(requireMethod != null, nameof(requireMethod) + " != null"); Debug.Assert(isOkProperty != null, nameof(isOkProperty) + " != null"); var namePropExp = Expression.Property(inputExp, propertyInfo); var nameNameExp = Expression.Constant(propertyInfo.Name); var requiredMethodExp = Expression.Call(requireMethod, nameNameExp, namePropExp); var assignExp = Expression.Assign(resultExp, requiredMethodExp); var resultIsOkPropertyExp = Expression.Property(resultExp, isOkProperty); var conditionExp = Expression.IsFalse(resultIsOkPropertyExp); var ifThenExp = Expression.IfThen(conditionExp, Expression.Return(returnLabel, resultExp)); var re = Expression.Block( new[] {resultExp}, assignExp, ifThenExp); return re; } Expression CreateValidateStringMinLengthExpression(PropertyInfo propertyInfo) { var minLengthMethod = typeof(X03PropertyValidationTest04).GetMethod(nameof(ValidateStringMinLength)); var isOkProperty = typeof(ValidateResult).GetProperty(nameof(ValidateResult.IsOk)); Debug.Assert(minLengthMethod != null, nameof(minLengthMethod) + " != null"); Debug.Assert(isOkProperty != null, nameof(isOkProperty) + " != null"); var namePropExp = Expression.Property(inputExp, propertyInfo); var nameNameExp = Expression.Constant(propertyInfo.Name); var requiredMethodExp = Expression.Call(minLengthMethod, nameNameExp, namePropExp, minLengthPExp); var assignExp = Expression.Assign(resultExp, requiredMethodExp); var resultIsOkPropertyExp = Expression.Property(resultExp, isOkProperty); var conditionExp = Expression.IsFalse(resultIsOkPropertyExp); var ifThenExp = Expression.IfThen(conditionExp, Expression.Return(returnLabel, resultExp)); var re = Expression.Block( new[] {resultExp}, assignExp, ifThenExp); return re; } } } catch (Exception e) { Console.WriteLine(e); throw; } } [Test] public void Run() { // see code in demo repo } public static ValidateResult Validate(CreateClaptrapInput input) { return _func.Invoke(input, 3); } public static ValidateResult ValidateStringRequired(string name, string value) { return string.IsNullOrEmpty(value) ? ValidateResult.Error(#34;missing {name}") : ValidateResult.Ok(); } public static ValidateResult ValidateStringMinLength(string name, string value, int minLength) { return value.Length < minLength ? ValidateResult.Error(#34;Length of {name} should be great than {minLength}") : ValidateResult.Ok(); } public class CreateClaptrapInput { [Required] [MinLength(3)] public string Name { get; set; } [Required] [MinLength(3)] public string NickName { get; set; } } } }

代码要点:

  1. 在 CreateClaptrapInput 中增加了一个属性 NickName ,测试用例也将验证该属性。
  2. 通过 List<Expression> 我们将更多动态生成的表达式加入到了 Block 中。因此,我们可以对 Name 和 NickName 都生成验证表达式。

第五步,通过 Attribute 决定验证内容

尽管前面我们已经支持验证多种属性了,但是关于是否进行验证以及验证的参数依然是写死的(例如:MinLength 的长度)。

本节,我们将通过 Attribute 来决定验证的细节。例如被标记为 Required 是属性才会进行必填验证。

using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Diagnostics; using System.Linq; using System.Linq.Expressions; using System.Reflection; using FluentAssertions; using NUnit.Framework; // ReSharper disable InvalidXmlDocComment namespace Newbe.ExpressionsTests { /// <summary> /// Using Attribute /// </summary> public class X03PropertyValidationTest05 { private const int Count = 10_000; private static Func<CreateClaptrapInput, ValidateResult> _func; [SetUp] public void Init() { try { var finalExpression = CreateCore(); _func = finalExpression.Compile(); Expression<Func<CreateClaptrapInput, ValidateResult>> CreateCore() { // exp for input var inputExp = Expression.Parameter(typeof(CreateClaptrapInput), "input"); // exp for output var resultExp = Expression.Variable(typeof(ValidateResult), "result"); // exp for return statement var returnLabel = Expression.Label(typeof(ValidateResult)); var innerExps = new List<Expression> {CreateDefaultResult()}; var stringProps = typeof(CreateClaptrapInput) .GetProperties() .Where(x => x.PropertyType == typeof(string)); foreach (var propertyInfo in stringProps) { if (propertyInfo.GetCustomAttribute<RequiredAttribute>() != null) { innerExps.Add(CreateValidateStringRequiredExpression(propertyInfo)); } var minlengthAttribute = propertyInfo.GetCustomAttribute<MinLengthAttribute>(); if (minlengthAttribute != null) { innerExps.Add( CreateValidateStringMinLengthExpression(propertyInfo, minlengthAttribute.Length)); } } innerExps.Add(Expression.Label(returnLabel, resultExp)); // build whole block var body = Expression.Block( new[] {resultExp}, innerExps); // build lambda from body var final = Expression.Lambda<Func<CreateClaptrapInput, ValidateResult>>( body, inputExp); return final; Expression CreateDefaultResult() { var okMethod = typeof(ValidateResult).GetMethod(nameof(ValidateResult.Ok)); Debug.Assert(okMethod != null, nameof(okMethod) + " != null"); var methodCallExpression = Expression.Call(okMethod); var re = Expression.Assign(resultExp, methodCallExpression); /** * final as: * result = ValidateResult.Ok() */ return re; } Expression CreateValidateStringRequiredExpression(PropertyInfo propertyInfo) { var requireMethod = typeof(X03PropertyValidationTest05).GetMethod(nameof(ValidateStringRequired)); var isOkProperty = typeof(ValidateResult).GetProperty(nameof(ValidateResult.IsOk)); Debug.Assert(requireMethod != null, nameof(requireMethod) + " != null"); Debug.Assert(isOkProperty != null, nameof(isOkProperty) + " != null"); var namePropExp = Expression.Property(inputExp, propertyInfo); var nameNameExp = Expression.Constant(propertyInfo.Name); var requiredMethodExp = Expression.Call(requireMethod, nameNameExp, namePropExp); var assignExp = Expression.Assign(resultExp, requiredMethodExp); var resultIsOkPropertyExp = Expression.Property(resultExp, isOkProperty); var conditionExp = Expression.IsFalse(resultIsOkPropertyExp); var ifThenExp = Expression.IfThen(conditionExp, Expression.Return(returnLabel, resultExp)); var re = Expression.Block( new[] {resultExp}, assignExp, ifThenExp); return re; } Expression CreateValidateStringMinLengthExpression(PropertyInfo propertyInfo, int minlengthAttributeLength) { var minLengthMethod = typeof(X03PropertyValidationTest05).GetMethod(nameof(ValidateStringMinLength)); var isOkProperty = typeof(ValidateResult).GetProperty(nameof(ValidateResult.IsOk)); Debug.Assert(minLengthMethod != null, nameof(minLengthMethod) + " != null"); Debug.Assert(isOkProperty != null, nameof(isOkProperty) + " != null"); var namePropExp = Expression.Property(inputExp, propertyInfo); var nameNameExp = Expression.Constant(propertyInfo.Name); var requiredMethodExp = Expression.Call(minLengthMethod, nameNameExp, namePropExp, Expression.Constant(minlengthAttributeLength)); var assignExp = Expression.Assign(resultExp, requiredMethodExp); var resultIsOkPropertyExp = Expression.Property(resultExp, isOkProperty); var conditionExp = Expression.IsFalse(resultIsOkPropertyExp); var ifThenExp = Expression.IfThen(conditionExp, Expression.Return(returnLabel, resultExp)); var re = Expression.Block( new[] {resultExp}, assignExp, ifThenExp); return re; } } } catch (Exception e) { Console.WriteLine(e); throw; } } [Test] public void Run() { // see code in demo repo } public class CreateClaptrapInput { [Required] [MinLength(3)] public string Name { get; set; } [Required] [MinLength(3)] public string NickName { get; set; } } } }

代码要点:

  1. 在构建 List<Expression> 时通过属性上的 Attribute 上的决定是否加入特定的表达式。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/49945.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信