Print Page | Close Window

Can Verifiers be Asynchronous?

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2302
Printed Date: 31-Oct-2024 at 10:00pm


Topic: Can Verifiers be Asynchronous?
Posted By: mjwolfe
Subject: Can Verifiers be Asynchronous?
Date Posted: 12-Nov-2010 at 2:51pm
Does the DevForce 2010 Verification Engine support one of my verifiers executing code that will cause a request to the server that would use a callback when the server-side call returns?
 
The purpose for this would be to get a stored procedure to run that can determine the validity of an instance of my entity based on an operation against a bunch of data in the database. It's too much data to bring it all client-side, but I don't want to wait to fail the save on the server to give the user feedback that I could have told them while they were entering the data on the client.
 
If it's not supported directly, could I implement my own class that holds the results of asynchronous verifiers returned from the server, and then have the callback re-fire the verifier on the associated property and have this new execution of the verifier pick up the results of the previous asynchronous execution to return this time instead of executing the asynchronous method a second time?
 
Thanks.
 



Replies:
Posted By: ting
Date Posted: 15-Nov-2010 at 7:52pm
We don't yet support asynchronous verification, but this is something we're looking at.

The implementation you propose in the last paragraph should work.  Alternatively, after performing your async validation (e.g. through InvokeServerMethod), you could add the validation error directly to the Entity.EntityAspect.ValidationErrors collection and it will be reflected in the UI automatically. In DevForce 6.0.7 (early December) we will support INotifyDataErrorInfo as well as IDataErrorInfo so this should work for both WPF and Silverlight.



Posted By: as123
Date Posted: 13-Dec-2010 at 10:26am

           Can you please elaborate on the part "you could add the validation error directly to the Entity.EntityAspect.ValidatonErrors collection"? When we add a ‘VerifierResult’ to the ValidatorErrors collection from the callback method specified as part of the InvokeServerMethodAsync call, how to specify the context (targetinstance and triggercontext)? I do not see a VerifierResult constructor taking these properties. Can you please give an example?

 

Thanks

 



Posted By: DenisK
Date Posted: 14-Dec-2010 at 3:17pm
as123;

The VerifierResult, in this context, is only used to automatically display the errors on the UI. The context is not important in this regard. Maybe I could give a better advice if you can tell me what you're trying to achieve with the TargetInstance and TriggerContext.


Posted By: as123
Date Posted: 14-Dec-2010 at 3:43pm

         For example, let us say Async Check Unique validators are defined for ID and Name properties on Employee. When the Check Unique validation for ID fails, the callback specified will know the context, but how can this context information be passed to the VerifierEngine so that the ID property can be marked as in error (red box around) and show the error message when hovering over the box (as done by InotifyDataErrorInfo).

           In the callback method, if I just create a verifier result how will the Validation Engine know that the error is for ID and not for Name? I was thinking that in this case Target needs to be set to “ID” in the VerifierResult so that it can be marked as in error. Can you please clarify?

 Thanks

 Sample pseudo-code:


ValidatorMethod (…)

{

        …

        /* Async Query to check for duplicate values in the database */

                   EntityManager.ExecuteQueryAsync(query, userCallback, userState)

        …

}

userCallback(…)

{

            …

            _verifierResult = new VerifierResult(_result, message);            

            _currInstance.EntityAspect. ValidationErrors.Add(_verifierResult);

            …

}



Posted By: DenisK
Date Posted: 15-Dec-2010 at 5:05pm
as123;

One of the constructors of a VerifierResult is

VerifierResult(VerifierResultCode code , string message, params string[] propertyNames)

So by passing the propertyName, you can trigger, for example, a DataGrid's BindingValidationError event which you can customize to show the error red box.

I have attached an example below.

http://www.ideablade.com/friends/SilverlightValidationSample.zip - www.ideablade.com/friends/SilverlightValidationSample.zip


Posted By: as123
Date Posted: 15-Dec-2010 at 5:47pm
    Thanks. I was looking into an earlier version of DF and this constructor was not there. It is available in 6.0.7.


Posted By: as123
Date Posted: 29-Dec-2010 at 10:34pm

           I tried this solution and it works with adding validation results to the collection. 

           However, if the same error occurs again for a different value for the same property (after the initial error), the error is added again to the validation summary control unlike the Verifier Engine behavior, which is only one entry in the Validation Summary even if the same verifier error occurs for the same property multiple times.  Do we need to have custom logic to avoid getting duplicate messages?

On a similar note, how do we remove the message after the Async call succeeds? If I try to remove the validation error by calling EntityAspect.ValidationErrors.Remove(), the message is removed from the collection, but the property still shows as in error and the ValidationSummary also shows the error.  Do we need to do anything else apart from removing the validation result from the ValidationErrors collection?

Thanks.



Posted By: DenisK
Date Posted: 30-Dec-2010 at 12:25pm
as123;

Yes, it looks like you need to add custom logic to avoid duplicate messages for this.

In regards to removing the errors, you will need to call ValidationErrors.Clear() as well as removing the errors from the ValidationSummary manually.


Posted By: as123
Date Posted: 30-Dec-2010 at 1:21pm
          Regarding the removal of errors, I believe you meant ValidationErrors.Remove(), and NOT ValidationErrors.Clear() as I am only trying to clear a specific error.
 
          You have mentioned about removing the entry from ValidationSummary manually, but how about the red box around the property in error (result of INotifyDataErrorInfo)?  As part of the removing the validation error entry from the ValidationErrors collection, the error notification (red box) does not go away. Do I need to do anything else to fire that event so that the property is no longer in error after removing the entry from ValidationSummary?
 
          It was mentioned earlier that IdeaBlade is looking into Async Verification. Is there a timeframe by which we can expect this?
 
          If there is a better workaround to handle all these situations for Async Verification in SilverLight, please let me know.
 
Thanks


Posted By: DenisK
Date Posted: 30-Dec-2010 at 4:09pm
as123;

You're right, it shouldn't be this complicated to handle Async Verification. I need to do more research on how this works with DevForce and get back to you. I will find out about the timeframe as well.

Meanwhile, here's a link which you might find useful.

http://www.silverlight.net/learn/videos/silverlight-4-videos/asynchronous-data-validation/ - http://www.silverlight.net/learn/videos/silverlight-4-videos/asynchronous-data-validation/


Posted By: as123
Date Posted: 30-Dec-2010 at 4:14pm
           Thanks for looking into it.


Posted By: DenisK
Date Posted: 10-Jan-2011 at 11:49am
Here's a sample app that should demonstrate at a basic level how to do async validation with DevForce.

http://www.ideablade.com/friends/SilverlightValidationSample.zip - www.ideablade.com/friends/SilverlightValidationSample.zip


Posted By: as123
Date Posted: 10-Jan-2011 at 12:40pm

          Thanks for the latest sample.

 

          If I make a Company Name error by entering @ as part of the name, it shows the error correctly, but as mentioned earlier the error is duplicated if I tab back to the same field. After correcting the error, the user is expected to come back to the filed twice to clear all the existing errors. Do you still think it is the problem with DataGrid/INotifyDataErrorInfo? If so, do you know why we do not see such a behavior when doing Synchronous Validations?

 

         Also, the ValidationError is removed by checking for mailto:@ -  

          For example, if we have validations to make sure that Name and ID must be unique (checked async), the messages might be just 'Must be Unique'  or 'ID must be unique.' or 'Description must be unique.'. It might be difficult to remove such messages. Another alternative might be to regenerate the same message and look for its existence and remove it. However, this won’t work if we have generic messages like ‘Must be unique’ and the context is added dynamically as part of displaying error summary.

 

          Thanks again for your help.



Posted By: DenisK
Date Posted: 11-Jan-2011 at 5:37pm
as123;

The error message duplication is indeed strange. I have verified that both INotifyDataErrorInfo error collection and the ValidationError collection only has 1 error count each. And when you remove the VerifierResult, it removes both message as well. This is the exact same thing that happens with Synchronous Validations. It looks like I need to dig deeper on this.

Regarding the user expected to come back to the field twice, this actually looks more of a UI issue in that if you focus on any other field, the error will go away. You don't have to come back to that same field twice to clear the errors.

As for your ValidationError removal question, I just used the Message property as an example. You can use any other property you like to search for the VerifierResult in question. For example, PropertyNames can be used as well.

var propertyName = Customer.PropertyMetadata.CompanyName.Name;          
var resultToRemove = validationErrors
       .Where(v => v.PropertyNames.Contains(propertyName))
       .FirstOrDefault();

 


Posted By: DenisK
Date Posted: 12-Jan-2011 at 4:23pm
as123;

I was finally able to find, what I think, is the root cause of the duplicate message. 

When adding a free form VerifierResult, the TriggerLinks are not being handled properly and due to our implementation of INotifyDataErrorInfo.GetErrors(String.Empty), it causes the duplicate message. This combines with the fact that TriggerLinks is not serializable. So for now the quick fix for this is to add the TriggerLink manually inside the server call back method.

private void OnServerMethodCompleted(InvokeServerMethodOperation args) {

...........................

var vr = (VerifierResult)args.Result;
        vr.Verifier.AddTrigger(Customer.PropertyMetadata.CompanyName.Name);

if (vr.IsError) { validationErrors.Add(vr);

.........................
  }

..........................
}

I will file a feature and/or defect request for this.




Posted By: as123
Date Posted: 11-Feb-2011 at 11:09am
           Thanks. For some reason, I did not get the e-mail notification for the last one, and I just saw it. Do you know when will this feature be fixed?
 
Thanks.


Posted By: DenisK
Date Posted: 11-Feb-2011 at 11:35am
Hi as123;

It's fixed as of 6.0.8.


Posted By: katit
Date Posted: 28-Sep-2011 at 9:05pm
Denis,
 
Is there update sample with Async validation in DevForce? Sample code you posted already removed.
 
Thanks!


Posted By: DenisK
Date Posted: 10-Oct-2011 at 11:28am
Hi katit;

Sorry for the delayed reply. I just got back from vacation. 

Please correct me if I'm wrong but it looks like you've already figured things out in this post.  http://www.ideablade.com/forum/forum_posts.asp?TID=2985&title=my-async-verifier-implementation - http://www.ideablade.com/forum/forum_posts.asp?TID=2985&title=my-async-verifier-implementation .

Let me know if you still want to see my sample. I might have to search for it.


Posted By: katit
Date Posted: 10-Oct-2011 at 11:40am
Denis,
 
Yes, I figured it out. If it's not too hard - I would still like to see your example as well. I'm learning DevForce in's and out's so it would useful to see how you did it.
 
Thanks!



Print Page | Close Window