Lesson learnt (1) — Upfront validation

Tomasz Dłużniewski
1 min readJun 9, 2021
Photo by Jon Tyson on Unsplash

Today one of my workmates ask me why verification of incoming parameters should be upfront at controller or service layer and not at data access layer. There are a few reasons :

  1. You shouldn’t let your process to go so far. It’s a waste of resources. I can easily imagine a process which makes a lot of things before accessing the method that will check parameters. You should do it on the beginning of this process.
  2. Separation of concerns. The controller is responsible for incoming parameters checking. Sometimes additional verification has to be done on the service layer but your database access layer should know nothing about it.
  3. Incorrect unit tests. When you write unit tests for a service layer and have validation at data access layer it’s possible that your test will end with a success but it shouldn’t. It’s because your data access methods are mocked and do not provide validation. One may think that you can add validation to service layer too, but then you will have unnecessary code duplication.

--

--

Tomasz Dłużniewski

Software engineer, architect, expert in database and java performance optimization. I believe in companies based on gratitude and teamwork (see Simon Sinek).