Validation of the object FluentValidation Based on Rookey. Frame, fluentvalidation

Source: Internet
Author: User

Validation of the object FluentValidation Based on Rookey. Frame, fluentvalidation

Yesterday I introduced you to the room key. the entity design of the Frame framework. Today, we will continue to share with you the FluentValidation verification of the object. multiple verification methods can be set in the Frame framework: FluentValidation verification, PrimaryKeyFields field verification, form field verification, and custom verification. Today we will introduce FluentValidation verification.

To verify the object FluentValidation, you need to create a new object FluentValidation class,The object verification class must be named with the class name + Validator.And the inheritance class AbstractValidator <T>, where T is the entity class. In this framework, the icon management entity class is used as an example. See the following code:

/// <Summary> /// icon management verification /// </summary> public class Sys_IconManageValidator: abstractValidator <Sys_IconManage >{/// <summary> /// constructor /// </summary> public Sys_IconManageValidator () {Expression <Func <Sys_IconManage, bool> exp = x =>! X. styleClassName. startsWith ("icon-"); RuleFor (x => x. styleClassName ). length (5, 30 ). withMessage ("the length of the Style Class Name is between 5 and 30! ");}}

There will be a lot of tutorials on How to Write verification expressions for Baidu FluentValidation verification.

After the FluentValidation validation class is constructed and a validation expression is set, the Framework automatically calls FluentValidation verification before saving it.

For FluentValidation verification, methods for FluentValidation verification have been encapsulated in Rookey. Frame. Operate. Base \ CommonOperate. cs. You can directly call the method if needed.

/// <Summary> /// FluentValidation verification /// </summary> /// <param name = "moduleId"> module Id </param> /// <param name = "model"> Object </param> // <returns> </returns> public static string FluentVerify (Guid moduleId, object model) {Type modelType = GetModelType (moduleId); // FluentValidation verification Type fluentValidationType = GetFluentValidationModelType (moduleId); if (fluentValidationType! = Null) // FluentValidation verification type {// instantiate verification object obj = Activator. createInstance (fluentValidationType); MethodInfo method = fluentValidationType. getMethod ("Validate", new Type [] {modelType}); // reflection execution method object executedObj = method. invoke (obj, new object [] {model}); ValidationResult validateResult = executedObj as ValidationResult; if (validateResult! = Null &&! ValidateResult. isValid) {string errMsg = string. join (",", validateResult. errors. select (x => x. errorMessage); return errMsg;} return string. empty ;}

Today, FluentValidation verification will be introduced here. Next time we will introduce other verification methods and wish you a happy life!

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.