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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Sergei Makarov](https://secure.gravatar.com/avatar/ae9951d9ab2ad1a2480508fad8b2aa17?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Sergei Makarov
13,839 PointsUnderstanding 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](https://uploads.teamtreehouse.com/production/profile-photos/5494222/micro_IMG_0409.jpg)
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsThere 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.
Sergei Makarov
13,839 PointsSergei Makarov
13,839 Pointsthank you!