Validator.GetService(IServiceProvider) Method
Takes an IServiceProvider
instance as a parameter and returns an instance of IRuleSet.
Namespace: DevExpress.Persistent.Validation
Assembly: DevExpress.Persistent.Base.v24.1.dll
NuGet Package: DevExpress.Persistent.Base
Declaration
Parameters
Name | Type | Description |
---|---|---|
serviceProvider | IServiceProvider | An object that implements the |
Returns
Type | Description |
---|---|
IRuleSet | An object that implements the |
Remarks
Use this method to access an object that implements the IRuleSet
interface. Refer to the IRuleSet topic for more information.
The GetService
method can be used in both .NET applications and older .NET Framework applications to access IRuleSet
. In .NET Framework applications, this method accepts null
as the serviceProvider
parameter (the XafApplication.ServiceProvider property always returns null
under .NET Framework).
Example
The following code snippet demonstrates how to use the Validator.GetService
method to access an IRuleSet
instance and use it to trigger validation against a specific object and context, and then handle the result:
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;
}
}