Implement Custom Contexts
- 3 minutes to read
In the eXpressApp Framework, when defining a Rule, specify Contexts in which the Rule will be checked. You can specify an Action as a Context, or use a built-in DefaultContexts.Save or DefaultContexts.Delete Context. If required, you can also implement your own Context and use it to check Rules in your applications. This topic clarifies the techniques for implementing your Contexts. For general information on the contextual validation concept, refer to the Validation Rules topic.
Before you start implementing your own Contexts, you should understand how Contexts are represented internally. There are actually no "built-in" Contexts or objects that represent Contexts (only a Context caption string). Validation implies that the RuleSet.Validate method of the RuleSet object assigned to the static Validator's Validator.RuleSet property is called. The object to be checked and the Context caption are passed as parameters. The following code demonstrates how to validate the obj object from the objectSpace Object Space in the context1 Context:
For instance, to provide validation in the DefaultContexts.Save and DefaultContexts.Delete Contexts, the built-in PersistenceValidationController subscribes to the IObjectSpace.Committing event and validates objects to be modified. Thus, all persistent objects will are checked in the DefaultContexts.Delete and DefaultContexts.Save Contexts automatically, when objects are being deleted or saved.
To provide validation when executing Actions, the Validation module's ActionValidationController adds a handler to the ActionBase.Executing event of each Action. In this event handler, the ValidateAll method is invoked as well.
You see that there is neither a Context repository nor registration. So if you want to check Rules in a custom Context, invoke the Validate or ValidateAll method in any place in your code, specifying the Context as a parameter. The Validation module's engine will automatically collect all Rules associated with this Context, and raise an exception in case of "broken" Rules.
The entire list of validation methods of the RuleSet class are as follows:
Checks whether or not an object satisfies a particular Rule. Returns an object of the RuleValidationResult type.
Checks whether or not a particular object satisfies all Rules associated with the specified Context. Returns an object of the RuleSetValidationResult type. If all Rules are satisfied, this object's IsValid property is set to true. Otherwise, it is set to false.
Similar to the ValidateTarget method, except that several objects can be checked.
Invokes the ValidateTarget method, and raises an exception if the IsValid property of the returned RuleSetValidationResult object is set to false.
Invokes the ValidateAllTargets method, and raises an exception if the IsValid property of the returned RuleSetValidationResult object is set to false.
These methods are supposed to be used from Controllers and Object Space's events, and not from the persistent class scope (they take the Object Space instance as a parameter). To access the RuleSet object, use the Validator.RuleSet property of the static Validator class.
You can handle the static RuleSet.CustomNeedToValidateRule event to turn validation on and off, depending on certain conditions. This event is raised when checking the validation context before the target object type is checked. Set the handler's NeedToValidateRule parameter to false to disable validation when required.