IRuleSet Interface
Specifies a set of Validation Rules that can be checked against an object.
Namespace: DevExpress.Persistent.Validation
Assembly: DevExpress.Persistent.Base.v24.1.dll
NuGet Package: DevExpress.Persistent.Base
Declaration
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 anIRuleSet
: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; } }