Java Reference
In-Depth Information
Hibernate, is encapsulated by the repository classes. Each repository consists of an
interface and a Hibernate implementation class and is responsible for one type of
object. The JDO implementation would be similar.
In this example, repositories call the Hibernate API s to access the database.
AccountRepository finds accounts and BankingTransactionRepository creates
BankingTransaction s. The TransferService is written in terms of the Account-
Repository and BankingTransactionRepository interfaces, which decouples it
from the persistence framework and the database. By the intelligent use of inter-
faces, you can avoid coupling your domain logic to a particular persistence frame-
work. This will enable you to test the domain model without the database, which
simplifies and accelerates testing. It also enables you to use a different persistence
framework if your needs change. For example, changing this application from
Hibernate to JDO or even EJB 3 is simply a matter of changing the concrete classes
that access the persistence framework. It's a generally accepted observation that
loosely coupled applications are easier to maintain and test, and you will see
examples of how to do this throughout this topic.
1.2.4
Eliminating DTOs
Another way to improve a J2EE application is to eliminate the DTO s, also known as
value objects. A DTO is a simple object consisting of only fields (i.e., no behavior)
and is used to return data from the business tier to the presentation tier. An EJB
application uses DTO s because EJB 2 entity beans cannot be efficiently accessed by
the presentation tier. Each call to an entity bean might be a remote call and/or a
separate database transaction. As a result, they must only be accessed by the ses-
sion façade, which copies data from them into DTO s. The trouble with using
DTO s, however, is that they and the code that creates them are extremely tedious
to develop and can sometimes be a significant portion of a J2EE application.
Hibernate, JDO , and EJB 3 objects do not have this limitation and can be accessed
directly by the presentation tier. As a result, we eliminate many or all of the DTO s
in an application.
Returning domain objects to the presentation tier
There are a couple of ways to return Hibernate, JDO , and EJB 3 objects to the pre-
sentation tier. One option is for the business tier to return objects that are still
persistent. This can be simpler to implement but requires the presentation tier to
manage database connections, which is sometimes neither desirable nor possible.
I will describe this option in more detail in chapter 8.
 
 
 
 
 
Search WWH ::




Custom Search