Check preconditions in Controller or Service layer

controller, service, spring, validation

Solution

Ideally you would do it in both places. But you are confusing two different things:

- Validation (with error handling)

- Defensivie Programming (aka assertions, aka design by contract).

You absolutely should do validation in the controller and defensive programming in your service. And here is why.

You need to validate for forms and REST requests so that you can send a sensible error back to the client. This includes what fields are bad and then doing localization of the error messages, etc... (your current example would send me a horrible 500 error message with a stack trace if ProductInfo.name property was null).

Spring has a solution for validating objects in the controller.

Defensive programming is done in the service layer BUT NOT validation because you don't have access to locale to generate proper error messages. Some people do but Spring doesn't really help you there.

The other reason why validation is not done in the service layer is that the ORM already typically does this through the JSR Bean Validation spec (hibernate) but it doesn't generate sensible error messages.

One strategy people do is to create their own preconditions utils library that throws custom derived `RuntimeException`s instead of guava's (and commons lang) `IllegalArgumentException` and `IllegalStateException` and then `try`...`catch` the exceptions in the controller converting them to validation error messages.

Problem

I'm using Google's Preconditions class to validate user's input data. But I'm worried about where is the best point of checking user's input data using Preconditions class. First, I wrote validation check code in Controller like below: ``` @Controller ... public void register(ProductInfo data) { Preconditions.checkArgument(StringUtils.hasText(data.getName()), "Empty name parameter."); productService.register(data); } @Service ... public void register(ProductInfo data) { productDao.register(data); } ``` But I thought that `register` method in Service layer would be using another Controller method like below: ``` @Controller ... public void register(ProductInfo data) { productService.register(data); } public void anotherRegister(ProductInfo data) { productService.register(data); } @Service ... public void register(ProductInfo data) { Preconditions.checkArgument(StringUtils.hasText(data.getName()), "Empty name parameter."); productDao.register(data); } ``` On the other hand, the method of service layer would be used in just one controller. I was confused. Which is the better way of checking preconditions in controller or service? Thanks in advance.

Original source