只要十步,你就可以应用表达式树来优化动态调用(一)( 四 )
本步骤 , 我们将会把 ValidateCore 中写死的三个 return 路径拆分为不同的方法 , 然后再采用表达式拼接在一起 。
如果我们实现了 , 那么我们就有条件将更多的方法拼接在一起 , 实现一定程度的扩展 。
注意:演示代码将瞬间边长 , 不必感受太大压力 , 可以辅助后面的代码要点说明进行查看 。
using System;using System.ComponentModel.DataAnnotations;using System.Diagnostics;using System.Linq.Expressions;using FluentAssertions;using NUnit.Framework;// ReSharper disable InvalidXmlDocCommentnamespace Newbe.ExpressionsTests{////// Block Expression///public class X03PropertyValidationTest02{private const int Count = 10_000;private static Func _func;[SetUp]public void Init(){try{var finalExpression = CreateCore();_func = finalExpression.Compile();Expression> CreateCore(){// exp for inputvar inputExp = Expression.Parameter(typeof(CreateClaptrapInput), "input");var minLengthPExp = Expression.Parameter(typeof(int), "minLength");// exp for outputvar resultExp = Expression.Variable(typeof(ValidateResult), "result");// exp for return statementvar returnLabel = Expression.Label(typeof(ValidateResult));// build whole blockvar body = Expression.Block(new[] {resultExp},CreateDefaultResult(),CreateValidateNameRequiredExpression(),CreateValidateNameMinLengthExpression(),Expression.Label(returnLabel, resultExp));// build lambda from bodyvar final = Expression.Lambda>(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
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 小米11迎强敌,配120W超充,0到100%只要15分钟
- 只需799元就可以把旧iPad换成新款iPad?是真的!但这羊毛可没那么容易薅
- 马化腾这招太高明!网友:只要我改名的速度够快,禁令就追不上我
- 飞腾桌面腾锐D2000 CPU发布:最高2.6GHz、八核只要25W
- 苹果官网产品出现 Bug 价:上千元产品显示只要一两百元
- 华为手机为什么耗电快?只要关闭手机这6个开关,2天1充不是梦
- 苏宁易购双十二10亿补贴引爆吃货狂欢,西贝明星单品只要9.9
- 别错过,双节钜惠还有2天结束
- 人群|出差旅行也能带呼吸机?M1 Mini就可以
- 瑞士宽带商TWiFi可提供18年免费上网服务