What are the DAO, DTO and Service layers in Spring Framework?

给你一囗甜甜゛ 提交于 2019-12-02 14:08:20

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.

vels4j

DAO - Data Access Object:

An object that provides a common interface to perform all database operations like persistence mechanism.

public interface GenericDao<T> {
  public T find(Class<T> entityClass, Object id);
  public void save(T entity);
  public T update(T entity);
  public void delete(T entity);
  public List<T> findAll(Class<T> entityClass);
}

See this example : Spring – DAO and Service layer

DTO - Data Transfer Object:

An object that carries data between processes in order to reduce the number of method calls means you combine more than one POJO entities in service layer.

For example a GET request /rest/customer/101/orders is to retrieve all the orders for customer id 101 along with customer details hence you need combine entity Customer and entity Orders with details.

gajendra kumar

Enterprise application is divided into tiers for easy maintenance and development. Tiers are dedicated to particular type of tasks like

  • presentation layer (UI)
  • Business layer
  • Data access layer (DAO, DTO)

Why this design: Let's pick an example you have an application which reads data from db and performs some business logic on it then present it to user. Now if you want to change your DB let say earlier application was running on Oracle now you want to use mysql so if you don't develop it in tiers you will doing changes everywhere in application. But if you implement DAO in application then this can be done easily

DAO: Data Access Object is design pattern just provide an interface for accessing data to service layer and provide different implementations for different data sources (Databases, File systems)

Example code:

public interface DaoService {
    public boolean create(Object record);
    public CustomerTemp findTmp(String id);
    public Customer find(String id);
    public List getAllTmp();
    public List getAll();
    public boolean update(Object record);
    public boolean delete(Object record);   
    public User getUser(String email);
    public boolean addUser(User user);
}

Service layer using Dao

@Service("checkerService")
public class CheckerServiceImpl implements CheckerService{

@Autowired
@Qualifier("customerService")
private DaoService daoService;

Now i can provide any implementation of DaoService interface. Service and DTO are also used for separation of concerns.

Babu Babu

SERVICE LAYER:

It receives the request from controller layer and process the request to Persistence layer

  • @Controller:It is the annotation which initialize whole controller layer.
  • @Service:It is the annotation which initialize whole service layer.
  • @Repository: It is the annotation which initialize whole persistence layer.

DTO:

It is an Data Transfer object which used to pass the properties from service layer to persistence layer.

DAO:

It is an Data Access object. it is also known as persistence layer. In this DAO we receive property values from service layer in DTO object. Here we write an persistence logic to db.

DTO is the @Entity annotation in Spring
DAO is the @Repository annotation in Spring (With Spring Boot JPA you just need to implement the interface now you don't need the annotations)
Service is the @Service annotation in Spring

You can read more here: Accessing Data with JPA

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!