Data validation process through custom attribute and generic extension encapsulation

Source: Internet
Author: User

Source of demand:

In the daily work, business process is often a lot of continuous data flow, processing, presentation process, in this process, the inevitable is the work of data validation. Data validation work is a tedious repetition of labor, there is no technical content, need is the business process of accurate understanding and careful patience. In the actual production environment, often appear business bug final check is because the data validation is not in place, resulting in the result of processing data is unreasonable. The entire troubleshooting process is also time consuming, so it is desirable to provide a convenient way to validate data.

In the System.ComponentModel.DataAnnotations namespace, Microsoft defines a series of attribute for data validation and is fully functional, but there are some requirements (such as Greaterthan,lessthan) that require a custom implementation, so consider the root According to the actual project requirements, a set of required data validation attribute is customized, and a generic extension authentication method is provided, so that the current object instance can be validated on demand at any time.

Implementation process:

The implementation is very simple, two steps:

1: On-Demand custom data validation attribute, with range and LessThan implemented as follows:

[AttributeUsage (attributetargets.property)] Public classRangeattribute:attribute { Public DoubleMin {Get;Set; }  Public DoubleMax {Get;Set; }  Public stringerrormsg {Get;Set; }  PublicRangeattribute (intMinValue,intMaxValue,stringErrorMessage ="Argument out of range") {Min=MinValue; Max=MaxValue; ErrorMsg=errormessage; }}[attributeusage (Attributetargets.property)] Public classLessthanattribute:attribute { Public DoubleThreshold {Get;Set; }  Public stringerrormsg {Get;Set; }  PublicLessthanattribute (DoubleThresholdstringErrorMessage ="Argument must less than the threshold") {Threshold=threshold; ErrorMsg=errormessage; }    }

2: Implement generic extension:

 Public Static void Validate<t> ( this T obj) {   //todo:validate Attribute Handle  }

The specific code is longer, not all of the thin layout is posted.

(implemented under the Https://github.com/cerasumat/Utilities/tree/master/CeraSumat.Utilities/Validation path of the Validater.cs file can be found)

How to use:

In the following example, defining a student class and adding data validation attribute as needed, after instantiating the class, invokes the Validate () method of the instance to easily validate the data:

         Public classStudent {[MaxLength (5)] [NotEqual ("Jiak")]             Public stringName {Get;Set; } [Range (1, -)] [NotEqual (1)] [LessThan (3.5)]             Public intId {Get;Set; } }        Static voidMain (string[] args) {            vars =NewStudent {Id =4, Name ="Jiak"};        S.validate (); }    

Data validation process through custom attribute and generic extension encapsulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.