title: Controller, Service, Repository and simple delegation methods
body: <p>I have a Java EE application using standard stack of spring/hibernate/jsp and typical layers:</p>
  
  <ul>
  <li>@Repository (or DAO)</li>
  <li>@Service</li>
  <li>@Controller</li>
  </ul>
  
  <p>Each repository has a number of find... methods (e.g. for a BookRepository: simple findById, findByTitle, findByAuthor, more complex findMostUsed, findMostCommented etc.)</p>
  
  <p>Service layer contains the business logic calling repositories as it supposed to be.</p>
  
  <p>Controllers call service methods and populate ModelAndView to be used in JSP.</p>
  
  <p>Of course, there are methods with some complex business logic in services. But it is annoying that there are a lot of stupid methods like this:</p>
  
  <pre><code>public List&lt;Book&gt; findMostUsed() {
      return repository.findMostUsed();
  }
  public List&lt;Book&gt; findMostCommented (boolean includeRating) {
      return repository.findMostCommented(includeRating);
  }
  ...
  </code></pre>
  
  <p>So they are just a simple delegation (there is no business logic in these find methods - a DB query in the repository does all the selecting and grouping).</p>
  
  <p>If I need to change a method in the repository the service needs to be changed as well. After 2 years of developing there was no need to add any logic to those methods - only queries and its parameters were changed.</p>
  
  <p>I saw even worse design where people created Controller -> Facade -> Service -> Repository -> DAO with every layer teeming with methods like this.</p>
  
  <p><em>What would be the better design?</em></p>
  
  <p>Maybe to remove all those methods and transform service to be more generic (such as BookPricingService, BookRatingService, etc. instead of single BookService (which violates single responsibility principle btw)? Then the Controller will call both Service and Repository layers which is not good.</p>
  
  <p>Maybe to make find methods more generic such as findByCriteria(criteria) to reduce their number. But the problem is that the queries are so different and this will end with a kind of switch-case block that picks the correct query/parameters based on the criteria type. Btw, Spring-Data also recommends one method per query with @Query annotation.</p>
  
  <p>Maybe this is just the price we should pay to have the abstraction layer?</p>