Flylib.com

Books Software

 
 
 

Seeing Partial Population in Practice


Seeing Partial Population in Practice

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 derivations allow you to partially populate data or retrieve portions of a row in a database without having to deal with the other pieces of data.

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.

Table 17-1: Roasted Coffee Bean Database Table

LIDOID

DESCRIPTION

NAME

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.

Table 17-2: Query Result Table

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 desire to couple data and behavior in a single class. Once you protect the data from the user and overlay the data with behavior, it becomes incrementally more difficult to allow a client to freely manipulate data.

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.



Understanding the Structure of the Partial Population Pattern

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 designate the data required or the data set by the service user . Figure 17-1 reflects the structure of the Data Transfer Object and Data Transfer Collection patterns from the previous chapter. The Partial Population pattern does not affect structure; it affects only the expected operation signatures and the expected results of operations.

click to expand
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 populated in a Partial Population scenario.

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 core patterns and increased the representation of data. You moved behavior to the Business Process and Asynchronous Business Process patterns. Therefore, the information in the remainder of this chapter could also apply to the primary interfaces on the base patterns.

Understanding the Components of the Partial Population Pattern

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 contains altered 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 additional methods 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 objects requested 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.

Understanding the Collaborations in the Partial Population Pattern

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.

click to expand
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.