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 Writing Your First Service

Sergei Makarov
Sergei Makarov
13,839 Points

Understanding Service Layer within context of Separation of Concerns.

Hello,

just one question: Why cannot be used DAOImpl methods in our Controller instead of using ServiceImpl methods.

Me seems it a little bit redundant, when we use Service, which stays between DAO-Interface and Controller-class. Would it be not the same principle of Separation of Concerns, if we use DAOImpl direct in our Controller, cause we also using the same time Interface and it seems in my opinion to be loose coupled?

Thank you!

1 Answer

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

There are a lot of questions about service layer. But sometimes it can be very useful, take a look at this discussion on StackOverflow: e.g http://stackoverflow.com/questions/3688664/simple-spring-app-why-use-service-layer I liked the answer a lot.

You probably noticed how in 'Spring With Hibernate' - gif is converted to bytes array on service layer. And it feels right. Because on DAO layer all you need is simple database operations: Create, Read, Delete, Update, findAll, findById, filter and etc.

I can tell you where I used service layer: For example you have database with some data, and you want to calculate correlation coefficient or calculate average, or other statistics of your data. How do you split it:

  • On DAO layer you get data needed: findAll;
  • On service layer you calculate the statistics.

This separation comes natural when you start solving problems or implementing new features. It is all about order. Programmer want to perfect Single Responsibility Principle. He is trying to achieve perfect clearness, to give database operations to this layer and data calculations to other layer, for example.

Another good reason to separate layers. As you will see, or may be know in 'Spring Unit Testing' course you will be introduced to CRUD repositories: what they do - very simple. They implement all simple CRUD operations for you: you just type public interface FavoriteDao extends CrudRepository<Favorite,Long> and then boom - you can use methods like findAll, save and many others without implementation.

Why it is possible? because you separated DAO from Service. If you did not do that, then you couldn't use these features.

And the last but not the least: think about DAO as a Rock Solid: you have: findAll, findById, save, delete, and etc... These methods are super common for everyone. Sometimes there is no need to change them.

Think about the Service Layer as Instrument to make these Rock Dao layer operations being in the shape that you need.