?? lib0031.html
字號:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Data Access Object Layer</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0030.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0032.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
<br>
<div class="chapter">
<a name="ch05"></a>
<div class="section">
<h2 class="first-section-title"><a name="137"></a><a name="ch05lev1sec2"></a>Data Access Object Layer</h2><p class="first-para">Data access objects (DAOs) manage access to persistent storage of some type. Usually, the storage used is a relational database, but DAOs can manage files, XML documents, and other types of persistent storage as well.</p>
<p class="para">The primary reasons to separate data access from the rest of the application is that it's easier to switch data sources and share DAOs between applications. Medium-to large-sized businesses in particular are likely to have multiple applications using the same data access logic.</p>
<p class="para">A couple of patterns for data access objects are most common. The simplest pattern has each persistent object represented as a DAO. I call this the simplified data access pattern. The more complex, but more flexible, pattern <a name="138"></a><a name="IDX-53"></a>in common use is a factory-based pattern. In fact, it's called the data access object pattern. I'll define each pattern later in the section.</p>
<p class="para">For convenience, I separate DAO objects in the package hierarchy (e.g., <span class="fixed">com.acme.appname.data</span> or <span class="fixed">com.acme.appname.dao</span>). I only mention this because some modeling tools (e.g., Rational Rose) encourage you to decide your package structure at modeling time. Some developers also add a DAO suffix to data access object names; for example, a customer DAO might be named <span class="fixed">CustomerDAO</span>.</p>
<div class="section">
<h3 class="sect3-title">
<a name="139"></a><a name="ch05lev2sec1"></a>Choosing a Database Persistence Method</h3>
<p class="first-para">The question of which persistence method is best is the subject of considerable disagreement and debate. Although the J2EE specification provides for entity beans, other popular forms of database persistence exist. The degree to which developers take sides in the debate is akin to a discussion of religion or politics. The debate is not entirely rational. I'll take you through my thoughts on the different options, what I see in the marketplace, and what I prefer to use. However, the modeling concepts in this chapter are applicable to all persistence methods.</p>
<p class="para">When beginning object-modeling activities, you can identify a DAO without choosing the persistence method you'll use at implementation. For instance, the DAO could be a custom-coded JDBC class, an entity bean (EJB) with bean-managed persistence (BMP) or container-managed persistence (CMP), a JDO object, or an object generated by an object-relational (O/R) mapping tool such as TopLink or Hibernate. J2EE applications are compatible with all these persistence methods. However, you should choose a persistence method before completing object-modeling activities, because the method you choose can affect the design.</p>
<p class="para">In making a decision, I first consider what needs to happen at the data access objects layer. I then grade each persistence method according to how well it achieves the goals, using the following rating system:</p>
<ul class="itemizedlist">
<li class="first-listitem">
<p class="first-para">High (best rating): Gets high marks toward achieving the stated goal</p>
</li>
<li class="listitem">
<p class="first-para">Medium (middle rating): Moderately achieves the stated goal</p>
</li>
<li class="listitem">
<p class="first-para">Low (lowest rating): Doesn't achieve the stated goal very well</p>
</li>
</ul>
<p class="para">
<a class="internaljump" href="#ch05table02">Table 5.2</a> lists the goals and ratings of several data persistence methods. Following the table are explanations of my reasoning in determining the ratings. I consider the first four goals listed in the table to be the most important to the majority of my clients.</p>
<a name="140"></a><a name="ch05table02"></a>
<table class="table" border="1">
<caption class="table-title">
<span class="table-title"><span class="table-titlelabel">Table 5.2: </span>Ratings of Data Persistence Methods</span>
</caption>
<thead>
<tr valign="top">
<th class="th" scope="col" align="left">
<p class="table-para">
<b class="bold">Goal</b>
</p>
</th><th class="th" scope="col" align="left">
<p class="table-para">
<b class="bold">JDBC</b>
</p>
</th><th class="th" scope="col" align="left">
<p class="table-para">
<b class="bold">EJB/BMP</b>
</p>
</th><th class="th" scope="col" align="left">
<p class="table-para">
<b class="bold">EJB/CMP</b>
</p>
</th><th class="th" scope="col" align="left">
<p class="table-para">
<b class="bold">JDO</b>
</p>
</th><th class="th" scope="col" align="left">
<p class="table-para">
<b class="bold">O/R Tool</b>
</p>
</th>
</tr>
</thead>
<tbody>
<tr valign="top">
<td class="td" align="left">
<p class="table-para">
<b class="bold">Minimize learning curve</b>
</p>
</td><td class="td" align="left">
<p class="table-para">High</p>
</td><td class="td" align="left">
<p class="table-para">Low</p>
</td><td class="td" align="left">
<p class="table-para">Low</p>
</td><td class="td" align="left">
<p class="table-para">Medium</p>
</td><td class="td" align="left">
<p class="table-para">Medium</p>
</td>
</tr>
<tr valign="top">
<td class="td" align="left">
<p class="table-para">
<b class="bold">Minimize code and configuration files written and maintained</b>
</p>
</td><td class="td" align="left">
<p class="table-para">Low</p>
</td><td class="td" align="left">
<p class="table-para">Low</p>
</td><td class="td" align="left">
<p class="table-para">Low</p>
</td><td class="td" align="left">
<p class="table-para">Medium</p>
</td><td class="td" align="left">
<p class="table-para">Medium</p>
</td>
</tr>
<tr valign="top">
<td class="td" align="left">
<p class="table-para">
<b class="bold">Maximize ability to tune</b>
</p>
</td><td class="td" align="left">
<p class="table-para">High</p>
</td><td class="td" align="left">
<p class="table-para">Medium</p>
</td><td class="td" align="left">
<p class="table-para">Low</p>
</td><td class="td" align="left">
<p class="table-para">Low</p>
</td><td class="td" align="left">
<p class="table-para">Low</p>
</td>
</tr>
<tr valign="top">
<td class="td" align="left">
<p class="table-para">
<b class="bold">Minimize deployment effort</b>
</p>
</td><td class="td" align="left">
<p class="table-para">High</p>
</td><td class="td" align="left">
<p class="table-para">Low</p>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -