Java Reference
In-Depth Information
The insert operation is called binding in LDAP -speak. Our application will
never know this, because we are going to wrap that in the DAO , and call it insert
instead.
public Contact insert(Contact contact) {
try {
DirContext ctx = getInitialContext();
ctx.bind(getDn(
contact.getUserId()),
null,
getAttributes(contact));
} catch (Exception e) {
log.error("Error adding contact", e);
throw new DaoException(e);
}
return contact;
}
Similarly, the update and delete methods use LDAP -specific classes to accomplish
their work and re-throw exceptions using the same technique. In JNDI terminol-
ogy, updating is called rebinding , and deleting is called unbinding . However,
because of our use of the DAO pattern, those terms will never appear in our appli-
cation—it is completely unaware of the LDAP dependency that we have created in
our DAO implementation.
LDAP is not the only strange data source we'll ever have to deal with, and while
there is no way to look at them all in one chapter (or even one topic!), we will take
a look at another that you will most likely have to deal with in the near future: the
web service.
11.2.2
Example: using a DAO with a web service
Using a DAO pattern for web services is a great idea. The reason for using a web
service with some sort of abstraction layer is to simplify testing the components
that use it. For example, if you have a credit card processor service that you want
to use or some other distant service that takes time to connect to, time to execute,
and time to process results, it can seriously hinder testing if you have to wait for it
(possibly several times). In addition, if the service requires a payment per call, or
is a public one (like Amazon, Google, or eBay) where the data that is returned
may change unexpectedly, it could be prohibitive to use for any purpose other
than integration or user acceptance testing because of the cost or variability of the
returned data. Adding to those issues is the need to prepare data that may change
over time for the tests—instead, you want to have reasonably static (or easily pre-
dictable) data requirements around your unit tests.
Search WWH ::




Custom Search