简体   繁体   English

关于Service / Repository层的Java Persistence层

[英]Java Persistence layer in regards to Service/Repository layers

I need a solid explanation on three software architecture layers (listed below) in a Java EE environment, that I have taken from multiple contexts. 我需要在Java EE环境中对三个软件体系结构层(下面列出)进行可靠的解释,这是我从多个上下文中获取的。 In short, this is what I have gained: 简而言之,这就是我所获得的:

  • Service Layer : Contains services behind the front-end with functionality fit for re-use during the software's life cycle 服务层 :包含前端后面的服务,其功能适合在软件生命周期中重复使用
  • Persistence Layer : Handles operations behind the front-end, intermediate between the front-end and the Repository layer. 持久层 :处理前端后面的操作,介于前端和存储库层之间。
  • Repository Layer : Wraps data-handling operations for data creation/retrieval in the back-end. 存储库层 :包装后端数据创建/检索的数据处理操作。

After panning through multiple articles, I am confused as to where the distinction lies between the Service and Persistence layers - could they overlap or be synonyms of one another? 在浏览了多篇文章之后,我对服务层和持久层之间的区别位置感到困惑 - 它们是否重叠或是彼此的同义词? I never hear them mentioned in really the same context. 我从来没有在真正相同的背景下听到过它们。

Are all of these layers always used/easily distinguishable? 所有这些层是否总是使用/容易区分?

Thank you. 谢谢。

The Service layer encapsulates the business logic and computations for the application. Service层封装了应用程序的业务逻辑和计算。 The word Service is used to emphasise the point that a business logic layer modeled using Service-oriented design principles can be used by different consumers, such as, a web presentation layer, an API integration layer, remote mobile clients, other services, and so on. 服务这个词用来强调使用面向服务的设计原则建模的业务逻辑层可以被不同的消费者使用,例如,Web表示层,API集成层,远程移动客户端,其他服务等等。上。 Examples of services could be PayrollService , DiscountService , OrderService , etc. This allows business logic to be written once and consumed in multiple places across different technologies, locations and applications. 服务的示例可以是PayrollServiceDiscountServiceOrderService等。这允许业务逻辑被编写一次并在不同技术,位置和应用程序的多个地方消费。

The Persistence layer is responsible for offering data access operations to the service layer. 持久层负责向服务层提供数据访问操作。 In order to obey the principle of loose-coupling, the service layer should not worry about how and where data is stored - simply that it can access required data when it needs to. 为了遵循松散耦合的原则,服务层不应该担心数据的存储方式和位置 - 只需要它可以在需要时访问所需的数据。 The service layer should simply apply the required business logic to the data, such that the data access code is separate from the business logic code (in any serious enterprise application). 服务层应该简单地将所需的业务逻辑应用于数据,使得数据访问代码与业务逻辑代码(在任何严肃的企业应用程序中)分离。


Repository is a design pattern commonly used for implementing the persistence layer. 存储库是一种常用于实现持久层的设计模式。 Among many other things, it allows the application data to be modeled and managed as a domain model (a way for technical team members and business users to share a common understanding of the business domain). 在许多其他方面,它允许将应用程序数据建模和管理为域模型(技术团队成员和业务用户共享对业务域的共同理解的方式)。 This allows the use of Domain-driven design by ensuring that the technical and non-technical users share a common terminology and technical artifacts mimic their real-world counterparts as closely as possible. 这允许使用域驱动设计 ,确保技术和非技术用户共享一个共同的术语,并且技术工件尽可能地模仿他们的现实对应物。 The repository pattern is also useful because it allows the service layer to access all (or at least most) data sources through a consistent interface (most frameworks that offer a repository based persistence layer provide the basic CRUD methods on all repositories). 存储库模式也很有用,因为它允许服务层通过一致的接口访问所有(或至少大多数)数据源(大多数提供基于存储库的持久层的框架在所有存储库上提供基本的CRUD方法)。 Examples of repositories could be OrderRepository , PersonRepository , DepartmentRepository , etc. Martin Fowler's website has an excellent overview of the repository pattern . 存储库的示例可以是OrderRepositoryPersonRepositoryDepartmentRepositoryOrderRepository Fowler的网站对存储库模式有一个很好的概述

That said, repository is not the only way to design and implement the persistence layer - it can be implemented in other ways as well, the simplest being the use of native data access technologies such as JDBC, ODBC, ADO.NET, etc. 也就是说,存储库不是设计和实现持久层的唯一方法 - 它也可以通过其他方式实现,最简单的方法是使用本机数据访问技术,如JDBC,ODBC,ADO.NET等。


Now, to answer the question, the Service and Persistence layers should be separate in a properly architected software system to provide loose coupling between the business logic and data access components of the system (if you agree that business logic and data access are two separate concerns). 现在,为了回答这个问题, 服务持久层应该在一个正确构建的软件系统中分开,以提供系统的业务逻辑和数据访问组件之间的松散耦合(如果您同意业务逻辑和数据访问是两个独立的问题)。 See SOLID principle for a primer on encapsulating individual application concerns as separate components. 有关将各个应用问题封装为单独组件的入门知识,请参阅SOLID原则 Any overlap between these two layers would be bad as it will lead to hard coupling, possible code duplication, scattered bugs, code inflexibility, etc. 这两层之间的任何重叠都会很糟糕,因为它会导致硬耦合,可能的代码重复,分散的错误,代码不灵活等。

Repository is a design pattern and one of the possible ways to implement the persistence layer, although there are other ways to implement the persistence layer. 存储库是一种设计模式,也是实现持久层的可能方式之一,尽管还有其他方法可以实现持久层。 The following visual depiction may help: 以下视觉描述可能有所帮助:

 -------------------------------                 -------------------------------
|       W e b   l a y e r       |               |        A P I   l a y e r      |
 -------------------------------                 -------------------------------
 -------------------------------------------------------------------------------
|                          S e r v i c e   l a y e r                            |
 -------------------------------------------------------------------------------
 -------------------------------------------------------------------------------
|                      P e r s i s t e n c e   l a y e r                        |
 -------------------------------------------------------------------------------
                                ---------------------------
                               /                          /
                               ---------------------------
                             /                           /
                             ---------------------------
                           /   D a t a   S t o r e s   /
                           ---------------------------

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM