Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Ricardo Sala
Ricardo Sala
16,211 Points

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.