通过自定义Attribute及泛型extension封装数据验证过程
需求来源:
在日常工作中,业务流程往往就是大量持续的数据流转、加工、展现过程,在这个过程中,不可避免的就是数据验证的工作。数据验证工作是个很枯燥的重复劳动,没有什么技术含量,需要的就是对业务流程的准确理解以及细心耐心。在实际生产环境中,经常出现业务BUG最终检查是由于数据验证不到位,最终导致加工数据的结果不合理。整个排查过程也是费时费力,因此,希望能够提供便捷的数据验证方式。
在System.ComponentModel.DataAnnotations命名空间中,微软定义了一系列的Attribute,用于数据验证工作,功能很全面,但也有一些需求(如GreaterThan,LessThan)需要自定义实现,因此考虑根据实际项目需求,定制化一套需要的数据验证Attribute,并提供一个泛型extension验证方法,这样可以随时按需对当前对象实例进行数据验证。
实现过程:
实现非常简单,两个步骤:
1:按需自定义数据验证Attribute,如下实现了Range及LessThan:
[AttributeUsage(AttributeTargets.Property)] public class RangeAttribute : Attribute { public double Min { get; set; } public double Max { get; set; } public string ErrorMsg { get; set; } public RangeAttribute(int minValue, int maxValue, string errorMessage = "Argument out of range") { Min = minValue; Max = maxValue; ErrorMsg = errorMessage; } } [AttributeUsage(AttributeTargets.Property)] public class LessThanAttribute : Attribute { public double Threshold { get; set; } public string ErrorMsg { get; set; } public LessThanAttribute(double threshold, string errorMessage = "Argument must less than the threshold") { Threshold = threshold; ErrorMsg = errorMessage; } }
2:实现泛型extension:
public static void Validate<T>(this T obj) { //TODO: Validate Attribute Handle }
具体代码较长,为精简版面就不全部贴出了。
(实现在https://github.com/cerasumat/Utilities/tree/master/CeraSumat.Utilities/Validation路径下的Validater.cs文件可以找到)
如何使用:
如下例,定义一个Student类,并按需加入数据验证Attribute,实例化该类后,调用实例的Validate()方法,即可便捷地进行数据验证:
public class Student { [MaxLength()] [NotEqual("JiaK")] public string Name { get; set; } [Range(, )] [NotEqual()] [LessThan(3.5)] public int Id { get; set; } } static void Main(string[] args) { , Name = "Jiak"}; s.Validate(); }
通过自定义Attribute及泛型extension封装数据验证过程的更多相关文章
- vue 自定义指令input表单的数据验证
一.代码 <template> <div class="check" > <h3>{{msg}}</h3> <div clas ...
- MVC 3 数据验证 Model Validation 详解
在MVC 3中 数据验证,已经应用的非常普遍,我们在web form时代需要在View端通过js来验证每个需要验证的控件值,并且这种验证的可用性很低.但是来到了MVC 新时代,我们可以通过MVC提供的 ...
- (转)MVC 3 数据验证 Model Validation 详解
继续我们前面所说的知识点进行下一个知识点的分析,这一次我们来说明一下数据验证.其实这是个很容易理解并掌握的地方,但是这会浪费大家狠多的时间,所以我来总结整理一下,节约一下大家宝贵的时间. 在MVC 3 ...
- <转>ASP.NET学习笔记之MVC 3 数据验证 Model Validation 详解
MVC 3 数据验证 Model Validation 详解 再附加一些比较好的验证详解:(以下均为引用) 1.asp.net mvc3 的数据验证(一) - zhangkai2237 - 博客园 ...
- Programming With Objective-C---- Encapsulating Data ---- Objective-C 学习(三) 封装数据
Programming with Objective-C Encapsulating Data In addition to the messaging behavior covered in t ...
- XsdGen:通过自定义Attribute与反射自动生成XSD
前言 系统之间的数据交互往往需要事先定义一些契约,在WCF中我们需要先编写XSD文件,然后通过自动代码生成工具自动生成C#对象.对于刚刚接触契约的人来说,掌握XMLSpy之类的软件之后确实比手写XML ...
- MVC数据验证原理及自定义ModelValidatorProvider实现无编译修改验证规则和错误信息
Asp.net MVC中的提供非常简单易用的数据验证解决方案. 通过System.ComponentModel.DataAnnotations提供的很多的验证规则(Required, StringLe ...
- Java实战之01Struts2-03属性封装、类型转换、数据验证
九.封装请求正文到对象中 1.静态参数封装 在struts.xml配置文件中,给动作类注入值.调用的是setter方法. 原因:是由一个staticParams的拦截器完成注入的. 2.动态参数封装: ...
- C#自定义Attribute值的获取与优化
C#自定义Attribute值的获取是开发中会经常用到的,一般我们的做法也就是用反射进行获取的,代码也不是很复杂. 1.首先有如下自定义的Attribute [AttributeUsage(Attri ...
随机推荐
- UNITY在VS中调试
下载地址:https://visualstudiogallery.msdn.microsoft.com/site/search?f%5B0%5D.Type=RootCategory&f%5B0 ...
- [No00004D]深度思考好文:软件工程师的困境
昨天是我一同学结婚的好日子,同学们大家聊各自的工作,有个同学突然问了我一句:我们同学中好像做软件的不多?如果再细分,好像做网络相关的更少? 回想起当时为何读计算机信息管理的专业,是因为那时听说读电脑未 ...
- 转载和积累系列 - 深入理解HTTP协议
深入理解HTTP协议 1. 基础概念篇 1.1 介绍 HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写.它的发展是万维网协会(World Wide Web C ...
- [推荐]两款Flash上传插件(1)–CFUpdate文件批量上传组件
搞开发的同学都知道,网上可搜到的可用的Flash图片上传的组件少之又少,可定制界面,不需要安装组件,且可免费用于任何商业系统的,更是凤毛麟角,如果你和我一样,迫切需要一款这样的一款组件的话,不妨试试C ...
- Centos5.8 安装 MySQL5.6.19
查看已经安装的mysql: sudo yum list installed |grep mysql 删除 sudo yum remove mysql 安装 sudo rpm -ivh MySQL-se ...
- git 解决fatal: Not a git repository
我用git add file添加文件时出现这样错误: fatal: Not a git repository (or any of the parent directories): .git 提示说没 ...
- ubuntu Apache 2命令
Task: Start Apache 2 Server /启动apache服务# /etc/init.d/apache2 startor$ sudo /etc/init.d/apache2 start ...
- 使用spring boot+mybatis+mysql 构建RESTful Service
开发目标 开发两个RESTful Service Method Url Description GET /article/findAll POST /article/insert 主要使用到的技术 j ...
- Html5的一些引擎使用感触
记得在2011年的时候,51CTO曾经采访我对H5的看法,因为当时Html5小组和雷友的关系,感觉是一片大火的形式,当时我的看法是:第一盈利模式不清晰,第二硬件跟不上,第三技术不成熟. 第一和第二点很 ...
- Fragment生命周期