0

I'm wondering what's the best method for validating a view field value to be unique in an entityset: before or after the update to persistence layer? The involved db field has an unique constraint, and its table is mapped to an EF model. I see two ways for unique value validation in an entityset:

  1. before saving changes to db (during model update or by decorating with custom DataAnnotations the model)
  2. after saving changes to db (by handling in the repository or controller the UpdateException generated by the persistence layer)

With the 1st method I need to query the db for checking the uniqueness, so any view update will require both a db select and a db update.

With the 2nd method, the additional select is not required, but it is difficult to identify the error type and the offending field.

I would prefer method 2, but the problem for determining if the insert/update failed due to a unique constraint force me to choose method 1.

Or is there another way?

fabri
  • 1

1 Answers1

0

The preferred and recommended way for checking unique constraint is from UI by custom DataAnnotation attribute. with this method you have to write a little code but this is what all the sites have been doing for checking uniqueness constraint. asp.net mvc 3 however provides RemoteAttribute out of the box to check uniqueness constraint. i would recommend using first method because some tiny ajax calls won't make noticeable effect on performance provided that you have organized it in a good manner.

Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155
  • Thank you. I will go for method 1. Although I do validation both on client and server side, I will run the uniqueness check only on server side, without using ajax calls from javascript. – fabri May 04 '11 at 14:48