简体   繁体   English

在Spring Boot应用程序中组织服务,服务实现和存储库的最佳实践

[英]Best practice to organize service,service implementation and repository in a spring boot application

Sample project structure that can be found in most of the appliations 可以在大多数应用程序中找到的示例项目结构
In most of the spring boot applications there are JPA repository, service and service implementation. 在大多数Spring Boot应用程序中,都有JPA存储库,服务和服务实现。 Can anybody explain the pros and cons of 任何人都可以解释其优缺点

  1. Just using repository only when needed 仅在需要时使用存储库
  2. Using service class and it's application. 使用服务类及其应用程序。
  3. Use service interfaces and use the implementation. 使用服务接口并使用实现。

Different blog posts have different explanations. 不同的博客文章有不同的解释。 Seeking for an expert experience. 寻求专家经验。

1. Just using repository only when needed -->You should create and use it only when its needed; 1.仅在需要时使用存储库 ->您应仅在需要时创建和使用它; that's fine. 没关系。 In general its good practice not to create something unless you really require it. 通常,除非您确实需要,否则不要创建任何东西,这是一种好的做法。

2. Use Service class and use that --> Service is just class where you are writing your business logic by fetching some data from repository classes(DAO-data access object layer); 2.使用Service类,并使用 -> Service只是通过从存储库类(DAO数据访问对象层)中获取一些数据来编写业务逻辑的类。 Its good practice to keep them segregated; 保持隔离的优良作法; so that any change in the DAO or service should not affect each other. 这样DAO或服务中的任何更改都不会相互影响。

3. User service interface and Implement that and use the implementation. 3.用户服务界面并实施并使用实施。

In general you code in terms of interface; 通常,您是根据接口进行编码的; its good design andcoding practice; 良好的设计和编码实践; this way you create loosely couple code; 这样,您可以创建松散的耦合代码; so where ever you use the Service you inject it as interface type and its underlined implementation you can plug in and plug out; 因此,无论您在哪里使用服务,都可以将其作为接口类型及其带下划线的实现注入,可以插入和拔出; of Course if there are multiple implementation for your Service interface. 当然,如果您的Service接口有多种实现。 For example-you have two different implementation for your ShapeService interface for two different client to calculate Area where one client is interested in to calculate the area of Recatangle whereas other is in Square. 例如,您的ShapeService接口有两种不同的实现方式,供两个不同的客户端计算Area,其中一个客户端有兴趣计算Recatangle的面积,而另一个客户端在Square中。 So in this case if you create Service interface and its Impl; 因此,在这种情况下,如果您创建Service接口及其Impl; you can keep the logic intact or unchanged for the class where this Service interface is going to be used. 您可以将要使用此Service接口的类的逻辑保持不变或不变。 Also in future if there are many implementation for shape it will be easy to change; 同样在将来,如果有很多形状实现方案,则很容易更改。 your code design would be open for extension but close for modification. 您的代码设计可以进行扩展,但可以进行修改。

I would recommend if you have single implementation for service class then go directly with class instead of creating Service interface and then separate Impl class; 我建议如果服务类具有单个实现,则直接使用类而不是创建Service接口,然后再分离Impl类。

First of all, every design pattern is made to solve a common problem in software development. 首先,每种设计模式都可以解决软件开发中的常见问题。 If you are sure that you won't face those problems in your specific use case, you don't need to follow these patterns. 如果您确定在特定用例中不会遇到这些问题,则无需遵循这些模式。

  1. You can call repositories directly from your controller or wherever you need it. 您可以直接从控制器或任何需要的地方调用存储库。 Repositories should perform basic database operations and there's no problem in calling them if that's all you need. 存储库应该执行基本的数据库操作,并且只要您需要就可以调用它们。

  2. But most of the time, you want some business logic to be performed before/after working with the database. 但是大多数时候,您希望在使用数据库之前/之后执行一些业务逻辑。 That's where Service classes get involved (one of the SOLID principles is the Single Responsibility Principle - you should not have both persistence operations and business logic in the same class). 这就是涉及服务类的地方( SOLID原则之一是单一职责原则-您不应在同一类中同时具有持久性操作和业务逻辑)。 For example, you call your EmployeeService.save() method to save an employee, and that method executes business logic (for example, checks that the employee's ID number is correct) and then calls the repository class (which only saves the employee in the database). 例如,您调用EmployeeService.save()方法来保存一名雇员,然后该方法执行业务逻辑(例如,检查该雇员的ID号是否正确),然后调用存储库类(该类仅将雇员保存在数据库)。

  3. The interface and implementation pattern is an extension of this last one. 接口和实现模式是最后一个的扩展。 Having an interface makes your application easier to maintain in the future if new functionalities are needed. 如果需要新功能,拥有接口可使您的应用程序将来更容易维护。 Ideally, most of your application will call the interface, and the appropriate implementation will be provided (for example, by injection in Spring components). 理想情况下,大多数应用程序将调用该接口,并提供适当的实现(例如,通过注入Spring组件)。 For example, if a year from now you need a special implementation that performs different business logic, you can just implement that interface and inject the bean as needed. 例如,如果一年之后需要使用执行不同业务逻辑的特殊实现,则只需实现该接口并根据需要注入Bean。

That's why most developers future-proof their applications by using the repository-interface-implementation structure. 这就是为什么大多数开发人员通过使用存储库接口实现结构来对应用程序进行未来验证的原因。 If you are sure that you'll only need basic CRUD operations, there's no need to create Services. 如果您确定只需要基本的CRUD操作,则无需创建服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 隐藏服务实现的最佳实践 - Best practice to hide a service implementation 在 Spring 引导上创建存储库的最佳实践是什么? - What is the best practice to create repository on Spring Boot? 最佳实践Spring 4 Controller MVC-服务发布 - Best Practice Spring 4 Controller MVC - Service releation 在spring服务层中使用继承是最佳做法吗? - Is it a best practice to use inheritance in the spring service layer? 带有控制器的单元测试Spring Boot->服务->存储库 - Unit Test Spring Boot with Controller -> Service -> Repository Spring引导Autowired Service和Repository抛出nullpointerexception - Spring boot Autowired Service and Repository throwing nullpointerexception 使用Jpa存储库测试的Spring Boot Service - Spring Boot Service using Jpa repository Testing Spring Boot 应用程序 - 从服务向控制器抛出 EntityExists - 或 EntityNotFoundException 是否被认为是不好的做法? - Spring Boot Application - Is it considered bad practice to throw EntityExists - or EntityNotFoundException from Service to Controller? 什么是使用Controller,Service和Repository注释的最佳实践? - Whats the best practice of using Controller,Service and Repository annotations? 在基于Spring的Web应用程序中,在运行时控制对服务/屏幕访问的访问的最佳实践 - Best Practice to control access to service/screen access during run time in Spring based web application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM