views:

28

answers:

1

Newbie question, I have a simple Silverlight 4 client. I'm creating some methods on the server side to handle new user creation / validation. Before I create the user, I'd like to validate a number of things. Does the user name already exist? Is the email address valid? Does the password pass strictness checking? Etc, etc.

I'm not really finding a clear way of handling this and I'm looking for some suggestions. I have multiple calls that I need to make to the server for each condition I want to check for. All calls are asynchronous. The way I see it I can do the following:

  1. Call each validation function on the server and do mock synchronous calls using waits. ew.
  2. Try and stitch all of the various callback functions completed events. Ew.
  3. Combine all of the validation calls into one on the server side. Seems clean now, but what happens when I want to perform different validations or different combinations of validations? That means more new functions. Seems like it could become a maintenance headache down the road.

Is there an elegant solution that is evading me?

Thank you.

A: 

You can validate some simple things (like whether the field fits a certain regex, or if the password is long enough) by adding attributes to your properties in the generated .metadata class that domain services usually generate if you let them.

As for business logic related things (like if the password already exists etc), I'd personally go with choice 1.

There's nothing wrong with that approach really... seems far more maintainable than your third option, on the long run. You'd only need to change which server side methods you call in your viewmodel, instead of changing server-side code. Maybe at some point you'll have different validation rules for certain types of users? You need to think about that.

Hypothetically, you could use reflection on the server and pass validation method names from the client, and let the server figure out what needs to be done based on method names. Seems like a decent option, but is probably more convoluted than others.

EDIT: If you want to turn async calls into sync calls it's better to use something like:

Context.Load(Context.GetUsersQuery()).Completed += (sender, args) =>
{
   //Context.Load... next call
};

, instead of waiting arbitrary amounts of time.

VexXtreme
Thanks for your answer. Do you have any recommendations on how to perform mock synchronous calls? Sample code, perhaps?
billb
Yep, I've just edited my answer and added some sample code :)
VexXtreme
Awesome! Thanks!
billb