views:

350

answers:

4

Hi!

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 do one know what to put where?

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

Thanks

+1  A: 

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 ?)
KLE
A: 

I think from the caller's perspective, there is no general difference. One advantage of using the repository pattern to wrap database access is that you can change the implementation to hit a service later if needed.

From the implementation perspective, the repository pattern usually describes something that hits a database. A service might refer to something that hits another service, but it can be more general then that.

Frank Schwieterman
+5  A: 

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.

jlembke
Thanks for this explanation! I've got it!
alexn
I would say that a Repository is a type of service that is used for data access.
Ian Ringrose
That's a fine definition in the sense that almost everything we write is a "service" at some level, but it loses the basic intent that a repository is supposed to be a *collection* of objects.
jlembke
+2  A: 

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.

David Stratton
Thanks for this answer! Simple and concise.
alexn