views:

18

answers:

1

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?

+2  A: 

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.

duffymo
Thanks. This makes sense, especially the bit about "what happens if you get rid of the web tier" - I hadn't thought about that possibility. But could you explain what you mean by "repository interfaces" - I assume this is the "that" in "That's the recommended Spring idiom"? Are there any recommendations about what the "separate service layer" should look like and when you say "separate", do you mean it is "separate" from the "layer of repository interfaces"?
Cornish
"Repository" == "Data Access Object" == "DAO" It's an interface for CRUD operations. And yes, there's another interface for services that orchestrate DAOs and model objects to implement use cases.
duffymo