THE EXPOSABLE DATA TRANSFER OBJECT PATTERN
Background
I've been using the Data Transfer Object (DTO) pattern in the design of enterprise systems in .NET and Java for a number of years. A DTO is a very simple object whose prime purpose is to encapsulate a complex data structure for transport, or transfer, between a data provider and a data consumer. DTOs can be seen simple "lumps" of data that is structured in a complex way and cannot easily therefore be represented using primitives or value types.
DTOs:
I've found DTOs particularly useful in situations where data needs to be transferred between systems across an interfacing layer or facade. The operations on an application's facade both accept and return data in a DTO format. DTOs help promote the decoupling of systems by allowing any complex data structures that need to be transferred to be done in a loose, independant structure that is not tied to the business domain structures of either the data provider or data consumer systems.
However, when operations on a facade need to return multiple DTOs, things can get a bit tricky. Most programming languages allow only one return value from an operation, which limits the structure of DTOs that can be returned to having one root (a single top-level DTO in the DTO structure). This is fine if the DTO structure has logically got one top-level DTO, but often this is not the case. This can pose a problem because developers will find themselves trying to manipulate the DTO structure to fit this constraint.
This is where the concept of an Exposable DTO (EDTO) steps in: The EDTO is a special DTO that is the highest-level DTO in a DTO structure. The EDTO itself does not contain any data, it simply encapsulates the complex DTO structure that needs to be transported. Using an EDTO as the root object in the DTO structure means that multiple DTOs (lists of DTOs as well as heirarchies) can be returned without the limitations of the single-return type of a program operation. This fits well with most common programming languages and paradigms today that are restricted to returning one object from an operation.
Pattern Definition
| Problem | You need to transfer complex data structures using mutliple DTOs to and from independant systems. |
| Solution | Create an Exposable DTO as the root DTO which "wraps up" the complex DTO structure to be transferred. |
| Consequences |
|
UML Design
The following class diagram illustrates how the EDTOs may be designed:
UML Class model of an EDTO framework
Observations
Here are some observations I've made regarding the DTO and EDTO patterns:
A good implementation of the EDTO pattern will therefore: