Implement an object verification Library series and verification Library Series

Source: Internet
Author: User

Implement an object verification Library series and verification Library Series
Past Situation Review:

Previous Article 1) interface Introduction and general idea overview briefly describe the design idea of the next Database

This article will talk about how to implement the validators. Thank you for your guidance on faster and better solutions.

2) validators

We start with the authenticator called by the user, so we should first do this

public class Validator : IValidator{public IValidateResult Validate(ValidateContext context){}}

But is there no rule for our validators? Recall that we seem to have added an interface for the validator to set rules for the Fluent design, so we should implement the validator as follows:

Public class Validator: IValidatorSetter {private List <IValidateRule> m_Rules = new List <rule> (); // Save the verification Rule public void SetRules (IEnumerable <IValidateRule> rules) {ParamHelper. checkParamNull (rules, "rules", "Can't be null"); m_Rules.AddRange (rules); // Add verification rules} public IValidateResult Validate (ValidateContext context) {ParamHelper. checkParamNull (context, "context", "Can't be null"); var list = con Text. RuleSetList; if (! List. IsEmptyOrNull () {context. RuleSetList = list. Where (I =>! String. isNullOrEmpty (I )). select (I => I. toUpper ()). toArray ();} // process the validation rule grouping flag Filter list var rules = m_Rules.Where (I => context. ruleSelector. canExecute (I, context )). toArray (); // filter validation rule var result = Container. resolve <IValidateResult> (); if (! Rules. isEmptyOrNull () {var tasks = rules. select (I => Task. factory. startNew () => I. validate (context ))). toArray (); Task. waitAll (tasks); // run the verification if (tasks. any (I => I. isFaulted) {var exceptions = tasks. where (I => I. isFaulted ). select (I => I. exception); throw new aggresponexception (exceptions);} // handle task exceptions var failures = tasks. where (I => I. isCompleted ). selectcenters (I => I. result. failures); result. merge (failures); // process the verification result} return result ;}}

 

Then we implement the verification rules:

Public class ValidateRule: IValidateRule {public IValidateRule NextRule {get; set;} public string RuleSet {get; set;} public string ValueName {get; set;} public string Error {get; set;} public Func <ValidateContext, bool> Condition {get; set;} public Func <ValidateContext, string, string, IValidateResult> ValidateFunc {get; set ;} public IValidateResult Validate (ValidateContext context) {paramel Per. checkParamNull (ValidateFunc, "ValidateFunc", "Can't be null"); ParamHelper. checkParamNull (context, "context", "Can't be null"); IValidateResult result = null; if (Condition = null | Condition (context )) // determine the user's special filter condition {result = ValidateByFunc (context); // verify the execution rule} else {result = Container. resolve <IValidateResult> ();} return result;} public IValidateResult ValidateByFunc (ValidateContext context) {IVali DateResult result = ValidateFunc (context, ValueName, Error); // run the validation logic var nextRule = NextRule; if (nextRule! = Null & (result. isValid | context. option = ValidateOption. continue) // determine whether it is necessary to execute the sub-level verification rule {var nextResult = nextRule. validate (context); // executes the result of the Child verification rule. merge (nextResult. failures); // process the sublevel verification result} return result ;}}

  

Wait, it seems that we haven't implemented the filter of the validation rule grouping flag. Okay, let's implement it.

public class RuleSelector : IRuleSelector{public bool CanExecute(IValidateRule rule, ValidateContext context){return string.IsNullOrEmpty(rule.RuleSet)|| context.RuleSetList.IsEmptyOrNull()? true: context.RuleSetList.Contains(rule.RuleSet);}}

  

We bind the current implementation and interface to the container:

Public static class Container {public static ILifetimeScope CurrentScope {get; set;} public static void Init (Action <ContainerBuilder> action) {ParamHelper. checkParamNull (action, "action", "Can't be null"); Clear (); var builder = new ContainerBuilder (); action (builder); var container = builder. build (); CurrentScope = container. beginlifetimescript ();} public static void Init () {Init (builder =>{// bind interface builder. RegisterType <RuleSelector> (). as <IRuleSelector> (). singleInstance (); builder. register (c => new ValidateContext () {RuleSelector = c. resolve <IRuleSelector> ()}); builder. registerType <ValidateRule> (). as <IValidateRule> (). instancePerDependency (); builder. registerType <ValidateResult> (). as <IValidateResult> (). instancePerDependency (); builder. registerType <Validator> (). as <IValidatorSetter> (). instancePerDependenc Y () ;}) ;}public static void Clear () {var scope = CurrentScope; if (scope! = Null) scope. Dispose () ;}public static T Resolve <T> () {return CurrentScope. Resolve <T> ();}}

 

Okay, let's try it:

Container. init (); // initial interface var v = new Validator (); var rule = new ValidateRule () {ValidateFunc = (c, name, error) ==>{ var f = new ValidateFailure () {Name = name, Error = error, Value = c}; return new ValidateResult (new List <ValidateFailure> () {f}) ;}}; v. setRules (new List <ValidateRule> () {rule}); // simple setup verification var context = new ValidateContext () {RuleSelector = new RuleSelector ()}; var result = v. validate (context); // try Assert. isNotNull (result); Assert. true (result. isValid); Assert. isNotNull (result. failures); Assert. areEqual (0, result. failures. count );

 

Okay, I didn't implement the Code for setting verification rules in Fluent. It looks ugly now.

 

NEXT: 3) Fluent and extension method implementation

Related Article

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.