Implement Custom Contexts
- 3 minutes to read
When you define a Rule in XAF, you have to specify Contexts in which the Rule will be checked. You can specify an Action as a Context or use the 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 to implement your Contexts. For general information on the contextual validation concept, refer to the following topic: Validation Rules.
Before you implement your own Contexts, you should understand how Contexts are implemented internally. There are actually no “built-in” Contexts or objects that store Contexts (only a Context caption string). To perform validation, you need to call the IRuleSet.Validate method of the IRuleSet object and pass the object to be checked and the Context caption as parameters. To obtain the IRuleSet
object, you can use the Validator.GetService(IServiceProvider) method as demonstrated by the following code snippet:
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
using YourApplicationName.Module.BusinessObjects;
namespace YourApplicationName.Blazor.Server.Controllers;
public class CustomBlazorController : ViewController {
public CustomBlazorController() {
var myBlazorAction = new SimpleAction(this, "MyBlazorAction", PredefinedCategory.Edit);
myBlazorAction.Execute += (s, e) => {
var os = Application.CreateObjectSpace(typeof(DemoTask));
var tsk = os.FirstOrDefault<DemoTask>(t => t.Subject == "Task1");
var employee = os.CreateObject<Employee>();
employee.FirstName = "James";
tsk.AssignedTo = employee;
IRuleSet ruleSet = Validator.GetService(Application.ServiceProvider);
ruleSet.Validate(os, employee, DefaultContexts.Save);
os.CommitChanges();
};
}
}
For instance, to implement validation in the DefaultContexts.Save and DefaultContexts.Delete Contexts, the built-in PersistenceValidationController subscribes to the Committing and CustomDeleteObjects events of the current View Object Space. When an application raises these events, the Controller validates modified/deleted objects in these contexts. Refer to the Controller description for more information.
To validate data when Actions are executed, 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. As such, if you wish 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 the case of “broken” Rules.
The entire list of validation methods of IRuleSet are as follows:
ValidateRule
Checks whether or not an object satisfies a particular Rule. Returns an object of the RuleValidationResult type.
ValidateTarget
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’sIsValid
property is set totrue
. Otherwise, it is set tofalse
.ValidateAllTargets
Similar to the
ValidateTarget
method, except that several objects can be checked.Validate
Invokes the
ValidateTarget
method, and raises an exception if theIsValid
property of the returnedRuleSetValidationResult
object is set tofalse
.ValidateAll
Invokes the
ValidateAllTargets
method, and raises an exception if theIsValid
property of the returnedRuleSetValidationResult
object is set tofalse
.
These methods should be used from Controllers and Object Space events, and not within the persistent class scope (which takes the Object Space instance as a parameter). To access the IRuleSet object, use the Validator.GetService(IServiceProvider) method.
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.