Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between repository and service?

What's the difference between a repository and a service? I don't seem to grasp it.

I'm talking about data access through a data access layer, typically with linq to sql.

Very often i see repositories with simple CRUD methods, and services with more business-specific methods.

We can take this blog post as an example. If you look at the interfaces at the bottom (images), he has two repositories and two services. How does one know what to put where?

As I said, repositories seems to be more for CRUD-like operations and Services more business oriented.

like image 532
alexn Avatar asked Sep 17 '09 17:09

alexn


People also ask

What is the difference between a repository and a service?

The Bank Metaphor: The bank holds your money in a vault, the vault is a database. The teller can deposit or withdraw from the vault, the teller is the repository. The customer is the one who asks the teller to deposit or withdraw, the customer is the service.

What is the difference between service layer and repository 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.

Can a repository use a service?

You definitely can. It's precisely the goal of repositories.

What is Controller Service and repository?

Controllers - contains application logic and passing user input data to service. Services - The middleware between controller and repository. Gather data from controller, performs validation and business logic, and calling repositories for data manipulation.


5 Answers

The repository is where the data is stored. The service is what manipulates the data.

In a real-world situation comparison, if your money is stored in a vault in a bank, the vault is the repository. The teller that deposits, withdraws, etc is the service.

like image 75
David Avatar answered Sep 30 '22 17:09

David


A Repository is essentially a facade for persistence that uses Collection style semantics (Add, Update, Remove) to supply access to data/objects. It is a way of decoupling the way you store data/objects from the rest of the application.

A service supplies coordination or other "services" that are required to operate your application. They are very different in that Services don't typically know how to access data from persistence, and repositories typically only access data/objects for any services you may have.

like image 37
jlembke Avatar answered Sep 30 '22 15:09

jlembke


I would say as a first try, in the general sense (until you give more context if you have one):

  • a repository is where you place some global objects, to be used later.
  • a service is a business logic code, made explicit (and ideally separated from the Presentation layer, and database layer ?)
like image 32
KLE Avatar answered Sep 30 '22 15:09

KLE


Take for instance in a MVC application.The Controller gives instruction to the Service and the service talks to the Repository to do some CRUD to the data in database.

This is done using DI(Dependency Injection:this is like a child telling the father to give him money but is not bothered about how the money is gotten,so the methods of getting the money was abstracted from the child's knowledge)

Repository communicate with the database either using raw SQL query Or Via ORM(e.g Eloquent,Sequelize,Gorm,Hibernate e.t.c)

Service calls one or more methods in the repository to get a specific result.(in the service you can call a sample method in the repository called findOne() and then base on the result you can call updateOne())

like image 24
Slycreator Avatar answered Sep 30 '22 16:09

Slycreator


A repository handles the data access and the service calls into it after performing any business logic needed.

@David 's answer definitely helped me but I would like to skew his approach a bit.

The Bank Metaphor: The bank holds your money in a vault, the vault is a database. The teller can deposit or withdraw from the vault, the teller is the repository. The customer is the one who asks the teller to deposit or withdraw, the customer is the service.

You can even take it further and say your employer (whoever writes your check) is the controller :D

The controller hands you your check -> you validate to make sure everything is correct before giving it to the teller -> the teller deposits.

So by thinking of it this way you can see the repository only cares about doing database operations or transactions, many services/customers can go to the same repository/teller.

like image 32
cjadd7 Avatar answered Sep 30 '22 17:09

cjadd7