Do you do input parameter validation in Resource class or Service Class?

I am creating a REST web service, such that the URL is as below: /users/{userId}/images/{imageId}

I would have to do the following validations, before I do anything else: 1. Validate userId is in proper format. Ex: integer and not a string etc 2. Validate imageId is in proper format. Ex: integer and not a string etc 3. Validate if userId exists in the database. 4. Validate if imageId exists in the database.

I have two classes (on server side). One class is the "Resource" class and this class gets the first call (control) from the client application. Then this class sends the inputs to the "Service" class. The service class takes care of the business logic.

Now which class should perform the four validations mentioned above?

Option 1: Perform validations: 'Validation 1' (above) and 'Validation 2' at the 'resource' class and perform 'Validation 3' and 'Validation 4' at the "service" class.

Option 2: Perform all validation in the "Service" class.

Which option is the BEST approach to validate the inputs. Please let me know which option is better.

Answers


The purpose of validation is to reduce the functionality calling with invalid data. So Option 1 would be the better. If Userid and image is not in a correct format dont call the further functionality.


Need Your Help

Images for retina display iphone

iphone xcode interface-builder iphone-4 retina-display

I have an working application which i have tested in my ipod touch. Everything works pretty fine.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.