◷ Reading Time: 3 minutes
Introduction
In the application, you may need to define some commonly used logic and then start reusing this. This separation can happen in two layers:
- Splitting into different Logic inside a Validation
- Splitting into Logic outside the Validation
Common logic
you may find Common logic useful for multiple Validations. For instance, IsNullOrEmpty, IsInRange,… You may implement this and reuse it inside a Validation or cross multiple Validations. If you want to use it across multiple Validations, you need to use the Include command to reference 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 the AddRule method.
private RuleSet GetRuleSet()
{
RuleSet rs = RuleSet.HierarchicalRuleSet();
rs.AddModel("Common/Strings", LoadAdapterUtility.LoadModel("Common.IsNullOrEmpty.xml"));
return rs;
}
Then you can use the RuleSet to create your Validator
private Validator GetValidator()
{
RuleSet rs = GetRuleSet();
// append new logic to ruleset
rs.AddModel("rules/validations", LoadAdapterUtility.LoadModel("Conditional_requiredField_UsingCommonRules.xml"));
// creating Validator directly, or we could use RuntimeEngine instead
var validator = new Validator(rs, "ruleset://rules/validations/PersonValidation");
return validator;
}
Direct file reference
Final Sample
<Logic name="NullOrEmpty" variable="$input">
<Or>
<Null value="$input"/>
<Empty value="$input"/>
</Or>
</Logic>
Referencing use RuleSet and ModelContainer
<Validation name="PersonValidation">
<!-- Referencing external logic -->
<Include source="ruleset://Common/Strings/NullOrEmpty"/>
<Logic name="check email using False">
<False message="Adult must have an email">
<When>
<And processAll="true">
<Check value= "Age>=18"/>
<Validate logic="NullOrEmpty" value="Email"/>
</And>
</When>
</False>
</Logic>
</Validation>
Next sections
In the next couple of articles, we will cover different aspects of modeling and executing the validation rules using Validation logic.
- Introduction to validation rules
- Validating hierarchy (Inheritance relation)
- Validating association (Aggregation, Composition)
- Validation rule execution and collecting results
- Pass extra input values to validation rules
- Extending validation conditions and actions
- How to apply rules under some conditions
- Referencing commonly used logic
- Sample for Order processing validation logic