Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Spring with Hibernate Data-Driven Application Design Making Service Calls from a Controller

Why do we need DAO AND service?

Being service only used to fetch the data...

Why cannot the user interact directly with the DAO?

I think it is because services are supposed to do something else (some computation?), but I am not sure if that is the only reason...

thanks!!!!

Binyamin Friedman
Binyamin Friedman
14,615 Points

The dao layer connects directly with the database and the service layer usually is used for additional processing that might need to be done to the data. Besides that, it makes testing a lot easier which you should see in later courses.

1 Answer

Like Binyamin said, the DAO communicates directly to the database - so your basic CRUD operations.

The Service is for preparing that data for the controller. For example, if you wanted to get a list of all Gifs and return just a list of Strings (perhaps just the descriptions), you would do that transformation in the Service.

This way, your controller can use the Service to get the data it needs to render the appropriate view. The Category example isn't that great because it is redundant.

Got it, thanks!