Search This Blog

Tuesday, June 20, 2017

Performance tuning of the saving/validation process using the PersistenceValidationController events

Regardless of how good and sophisticated your development tools are, real production use often imposes research and solving speed issues. In this tip, I will talk about tuning the Validation Module to do its best when saving a master object exposing  large collections of aggregated data. 

By default, aggregated objects are considered to be an integral part of a master object, and thus, they should be validated together with the master object, which may take significant time if you have hundreds or thousands of complex detail records. The entire detail collection will be loaded and validated in this instance.

The good news is that XAF is very flexible and has a customizable framework that provides many ways to intercept this validation process. 
Validation is organized in the UI with the PersistenceValidationController that manages the adjustment of rules. Thus, it is possible to specify whether objects with a particular property value need to be validated, modify a collection of aggregated objects or a collection of all objects to be validated in a particular context. For instance, you can use its CustomGetAggregatedObjectsToValidate

 event to change this behavior and cancel validating the aggregated objects that are not updated. You can use the IObjectSpace.IsObjectToSave method to ensure that a certain object is not modified.

void CustomGetAggregatedObjectsToValidate(object sender, CustomGetAggregatedObjectsToValidateEventArgs e) {
    if (e.OwnerObject is Contact) {
        foreach (PhoneNumber number in ((Contact)e.OwnerObject).PhoneNumbers) {
            if (ObjectSpace.IsObjectToSave(number)) {
                e.AggregatedObjects.Add(number);
            }
        }
    }
    e.Handled = true;
}

The drawback of this approach is that invalid aggregated objects that were added before the rule is introduced, or created in the database directly, will be skipped.

The PersistenceValidationController exposes two more events (ContextValidating and NeedToValidateObject) + see RuleSet Members that allow you to customize the default validation process further, so check it out if you need total control over this.



I am looking forward to hearing from you on whether you are already doing something similar or are going to try it soon. Here I also want to remind you that before trying this and other solutions, it is best to diagnose and understand the nature of the performance problem experienced by performing several known diagnostics (e.g., SQL queries number and duration, profiling .NET methods execution). After that, the solution will come soon. We collect useful information in this regard in the How to measure and improve the application's performance article, so check it too. Thanks.


FreeImages.Com/Luca Baroncini

No comments:

Post a Comment