Java Reference
In-Depth Information
Because the business logic in this use case is simple, there isn't a separate
POJO
façade and the
AcknowledgeOrderService
is called by directly by the presentation
tier. A Spring
TransactionInterceptor
ensures that each call to
AcknowledgeOr-
derService
executes within its own transaction. The same design works with both
Hibernate and
JDO
detached objects because the different
API
s for detaching and
attach objects are encapsulated behind the
OrderAttachmentManager
inter-
face.The responsibilities of each class are as follows:
■
AcknowledgeOrderService
defines the public interface that encapsulates the
business logic.
■
AcknowledgeOrderServiceImpl
implements the business logic.
■
Order
is the domain object.
■
OrderRepository
is used by the
AcknowledgeOrderService
to find the order.
■
OrderAttachmentManager
encapsulates the mechanism for attaching and
detaching orders.
■
AcknowledgeOrderResult
is returned by the service and contains a status
code and a detached order.
In this design, the presentation tier calls
AcknowledgeOrderService.getOrder-
ToAcknowledge()
, which returns the detached order, including its line items and
restaurants. The presentation tier displays the order to the user and stores it in
the
HttpSession
. When the user saves their changes, the presentation tier updates
the order by calling
Order.accept()
or
Order.reject()
, and then passes it to
AcknowledgeOrderService.acknowledgeOrder()
. This method reattaches the
order, which updates the database with the changes. If the order has changed in
the database, the persistence framework throws an exception.
13.4.1
Implementing the domain service
Most of the business logic for this use case is implemented by
AcknowledgeOrder-
ServiceImpl
, which is shown in listing 13.1. It defines a
getOrderToAcknowledge()
method, which returns the detached order, and an
acknowledgeOrder()
method,
which reattaches the updated order.
AcknowledgeOrderServiceImpl
uses an
OrderRepository
to retrieve the order and an
OrderAttachmentManager
to detach
and attach the
Order
.
Listing 13.1
AcknowledgeOrderServiceImpl
public class AcknowledgeOrderServiceImpl implements
AcknowledgeOrderService {
Search WWH ::
Custom Search