Where Best to validate data?

Discussions

General J2EE: Where Best to validate data?

  1. Where Best to validate data? (2 messages)

    Hello.

    I am building a "typical" J2EE appplication using a layered architecture consisting of:

    1) Data domain layer with data access object
    2) Business objects layer
    3) MVC layer using Servlets and JSPS, and Data Transfer objects to communicate with the business layer.

    I want to be able to validate data inputted by users on web screens. Now, this validation obviously has to be performed in the MVC layer. But surely, it is normal to define validations within the data domain objects also.


    a) What is the best design approach for validating data, in such a way that it is re-usable and not have to be maintained seperately in 2 places?

    Confused

    Paddy

    Threaded Messages (2)

  2. Where Best to validate data?[ Go to top ]

    There is, unfortunately, no best answer to this question. The various locations have different advantages and disadvantages.

    1) Client-side validation with JavaScript give the user the most immediate feedback and the best response, but only has access to information within the page itself.

    2) Validation in the Java layer (DAO, Business Object or your MVC controller) has access to all of the server's information, and the best language (Java), but is Java specific.

    3) Constraints in the database are language-independent validation logic, but give terrible error messages.

    Put your validation logic in the best place for the kind of logic you are performing. Often this means duplicate validation logic in the various layers. While this may seem painful, think of it this way: each layer (client, server, database) is really a separate application. Therefore, for the most robust possible application, each layer should be responsible for its own validation.

    In practice, many people ignore this rule, and only validate in one place. Also, people various code-generation strategies to define your validation logic in one place, and the generate the validation logic for each layer (at compile-time or even run-time).
  3. Use Wrapper Class[ Go to top ]

    Hi
    I agree with paul,if it is a cleint side validation then best practice is to use Javascript,but to keep consistency regaring message text.use generic method to do simple validation,by passing some parameters.
    Same in case of Data Side validation also,that we need to do on Data Laye/on Business Layes ,for this also better use Wrapper class which defines all the of error messages.

    Thanks
    Manish