0

I have a general question on where to put validation.

I have an asp.net mvc that gets/sets data from/to a DataSet. Currently, the fields in the form are validated by the form itself (e.g. for invalid length, range, etc.). Is it a good or better idea to transfer these validation checks into the DataSet? The downside is I need to trigger update calls to the DataSet in order to get the column with errors. In using forms, I can catch the error earlier.

Or should I use validation for both of them?

For additional consideration - it's possible I will be re-using this Dataset assembly with another project (a WCF service?). And I'd like to re-use the same validation code when possible.

1

You should consider validating the inputs on the form at both the UI layer as well as at the service layer.

Validating at the UI layer means your application will appear more responsive and can instantly provide feedback to the user on what's missing or what's wrong. Validating at the UI avoids a network call, and makes it simpler to identify what's wrong because you don't have to dig into an object and find a list of errors to report.

Validating at the service layer is required since your service shouldn't blindly trust what's being passed to it by a client. If you only did one, you must validate at your service layer.

Unfortunately, validating at both layers means you'll likely duplicate some of the validation code. But that's the trade-off you have to make in order to gain a responsive UI with quick problem notification as well as having a secure service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.