Java Reference
In-Depth Information
then return some of the DTO s to the presentation tier, which uses the data within
those DTO s to generate the response to the user.
Accessing the database with DAOs
Transaction scripts could access the database by calling JDBC or i BATIS directly.
The trouble with this approach is that the transaction scripts will contain a mixture
of business logic and database access code, which can make them difficult to main-
tain and test. It's better to move the database access code into a separate set of DAO
classes that encapsulate the database access logic and define methods for creating,
reading, deleting, and updating rows in the database tables. There is typically one
DAO for each of the main entities in the application. A DAO returns the results of
a query as one or more DTO s, and a transaction script passes DTO s to the DAO s in
order to insert or update data in the database. As you will see later, using DAO s sim-
plifies the transaction scripts and makes them considerably easier to test.
An example of a transaction script-based design
Let's look at a simple example of a transaction script-based design. The design,
which is shown in figure 9.1, uses the Transaction Script pattern to implement the
business logic for the Place Order use case. It consists of the following:
The PlaceOrderTransactionScripts class implements the transaction scripts.
DAO s encapsulate the database access logic.
DTO s contain data that is retrieved from the database and returned to the
presentation tier.
The Spring TransactionInterceptor class ensures that each invocation of a
transaction script is transactional.
The PlaceOrderTransactionScripts class defines a transaction script for each
request that it must handle, including updateDeliveryInfo() , which is called
when the user enters the delivery information, and updateRestaurant() , which is
called when the user selects a restaurant.
The transaction scripts access the database by calling the DAO s, which define
methods for creating, finding, deleting, and updating pending orders and restau-
rants in the database. Each DAO consists of an interface, as well as an implementa-
tion class that uses either JDBC or i BATIS to access the database. The
PendingOrderDAO queries and updates the PENDING_ORDER and PENDING
_ORDER_LINE_ITEM tables, and the RestaurantDAO queries the RESTAURANT and
MENU_ITEM tables.
 
 
Search WWH ::




Custom Search