Skip to main content
All docs
V24.2
.NET Framework 4.6.2+

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

IRuleSet Interface

Specifies a set of Validation Rules that can be checked against an object.

Namespace: DevExpress.Persistent.Validation

Assembly: DevExpress.Persistent.Base.v24.2.dll

#Declaration

public interface IRuleSet

The following members return IRuleSet objects:

#Remarks

Validation is based on rules declared in business class code and in the Validation node of the Application Model. Internally, these rules are automatically collected in the RegisteredRules property of an IRuleSet object. The rules are checked automatically when objects are saved or deleted and when an Action with the appropriate validation context is executed.

If you need to modify the default behavior, you can use one of the following techniques to access an object that implements the IRuleSet interface (both examples demonstrate how to trigger validation against a specific object and context, and then handle the result):

  • Use Dependency Injection to access the IValidator service. Use the injected service’s RuleSet property to access an IRuleSet:

    File: MySolution.Module.Controllers.MyController.cs

    using DevExpress.Persistent.Validation;
    using Microsoft.Extensions.DependencyInjection;
    // ...
    namespace MySolution.Module.Controllers;
    public partial class MyController : ViewController {
        IServiceProvider serviceProvider;
    
        public MyController() { }
    
        [ActivatorUtilitiesConstructor]
        public MyController(IServiceProvider serviceProvider) : this() {
            InitializeComponent();
            this.serviceProvider = serviceProvider;
        }
    
        protected override void OnActivated() {
            base.OnActivated();
            RuleSetValidationResult result = serviceProvider.GetRequiredService<IValidator>()
                .RuleSet.ValidateTarget(View.ObjectSpace, View.CurrentObject, "MyContext");
            if (result.ValidationOutcome > ValidationOutcome.Information)
                ((Contact)View.CurrentObject).Notes += "[Validation Error] " + result.Results[0].ErrorMessage;
        }
    }
    
  • Call the static Validator.GetService method. This method takes an IServiceProvider instance as a parameter. In a context where XafApplication is available (for example, in a View Controller), you can use this method as follows:

    File: MySolution.Module.Controllers.MyController.cs

    using DevExpress.Persistent.Validation;
    // ...
    namespace MySolution.Module.Controllers;
    public partial class MyController : ViewController {
        // ...
        protected override void OnActivated() {
            base.OnActivated();
            RuleSetValidationResult result = Validator.GetService(Application.ServiceProvider).ValidateTarget(
                View.ObjectSpace, 
                View.CurrentObject, 
                "MyContext"
            );
            if (result.ValidationOutcome > ValidationOutcome.Information)
                ((Contact)View.CurrentObject).Notes += "[Validation Error] " + result.Results[0].ErrorMessage;
        }
    }
    
See Also