Issue
I am reading about the DAO design pattern on Oracle's website and I'm trying to understand the below image in the context of using JSP's, Servlets, plain java objects, and the MVC pattern. In my case would the BusinessObject be my servlet and the TransferObject be my java class with only properties, mutators, and accessors (DTO)?
For example, if I had this code in a servlet (controller)
DTO.setFirstName(request.getParameter("firstName"));
DTO.setLastName(request.getParameter("lastName"));
DAO.save(DTO);
(source: sun.com)
Solution
Almost. Between the controller, which handles presentation logic, and the DAO, which handles Data access logic, there should be a business layer, containing the business objects.
The main responsibilities of these business objects are
- to provide business services to the controllers. They are a facade
- to encapsulate the business logic of the application
- to demarcate transacations
- to use one or several DAOs to get, find and persist objects.
This layer is very important because you want to be able to perform several operations on your database within a single transaction. And it should not be the responsibility of the web controller to handle this. Moreover, the same business services could be used by other clients than the web controllers (Swing client, batch, etc.)
Business objects are typically implemented using session EJBs, or Spring services.
They're also useful to be able to
- unit test the controller by mocking the business objects
- unit test the business logic by mocking the DAOs
Answered By - JB Nizet