Partial population is most evident in the database world. With databases, there is typically no requirement to retrieve an entire row of data or update an entire row of data at a single time. In fact, SQL and other query language
Table 17-1 shows one of the database tables that lies underneath the RoastedCoffeeBeansImpl class and that Java Data Objects (JDO) use for persistence. This table tracks the information about the individual coffee beans that you roast and sell.
|
LIDOID |
DESCRIPTION |
|
SKU |
WHOLESALE PRICE |
RETAIL PRICE |
POUNDS |
TYPE |
|---|---|---|---|---|---|---|---|
|
6 |
Bold and Rich |
French |
1044147687015 |
7.00 |
9.95 |
1 |
|
|
7 |
Smooth |
P.T. Breakfast |
1044147687016 |
7.00 |
9.95 |
1 |
Using SQL, it is easy to get a summary of the roasted coffee beans that includes the name, description, and retail price:
select Name, Description, RetailPrice from wsbook.csbwe_roastedcoffeebeansimpl;
From this query, the database returns the data in Table 17-2.
|
NAME |
DESCRIPTION |
RETAIL PRICE |
|---|---|---|
|
French |
Bold and Rich |
9.95 |
|
P.T. Breakfast |
Smooth |
9.95 |
The flexibility inherent in SQL and database manipulation does not typically translate to object-oriented Application Programming Interfaces (APIs). Object orientation puts type safety and structured contracts above the ability to manipulate the underlying data at will. Part of the reason for the structure is the
On the other hand, this book has encouraged decoupling data and behavior, leading to the promotion of the four base patterns (the Business Object, Business Object Collection, Business Process, and Asynchronous Business Process patterns). Using structured data allows you to use more flexible data manipulation techniques. Using a partial population technique in the P.T. Monday Coffee Company application will help set up your customers for success by allowing them to select and change only the data they need without the weight of the extraneous data. This pattern gives more flexibility to the Web Service user and does not force you to create data transfer objects for every variant of data that you think a customer may want.
You have a few different ways to implement a Partial Population pattern in object- oriented and service-oriented architectures. In this chapter, you will extend the Data Transfer Object and Data Transfer Collection patterns from the previous chapter with additional arguments on the retrieval and set operations. These operations allow you to
Figure 17-1:
The data transfer object and collection structures
The
DataTransferObject
instance serves as the basis for the Partial Population pattern. For the Partial Population pattern to be useful, the
DataTransferObject
instance contains more than a single value. A Partial Population pattern with a single value on the
DataTransferObject
instance does not make much sense; however, it is possible. In a
DataTransferObject
instance with two values,
value1
and
value2
, only one of these values must be
When looking at Figure 17-1, it is important to remember what you learned in the "An Observation on Web Services and the Data Transfer Object" sidebar from Chapter 16, "Implementing the Data Transfer Object Pattern." Specifically, the
DataTransferObject
instance could be the equivalent of a complete
BusinessObject
instance because of the way you implemented the Business Object pattern earlier in the book. You reduced the behavior on your
Rather than seeing each component of the pattern in full, you will focus on the pieces of the structure that distinguish the Partial Population pattern from the Data Transfer patterns:
DataTransferObject: The DataTransferObject component is a partially populated instance of the class designed by the service's developers. An alternative implementation of the pattern could add a set of keys to the DataTransferObject component to
indicate what fields are populated. For example, the key " value1 "tells the consumer that the property value1 containsaltered data. On the other hand, the property value2 contains stale or irrelevant data. Without this key field, you should expect that null fields are not populated and that non-null fields are populated. In fact, enforcing the usage of nulls to identify irrelevant data helps improve performance in distributed processes because the irrelevant data does not have to be marshaled across process boundaries.DataTransferCollection: Like the DataTransferObject component, you do not modify the DataTransferCollection component for the Partial Population pattern implementation. Further, there is not a variation for this top-level object; it remains as described in the previous chapter ” a collection of data transfer objects.
BusinessObject/BusinessObjectCollection: These classes carry the
burden of the Partial Population pattern implementation. In addition to simple get / set operations for the Data Transfer patterns, these objects typically surface additionalmethods that allow keys to be specified. The keys identify specific data that the client wants returned; this data is a subset of the data in the DataTransferObject itself.Client: The client
interacts with the BusinessObject and BusinessObjectCollection objects to retrieve or push instances of transfer objects and collections. When the client creates the transfer objects and collections, it populates only the fields that the target service should use. When the client consumes transfer objects and collections, it consumes only fields that are populated. If the client wants to customize the contents of the transfer objectsrequested from a BusinessObject or BusinessObjectCollection object, they should call appropriate methods on these instances that allow keys to be specified for the appropriate data.
As in the previous chapter, you could easily replace the BusinessObject and the BusinessObjectCollection components with the BusinessProcess and BusinessProcessManager components. The important structuring to remember for the Partial Population pattern is simply that you have target Web Services with collections of interesting data and you want to retrieve that data from a client or change custom groups of that data.
Just as the components of this pattern are the same as in the Data Transfer patterns, the interactions between the components are also the same. Whereas the previous chapter focused on retrieving data, this chapter focuses on changing data. Figure 17-2 illustrates a set of collaborations originating from a client to change data in an object on the server. This diagram could apply directly to the Data Transfer patterns.
Figure 17-2:
Changing data in the Partial Population and Data Transfer patterns
Except for the signature of the operations that act between components in Figure 17-2, there is no difference between this sequence and the sequence in the Data Transfer patterns.