Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsibilities of Service and Repository layers

Just trying to get my head round the responsibilities of the service layer and repository layer when saving an object to my persistence store.

My current under standing is this:

In my controller I have created a "Note" object from the data submitted by the user (from the form). The user then calls "Save" on the "NoteService" (which is there via dependency injection).

Within the "Save" method on the "NoteService" I carry out my business logic validation and then pass the "Note" object to the "Save" method of the "NoteRepository".

The "Save" method of the "NoteRepository" then checks to see if there is an existing primary key on this object and if so then get's that object from the db and updates it's properties using the "Note" object passed through and it's then saved back to the db. If there is no primary key then the object is simply saved to the db and the then returned to the service with it's newly created primary key.

like image 735
Gaz Avatar asked Jun 14 '10 13:06

Gaz


People also ask

Which of the following is the responsibility of the Service Layer?

The service implementations that provide the data logic have three major responsibilities: to provide access to the persistent data of the business, to support data composition of the business, and to provide their own sub-architecture for managing the flow of data across the organization.

What does repository layer do?

Repositories are classes or components that encapsulate the logic required to access data sources. They centralize common data access functionality, providing better maintainability and decoupling the infrastructure or technology used to access databases from the domain model layer.

Is repository a Service Layer?

Repository layer is implemented to access the database and helps to extend the CRUD operations on the database. Whereas a service layer consists of the business logic of the application and may use the repository layer to implement certain logic involving the database.


1 Answers

Your separation of concerns sounds pretty good to me. We follow the same pattern, but tend to add one more layer right about the repository layer. We call it the domain layer and perform all our business logic in there. Our service layer is just a pass through to our domain in case we need to publish any of our services to an ESB in the future.

The biggest benefit of what you are doing is not cluttering all the business and DB logic in the controller which a lot of people tend to do. You always want your controllers to be as light as possible if you really want to follow MVC.

like image 85
amurra Avatar answered Sep 29 '22 17:09

amurra