CreateValidateStringRequiredExp(){return (name, value) =>string.IsNullOrEmpty(value)? ValidateResult.Error($"missing {name}"): ValidateResult.Ok();}private static Expression CreateValidateStringMinLengthExp(int minLength){return (name, value) =>value.Length < minLength? ValidateResult.Error($"Length of {name} should be great than {minLength}"): ValidateResult.Ok();}}}代码要点:
- CreateValidateStringMinLengthExp 静态方法 , 传入一个参数创建得到一个和 CreateValidateStringRequiredExp 返回值一样的表达式 。 对比上一节中的 ValidateStringMinLengthExp, 实现了固定 int 参数而得到一个新表达式的操作 。 这就是一种柯理化的体现 。
- 为了统一都采用静态方法 , 我们将上一节中的 ValidateStringRequiredExp 也改为 CreateValidateStringRequiredExp 静态方法 , 这仅仅只是为了看起来一致(但实际上增加了一点点开销 , 因为没必要重复创建一个不变的表达式) 。
- 相应的调整一下 List 组装过程的代码 。
第八步 , 合并重复代码本节 , 我们将合并 CreateValidateStringRequiredExpression 和 CreateValidateStringMinLengthExpression 中重复的代码 。
其中只有 requiredMethodExp 的创建方式不同 。 因此 , 只要将这个参数从方法外面传入就可以抽离出公共部分 。
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 InvalidXmlDocCommentnamespace Newbe.ExpressionsTests{////// Refactor to CreateValidateExpression///public class X03PropertyValidationTest08{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");// exp for outputvar resultExp = Expression.Variable(typeof(ValidateResult), "result");// exp for return statementvar returnLabel = Expression.Label(typeof(ValidateResult));var innerExps = new List {CreateDefaultResult()};var stringProps = typeof(CreateClaptrapInput).GetProperties().Where(x => x.PropertyType == typeof(string));foreach (var propertyInfo in stringProps){if (propertyInfo.GetCustomAttribute() != null){innerExps.Add(CreateValidateStringRequiredExpression(propertyInfo));}var minlengthAttribute = propertyInfo.GetCustomAttribute();if (minlengthAttribute != null){innerExps.Add(CreateValidateStringMinLengthExpression(propertyInfo, minlengthAttribute.Length));}}innerExps.Add(Expression.Label(returnLabel, resultExp));// build whole blockvar body = Expression.Block(new[] {resultExp},innerExps);// build lambda from bodyvar final = Expression.Lambda>(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)=> CreateValidateExpression(propertyInfo,CreateValidateStringRequiredExp());Expression CreateValidateStringMinLengthExpression(PropertyInfo propertyInfo,int minlengthAttributeLength)=> CreateValidateExpression(propertyInfo,CreateValidateStringMinLengthExp(minlengthAttributeLength));Expression CreateValidateExpression(PropertyInfo propertyInfo,Expression
推荐阅读
-
灵锦文化假期结束A股市场走向不明,智能包装龙头完成数字化转型
-
星话天天看|事业一帆顺风,鸿运驾到,日子无忧发大财的属相,10月1-8号
-
-
丰田卡罗拉|拉人拉货两不误!卡罗拉旅行版发布,自带双人床,国产或12万起
-
图书馆里真的有早上进馆一直做题晚上闭馆才出去 ,午饭都常不吃的学生吗
-
-
雍亲王府|但康熙还是有传位给雍正的明显征兆,雍正虽不是康熙最心仪的人选
-
露笑科技|第三代半导体核心个股,露笑科技有这个底气吗?
-
王小二君临天下▲F-22紧急升空拦截,突发!两架他国军机突然闯入美国防空识别区
-
-
中芯国际|大资金都开始“避险”了,散户却依然在“狂欢”,这意味着什么?
-
萌宠的小世界|跑别人家去现在不肯走了 我问大家怎么办,聪明金毛记错自家楼层
-
-
上海虹口门户网站|区科委党委运用“O2O”模式开展“四史”学习教育
-
-
金杰娱乐|李小龙死在她床上,这个叫丁佩的女人有多狠?,20岁以性感出道
-
-
「华丽志」推出公益APP,为法国医护人员提供免费住房,Airbnb
-
-
天涯明月刀|3款近期超人气全新手游,天刀原神只是开胃菜,街霸对决才是C位