Spring框架中的DAO,DTO和Service层是什么? [英] What are the DAO, DTO and Service layers in Spring Framework?
问题描述
我正在使用spring和hibernate编写RESTful服务.我在互联网上阅读了许多资源,但是它们并没有澄清我的疑问.请详细解释一下spring框架中的 DAO , DTO 和 Service 层是什么?以及为什么在春季需要使用这些层来开发RESTfull API服务.
首先,这些概念是平台不可知的,因此对于Spring Framework或任何其他框架不是唯一的.>
数据传输对象
DTO
是在进程之间传送数据的对象.当您使用远程接口时,每次调用都很昂贵.因此,您需要减少通话次数.解决方案是创建一个Data Transfer Object
,它可以保存呼叫的所有数据.它必须是 serializable 才能通过连接.通常,在服务器端使用汇编程序在DTO
和任何域对象之间传输数据.通常很少
不仅仅是一堆字段,还有它们的获取器和设置器.
数据访问对象
A Data Access Object
抽象并封装对以下内容的所有访问
数据源. DAO
管理与数据源的连接,以
获取和存储数据.
DAO实现了使用数据源所需的访问机制.
数据源可以是像RDBMS
这样的持久性存储,也可以是通过REST
或SOAP
访问的业务服务.
DAO
将Service
对象的基础数据访问实现抽象为
启用对数据源的透明访问. Service
也可以委托
数据加载和存储到DAO
.
服务
Service
对象正在执行
应用程序需要针对您正在使用的域进行操作.它涉及基于输入的计算和
存储的数据,验证来自演示文稿的任何数据,并弄清楚究竟是什么数据
要分发的源逻辑,具体取决于从演示文稿收到的命令.
A Service Layer
从以下位置定义应用程序的边界及其可用操作集
连接客户端层的角度.它封装了应用程序的业务逻辑,控制
交易并协调其运营过程中的响应.
推荐参考
Martin Fowler有一本关于常见的 Application Architecture Patterns 的好书,名为企业应用程序体系结构的模式.还有核心J2EE模式,值得研究
I am writing RESTful services using spring and hibernate. I read many resource in internet, but they did not clarify my doubts. Please explain me in details what are DAO, DTO and Service layers in spring framework? And why usage of these layers is required in spring to develop RESTfull API services.
First off, these concepts are Platform Agnostic and are not exclusive to Spring Framework or any other framework, for that matter.
Data Transfer Object
DTO
is an object that carries data between processes. When you're working with a remote interface, each call it is expensive. As a result you need to reduce the number of calls. The solution is to create a Data Transfer Object
that can hold all the data for the call. It needs to be serializable to go across the connection. Usually an assembler is used on the server side to transfer data between the DTO
and any domain objects. It's often little
more than a bunch of fields and the getters and setters for them.
Data Access Object
A Data Access Object
abstracts and encapsulates all access to
the data source. The DAO
manages the connection with the data source to
obtain and store data.
The DAO implements the access mechanism required to work with the data source.
The data source could be a persistent store like an RDBMS
, or a business service accessed via REST
or SOAP
.
The DAO
abstracts the underlying data access implementation for the Service
objects to
enable transparent access to the data source. The Service
also delegates
data load and store operations to the DAO
.
Service
Service
objects are doing the work that the
application needs to do for the domain you're working with. It involves calculations based on inputs and
stored data, validation of any data that comes in from the presentation, and figuring out exactly what data
source logic to dispatch, depending on commands received from the presentation.
A Service Layer
defines an application's boundary and its set of available operations from
the perspective of interfacing client layers. It encapsulates the application's business logic, controlling
transactions and coordinating responses in the implementation of its operations.
Recommended References
Martin Fowler has a great book on common Application Architecture Patterns named Patterns of Enterprise Application Architecture. There is also, Core J2EE Patterns that worth looking at.
这篇关于Spring框架中的DAO,DTO和Service层是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!