Referencing external logic

From FlexRule Wiki
Jump to: navigation, search

Introduction

In the application, you may need to define some commonly used logic and then start reusing them. This separation can happen in two layers:

  1. Splitting into different Logic inside a Validation
  2. Splitting into Logic outside the Validation

Common logic

Common logic are the one that you may find them usually are useful for multiple Validations. For instance IsNullOrEmpty, IsInRange,... You may implement them and reuse them inside a Validation or cross mutliple Validations. If you want to use them across multiple Validations you need to use Include command to reference an external logic to a Validation.

RuleSet and ModelContainer

In order to create a RuleSet, you simply load your ModelContainer and add it to a RuleSet collection using AddRule method.

  1. private RuleSet GetRuleSet()
  2. {
  3.     RuleSet rs = RuleSet.HierarchicalRuleSet();
  4.     rs.AddModel("Common/Strings", LoadAdapterUtility.LoadModel("Common.IsNullOrEmpty.xml"));
  5.     return rs;
  6. }

And then you can use the RuleSet to create your Validator

  1. private Validator GetValidator()
  2. {
  3.     RuleSet rs = GetRuleSet();
  4.     // append new logic to ruleset
  5.     rs.AddModel("rules/validations", LoadAdapterUtility.LoadModel("Conditional_requiredField_UsingCommonRules.xml"));
  6.  
  7.     // creating Validator directly, or we could use RuntimeEngine instead
  8.     var validator = new Validator(rs, "ruleset://rules/validations/PersonValidation");
  9.     return validator;
  10. }

Direct file reference

Final Sample

  1. <Logic name="NullOrEmpty" variable="$input">
  2.   <Or>
  3.     <Null value="$input"/>
  4.     <Empty value="$input"/>
  5.   </Or>
  6. </Logic>

Referencing use RuleSet and ModelContainer

  1. <Validation name="PersonValidation">
  2.   <!-- Referencing external logic -->
  3.   <Include source="ruleset://Common/Strings/NullOrEmpty"/>
  4.  
  5.   <Logic name="check email using False">
  6.     <False message="Adult must have an email">
  7.       <When>
  8.         <And processAll="true">
  9.           <Check value= "Age>=18"/>
  10.           <Validate logic="NullOrEmpty" value="Email"/>
  11.         </And>
  12.       </When>
  13.     </False>
  14.   </Logic>
  15.  
  16. </Validation>

Next sections

In the next couple of articles we will cover different aspect of modelling and executing the validation rules using Validation engine.

  1. Introduction to validation rules
  2. Validating hierarchy (Inheritance relation)
  3. Validating association (Aggregation, Composition)
  4. Validation rule execution and collecting results
  5. Pass extra input values to validation rules
  6. Extending validation conditions and actions
  7. How to apply rules under some conditions
  8. Referencing commonly used logic
  9. Sample for Order processing validation logic