Back to Devexpress

Validator.GetService(IServiceProvider) Method

expressappframework-devexpress-dot-persistent-dot-validation-dot-validator-dot-getservice-x28-system-dot-iserviceprovider-x29.md

latest3.9 KB
Original Source

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.v25.2.dll

NuGet Package : DevExpress.Persistent.Base

Declaration

csharp
public static IRuleSet GetService(
    IServiceProvider serviceProvider
)
vb
Public Shared Function GetService(
    serviceProvider As IServiceProvider
) As IRuleSet

Parameters

NameTypeDescription
serviceProviderIServiceProvider

An object that implements the IServiceProvider interface.

|

Returns

TypeDescription
IRuleSet

An object that implements the IRuleSet interface.

|

Remarks

Use this method to access an object that implements the IRuleSet interface. Refer to the IRuleSet topic for more information.

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

csharp
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;
    }
}

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the GetService(IServiceProvider) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

xaf-how-to-highlight-invalid-properties-when-a-view-is-activated/CS/EFCore/ValidateHighlightEF/ValidateHighlightEF.Module/Controllers/ImmediateValidationController.cs#L55

csharp
if (resultsHighlightController != null) {
    IRuleSet ruleSet = Validator.GetService(Application.ServiceProvider);
    RuleSetValidationResult result = ruleSet.ValidateAllTargets(ObjectSpace, targets, DefaultContexts.Save);

See Also

Validator Class

Validator Members

DevExpress.Persistent.Validation Namespace