Exception methodology to use for server-side validation?

Exception methodology to use for server-side validation?

Old forum URL: forums.lhotka.net/forums/t/5924.aspx


rsbaker0 posted on Tuesday, December 02, 2008

For those of you doing server-side validation by explicitly checking rules at object Save time, how do you handle the broken rules that are newly detected on the server?

For example, if you call CheckRules() on the server side of the data portal on a previous "valid" object (e.g. no broken rules), and now the object appears to be broken, what is the best way to communicate this back to the client? I've seen posts that reference rules associated with a pseudo-property to defer them until they are explicitly checked server-side, so I was mainly interested in this case.

You could throw a ValidationException, but since the object presumably was cloned before the save attempt, the original object still won't have any broken rules.

One possibility might be to just return the invalid object and not throw an exception, but I'd think the client would have to be expecting this possibility so I'm not sure about that approach.

 

 

JoeFallon1 replied on Wednesday, December 03, 2008

In my web app I use a code style like this for my btnSave code:

If myBO.IsValid Then

  'save BO  (optionally bypass the call to IsValid in the normal CSLA dataportal since we just did it.)

Else

  'show list of broken rules

End If

Joe

 

SonOfPirate replied on Wednesday, December 03, 2008

What would be the reason/benefit of only validating the rules on the server?

 

rsbaker0 replied on Wednesday, December 03, 2008

SonOfPirate:

What would be the reason/benefit of only validating the rules on the server?

 

I haven't actually done this yet -- I got this idea from other forum participants. One area in which this seemed to possibly make sense was for business rules that hit the database.

SonOfPirate replied on Wednesday, December 03, 2008

Ok, that makes sense then.

So, the problem is really because the broken rules are maintained outside of the BO (in the ValidationRules instance).  But, it is still part of the object's state and, I believe, it is serialized between the server and client along with the BO.  So, when the instance is returned from the server with the broken rules, you could replace the original BO's _validationRules variable with the instance returned from the server.  Then your original object would have the complete list of rules that were broken during validation on the server.

Unfortunately, this would have to be done in Core.BusinessBase which has access to the private _validationRules variable or some other work-around will have to be found.

 

Copyright (c) Marimer LLC