Java Repository 和 Service 层的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22963352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:38:51  来源:igfitidea点击:

Difference between Repository and Service layer

javaservicerepositorydata-access-layer

提问by April

I looked through some related questions but still I don't see much difference between the a repository and a service layer. So given the example I suppose it should look like this , if not please tell me why?

我查看了一些相关的问题,但我仍然没有看到存储库和服务层之间的太大区别。所以给出这个例子,我想它应该是这样的,如果不是,请告诉我为什么?

public interface ProductRepository extends CrudRepository<Product, Long>{

    public List<Product> findByName(String name);
    public List<Product> findByPrice(Double price);
}

public interface ProductService {

    public List<Product> findAll();
    public Product findById(Long id);
    public Product save(Product product);
    public void delete(Product product);
    public List<Product> findByName(String name);
    public List<Product> findByPrice(Double price);
}

and the implementation of the ProductService would use the ProductRepository to implement the methods. As I understand from http://docs.spring.io/spring-data/jpa/docs/1.3.0.RELEASE/reference/html/jpa.repositories.htmlthe queries for methods from the repository are auto generated. In my example the methods are repeated in the repository and Service, so please explain what/why needs to be changed?

而 ProductService 的实现将使用 ProductRepository 来实现这些方法。正如我从http://docs.spring.io/spring-data/jpa/docs/1.3.0.RELEASE/reference/html/jpa.repositories.html了解到的,对存储库中方法的查询是自动生成的。在我的示例中,这些方法在存储库和服务中重复,所以请解释什么/为什么需要更改?

回答by JasonWilczak

As far as I know, the Repository is meant for directly accessing the database. This is where direct calls to the stored procedures or whatever your data storage mechanism is will be.

据我所知,Repository 是用来直接访问数据库的。这是直接调用存储过程或任何数据存储机制的地方。

The service layer is the API to your data. There is usually some logic level that you would do hear, or in another layer of abstraction between the service and the repository.

服务层是数据的 API。您通常会听到一些逻辑级别,或者在服务和存储库之间的另一个抽象层中。

For example, a website would call a method in your service. Your service would call your repository for getting that data, then your service would transform it somehow (build objects, generate dynamic information based on business rules, etc.) then pass that back up to the website.

例如,网站会调用您的服务中的方法。您的服务会调用您的存储库来获取该数据,然后您的服务会以某种方式对其进行转换(构建对象、根据业务规则生成动态信息等),然后将其传回网站。

回答by Martin

Repository Layer gives you additional level of abstraction over data access. Repository layer exposes basic CRUD operations.

存储库层为您提供了对数据访问的额外抽象级别。存储库层公开基本的 CRUD 操作。

Service layer exposes business logic, which uses repository.

服务层暴露业务逻辑,使用存储库。

You can read a more detailed answer here: https://stackoverflow.com/a/5049454/1446006

您可以在此处阅读更详细的答案:https: //stackoverflow.com/a/5049454/1446006

回答by Jay

A Repository is a data access pattern in which data transfer objects are passed into a repository object that manages CRUD operations. This pattern is useful in situations where the mechanism of your data access may change significantly -- e.g. you expect to have varying data stores like Oracle in one implementation and SQL Server or even HADOOP in another.

存储库是一种数据访问模式,其中数据传输对象被传递到管理 CRUD 操作的存储库对象。这种模式在您的数据访问机制可能发生重大变化的情况下很有用——例如,您希望在一个实现中拥有不同的数据存储,如 Oracle 和 SQL Server 甚至 HADOOP 在另一个实现中。

A Service Layer is a business logic pattern that is commonly used in SaaS architectures. Using a service layer allows one or more presentation implementations to access your business logic through a common interface. For example if you wanted your website to have an API you would use a service layer to implement common back-end functionality that both the site and the API would consume.

服务层是 SaaS 架构中常用的业务逻辑模式。使用服务层允许一个或多个表示实现通过公共接口访问您的业务逻辑。例如,如果您希望您的网站有一个 API,您可以使用服务层来实现网站和 API 都将使用的通用后端功能。

The former should be concerned mostly with data access and the latter with business logic. Neither are mandatory nor must one accompany the other. In simple applications both patterns may be implemented by the same class.

前者主要关注数据访问,后者关注业务逻辑。两者都不是强制性的,也不是必须伴随另一个。在简单的应用程序中,两种模式都可以由同一个类实现。

回答by Mani

All your business logic should be in the Service Layer.

您所有的业务逻辑都应该在服务层中。

Any access to the Database (any storage) should go to the Repository Layer.

对数据库(任何存储)的任何访问都应转到存储库层。

Lets take an Example. You have to save an entity(Person). But before saving the Person you want to make sure that the Person's FirstName does not exist already.

让我们举个例子。你必须保存一个实体(人)。但是在保存 Person 之前,您要确保 Person 的名字不存在。

So the validation part should go to the business layer.

所以验证部分应该去业务层。

In the service Layer

在服务层

PersonRepository repository; 
public Person save(Person p){
   Person p = findByName(p.getName();
   if (p != null){
          return some customException();
   }
   return repository.save(p); 
}

public Person findByName(String name){
     return repository.findByName(name);
}

And in your Repository Layer just concentrate on DB Operation.

在您的存储库层中,只需专注于数据库操作。

You could have done this in Repository Layer it self. Assume you have implemented this in your Repository then your save method always check before saving (some time you may not required to do).

您可以自己在 Repository Layer 中完成此操作。假设你已经在你的 Repository 中实现了这个,那么你的 save 方法总是在保存之前检查(有时你可能不需要这样做)。