compare repository vs provider vs service

architecture, design-patterns, oop

Solution

The Repository and Service are not mutually exclusive. In fact, they are often used together.

The service layer sits on top of your domain objects and provides a coarse-grained interface for business operations. It usually describes the use cases of your application. The service layer uses repositories for obtaining your domain objects and delegates further execution to them where possible.

The Repository acts like a collection of persistent domain objects. It provides methods for finding the right objects using some criteria. It also provides methods for saving those objects.

The implementations of repository in the wild vary quite a bit. Repositories could provide methods like

List<Person> findPersonByName(String name)

or a more generic approach with criteria objects

List<Person> find(Criteria criteria)

Additional reading: service layer, repository

I'm not familiar with the Provider pattern.

Problem

I need to implement logic which will be retrieve data from some remote datasource. And now I need to decide - which concept should I need: Provider, Repository or Service. Actually I do not understand very well all great difference between that. Yeas, I know that repository is something more data specific and should not contain any business logic. Provider for other hand may contain some business rules in addition to manage data. And Service also could contain some business logic in addition to manage data. Then what is the difference between Service and Provider. From the other point of view, I think that using service is better approach to show that it's an abstraction for remote access. In conclusion: All this approaches looks reasonable and I completely confused with it. Will be pretty much appreciate if someone will help me with it.

Original source

Related problems