Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should repository layer always be encapsulated by a service layer?

In the repository layer I define normal data operations such as inserting, finding and so on. I am using Spring and I have the @Repository annotation above the class. Is it bad to use this class directly in the @Controller class for instance? Should all repositories always have a service layer that just delegate to the repository layer?

like image 411
LuckyLuke Avatar asked Oct 22 '22 12:10

LuckyLuke


2 Answers

It totally depends on your choice. In Spring Roo, you don't just skip the Repository or Service layer, but use a Rich Domain Model where you have the data access logic in the domain itself. Frameworks like Groovy on Grails use a single Repository layer. So i think its OK to use it directly in the Controller.

like image 121
amit bakle Avatar answered Nov 03 '22 21:11

amit bakle


I similarly asked what use are EJBs? An answer said this:

your [service layer] typically orchestrates application of business logic but more often than not does not actually perform it, it is usually a very thin wrapper.

The service-layer methods can define the operations that are transactions, and which therefore have annotations to get AOP transaction support added automatically for you. See also an answer to a related question, which says:

Spring's idiom would recommend having a service interface that knows about units of work and a persistence interface that deals with relational databases. The methods in the service interface should map closely to your use cases. The service implementation knows about all the model and persistence packages and classes it needs to accomplish the goals of the use case.

like image 26
Raedwald Avatar answered Nov 03 '22 22:11

Raedwald