Validation of commonlibrary

Source: Internet
Author: User

Commnlibrary is the best open-source general-purpose class library I have seen. It contains about 40 general-purpose modules,CodeThe writing is also very good. Now we can take some time to learn in detail and record some common modules to replace the previous general class libraries.

 

Validation

When I see the implementation and application of this verification module,

 

 
Validation. isnumeric ("asdklf ")
 
// Parameter 1: Text Value parameter 2: can it be blank validation. isalphanumeric ("asd123dsd43", false)
// Parameter 1: Text Value parameter 2: can it be null, 3, 4, whether to check the maximum (small) length, 5, 6 maximum (small) length validation. isstringlengthmatch ("user01", false, true, true, 2, 12) // use an errors object to save the error set var errors = new errors (); validation. isalpha ("123abc", false, errors, ""); // use the Lamda expression to verify var val = new validator (valevent =>{ int errcount = valevent. results. count; validation. isemail ("Kishore @", false, valevent. results, String. empty); validation. isurl ("http: // www", false, valevent. results, String. empty); validation. isphoneus ("111-111-111", false, valevent. results, String. empty); Return errcount = valevent. results. count;}); printerrors (Val. validate (); // 4. make all type verification get var val = new validatorwithrules (); Val. add (E => validation. isemail ("Kishore @", false, E. results, String. empty); Val. add (E => validation. isurl ("http: // www", false, E. results, String. empty); Val. add (E => validation. isphoneus ("111-111-111", false, E. results, String. empty); printerrors (Val. validate ());
// 5. use a chained call to verify Val. check () => User. username ). isnotnull (). isbetween (1, 50 ). check () => User. createdate ). isaftertoday (). check ("email", user. email ). isvalidemail (). check () => User. mobilephone ). isvalidphoneus ();
 
// 6. the verification results of executing multiple custom classes at a time are saved in a set. var validators = new list <ivalidator> () {New mycustomuseridvalidator ("admin "), new mycustomuseridvalidator ("Batman")}; // run all the validators and collect the errors. validationresults errors = new validationresults (); validationutils. validate (validators, errors );

After reading the above methods, the verification required by the server is included. Next, we will analyze the implementation of this module from the overall design to the implementation of each method:

 

Validation Module Interface diagram:

When I first saw this, I thought the design of this function module was complicated, but I figured out the specific relationship, which is actually complicated. Start with ierrors, errors, ivalidationresults, and validationresults.

IerrorsThe interface defines some methods and attributes of the operation set,ErrorsClass to save the error information through a dictionary <string, string>, list <string>. Here, two sets are used to keep the error information, dictionary is added because it is used to indicate the verification error for the key, and the value corresponds to the error value. For example, key: User Name value: cannot be blank.


IvalidationresultsThe interface inherits the ierrors interface and has only one attribute, bool isvalide, to control whether the verification is passed.ValidationresultsInheritsIerrorsInterface, which only defines a readonly self-object, so that you can not only use the public method of the errors class, in addition, you have added control over the error set (two sets in the errors class) so that the error set cannot be changed, this design can maximize code flexibility and clarify the division of labor, but I think it is too cumbersome. If I design it, I will remove the ivalidationresults interface and validationresults class, control in ierror and errors classes :)


IvalidatorstatefulAndIvalidatornonestatefulThe interface defines the verification method. The difference is that nonestateful does not save the object state. This allows all the verification tasks to be completed in one method.


IvalidatorThe interface inheritsIvalidatorstatefulAndIvalidatornonestatefulInterface. Here it is just an empty interface.


IvalidatorwithrulesInheritanceIvalidatorInterface, which is used to define custom Verification


ValidatorClass, inheritedIvalidatorThis interface implements all the methods of ivalidatorstateful and ivalidatornonestateful.


ValidatorwithrulesInheritanceValidatorClass, implementationIvalidatorwithrulesInterface


Ivalidator is a null interface. Here, the flexibility of polymorphism is introduced. In the validator class, several implementation methods are defined as virtual methods, so that successors can easily rewrite them.I feel that the entire interface is too complex to be designed, and the project nature is different. The complicated internal design brings high flexibility and scalability and is encapsulated into a DLL, the caller only needs to know how to call the code. The common Class Library maintenance personnel can control code extension and maintenance. In normal times, this design works onProgramThe level of understanding of the Code requires a little bit. It cannot be discussed in a large area in the project. Otherwise, we will fix a class after discussing it for 10 days and half a month.

This hydrology has been written for three days. It is sharp. Take time to write a bit every night to have fun in your plain life. The specific implementation is now discussed. Today, I learned how to implement fluent interface (smooth interface) in my project. Write down the validatorfluent implementation in commnlibrary today,

Validatorfluent

In Example 5, the syntax of jquery's chained call is quite comfortable. Such a syntax format can make the code simple, "efficient", and coherent. The implementation is not complicated. Let's look at the code in this class. All the methods return this, which facilitates the continuation of chained calls. What surprised me is that the author adds one in this class:

 

 
Public validatorfluent end () {return this ;}


The use of human thinking to write code for computer reading greatly increases the readability of the code, and part of the role of the extension method I understand also lies in this. In addition, the check method has three reloads. Check (expression <func <Object> exp) supports expression parsing, in the other two check methods, why don't I use generic t to transmit parameters? In this class, passing generics is of little significance. In some verifications, all types still need to be inferred or converted to specific types (such as: string); in this class, combined with the validation of the validation class, if necessary, you can continue to add the validation of the validation class of isdate and isnumeric. All verification error messages are put in the set of validationresults class mentioned above for merging.

 

It's too late. I went to bed first. I will continue writing tomorrow and want to talk about everything. I found that many places can be understood at a Glance. I don't need to talk about it, and I don't know about it.ArticleI hope you can give me some comments on this topic.

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.