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.
Generally the DAO is as light as possible and exists solely to provide a connection to the DB, sometimes abstracted so different DB backends can be used. The service layer is there to provide logic to operate on the data sent to and from the DAO and the client.
The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies like JDBC, Hibernate, JPA or JDO in a consistent way.
The DAO classes provide methods for insertion, deletion, updation and finder methods. The basic purpose of creating DAO is the loose coupling and non-repeation of code. DTO: "Data transfer objects " can travel between seperate layers in software architecture.
DAO is a class that usually has the CRUD operations like save, update, delete. DTO is just an object that holds data. It is JavaBean with instance variables and setter and getters. The DTO is used to expose several values in a bean like fashion.
First off, these concepts are Platform Agnostic and are not exclusive to Spring Framework or any other framework, for that matter.
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.
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With