springservicespring-mvcqueryingstereotype

Should I create multiple services (using the Spring `@Service` stereotype) to do database lookups for different controllers?


I'm not sure where to do database lookups for Spring controllers.

It seems to make sense to use the Spring @Service stereotype and create multiple "services" to provide lookup support to controllers rather than doing lookups directly in the controllers.

Is this correct or is there a more appropriate place to perform database lookups?


Solution

  • I don't think controllers should be doing database lookups. If you ditch your web tier, the lookup functionality goes away.

    I'd put those behind a layer of repository interfaces. You can test them without the web tier that way. There could also be a separate service layer that owned units of work and transactions. Lookups should be read-only, so they might be safe enough to call directly from controllers.

    That's the recommended Spring idiom.