Skip to main content

Implement Custom Contexts

  • 4 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 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). To perform validation, you have to call the RuleSet.Validate method of the RuleSet object and pass the object to be checked and the Context caption as parameters. To obtain the RuleSet object, you can use the Validator.GetService(IServiceProvider serviceProvider) 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 provide 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 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:

  • 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’s IsValid property is set to true. Otherwise, it is set to false.

  • ValidateAllTargets

    Similar to the ValidateTarget method, except that several objects can be checked.

  • Validate

    Invokes the ValidateTarget method, and raises an exception if the IsValid property of the returned RuleSetValidationResult object is set to false.

  • ValidateAll

    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.GetService(IServiceProvider serviceProvider) 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.

See Also