views:

20

answers:

1

In the project I am working with, there is a UserDetailServiceImpl class which carries out all of the user-related database lookups.

The problem is, I have gotten into the bad habit of doing non-user-related database lookups there too and using the UserDetailServiceImpl class as a default lookup service to avoid doing database lookups directly in the controller action methods. The advantage of this is that I don't have to create a separate lookup service for each of my models - which would seem rather silly and redundant. But is there a more standard approach to database lookups that I should consider to avoid angering the Spring gods?

+1  A: 

I don't like using services just to wrap database lookups, you end up with all of the business logic in your controller. I would organize my services so there would be one per type of user. So if I had customers and data-entry people and admins, I would make a customerService and a dataEntryService and an adminService. Each service would expose the methods that that kind of customer needs. No business logic should be in a controller, it is strictly concerned with getting inputs from the url and request and the session, calling a method on some service (passing in those inputs), then taking the result of the service call and putting that where the page can display it.

Nathan Hughes
Well, let's say you needed to run reports tallying up orders placed by your customer base. Would you also do the lookups for those reports in the "customer service" even if they are not specific to individual customers? Or would you create a separate "reporting service" to do those lookups and let the "customer service" focus on queries for individual customers?
Chris Collins
@Chris Collins: I'd organize it by who's doing stuff, not who it's about, so which service it would go in would depend on who is generating the report. The 'customer service' would focus on providing things that individual customers can do.
Nathan Hughes