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!

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 Software Design Principles

DAO And Service Layer -why do we need both?

Actually I have understood there are a few consideration such as Unitet for only the DB part or controller API which we want to expose to another device which doesnt include the web part and when we want to expose our queries to be checked so because all the design priciples you mentioned this so be separetely... But in our case is it really necessary(I thought that in the category examlple shown in the video) there is no need to do this -Because there is a seprartion of concerns and all the stuff above .Can you demonostrate an example which this DAO and service are both needed?Thanks:)

Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

Sorry Racheli, I am not familiar enough with Spring to know why you use both DAO and Service Layer. Hope you find the answer!

1 Answer

Venkatesh Gangisetti
Venkatesh Gangisetti
4,741 Points

DAO as defined only used to get results from a data source and send it back to the service layer, so this should have code only to do CRUD operations nothing more nothing less. Service Layer exists because you want to free controller from most of the responsibilities like formatting, doing additional calculations on data set etc.. so that you can change your entry points, and can reuse the business logic across multiple controllers. Suppose you have two controllers one returns the data in JSON format and another uses a template and send an html web page, now both can use the same service layer class.

https://softwareengineering.stackexchange.com/questions/162399/how-essential-is-it-to-make-a-service-layer https://softwareengineering.stackexchange.com/questions/162399/how-essential-is-it-to-make-a-service-layer