Title: DynaDTO Dynamic Data Transfer Objects
1DynaDTO Dynamic Data Transfer Objects
- Brian Sam-Bodden
- Integrallis Software, LLC.
2Presentation Contents
- J2EE Patterns
- Typical Patterns in a J2EE Web Application
- The Transfer Object Pattern
- The Transfer Object Assembler Pattern
- Transformation Layers in J2EE
- Typical DTO Strategies
- Problems with existing DTO strategies
- The DynaDTO strategy
- Typical Usage Scenarios
- Configuring DynaDTO
- Using DynaDTO
3J2EE Patterns
- Abstractions of a Solution (design) to a
recurring problem - Refactoring Good Design Time Design Pattern
- Patterns are discovered, not created
- J2EE Patterns are
- Strategies
- Interpretations
- Extensions
- of more general design patterns (GoF)
4J2EE Patterns
- Each pattern (design) can have multiple
implementations (strategies) - The trick is...
- ... finding the right granularity for your
problem - ... collaboration amongst patterns
- Pragmatic View
- A practical solution to a real world recurring
problem and the guidelines of when and where to
apply it
5J2EE Patterns
- J2EE Pattern Catalog Pattern Format
- Problem
- Forces
- Solution
- Structure
- Interaction
- Consequences
- Strategies
6J2EE Patterns
- J2EE Pattern Catalog classifies patterns by Tier
- Presentation Tier
- Business Tier
- Integration Tier
7Presentation Tier Patterns
- Intercepting Filter
- Front Controller
- Composite View
- View Helper
- Service to Worker
- Dispatcher View
- Context Object
- Application Controller
8Business Tier Patterns
- Business Delegate
- Session Facade
- Service Locator
- ? Transfer Object
- Composite Entity
- ? Transfer Object Assembler
- Value List Handler
- Business Object
- Application Service
9Integration Tier Patterns
- Data Access Object
- Service Activator
- Domain Store
- Web Service Broker
10J2EE Patterns
11Typical Patterns in a J2EE Web Application
- Presentation Tier
- MVC Model II OR Component-based MVC like
- JSPServlet, Struts, JSF, Tapestry and others
- Business Tier
- POJO Domain Model
- Domain Model persistence via DAO
- DAO wraps persistence mechanism (JDBC, CMP, JDO,
Hibernate) - Services in Session Façade
- Services return and take DTOs
12Typical Patterns in a J2EE Web Application
- Business Tier (cont.)
- DTOs are assembled from Domain Objects
- Assembly step is manual
13The Transfer Object Pattern
- Also known (old) as Value Object Pattern
- The Problem
- Coarse-grained Entities
- CMP EJBs not meant to leave the Tier they live in
- Each data access (method call) is a potentially
remote call - Performance degradation, portability problems,
brittle architecture
14The Transfer Object Pattern
- The Problem (cont.)
- Fine-Grained Entities
- Domain Model should not be exposed to the client
- Client needs are temporal (application specific)
- Domain should outlive client needs and be
reusable - Anemic Domain Model (Fowler) results from Domain
Model tainted by both Data Model and the User
Interface Needs - No separation of concerns, lack of encapsulation
15The Transfer Object Pattern
- The Problem (cont.)
- Bulk-Transfers
- Not only for the data of a single entity
- Collections Transfer
- Support for pagination schemes
16The Transfer Object Pattern
- The Forces
- Web Applications are typically read-mostly and
save/update-occasionally - Data Viewing requirements per page are
- Typically composed of multiple attributes
- Transformed for display purposes
17The Transfer Object Pattern
- Solution (typical)
- Transfer Object to encapsulate business data
- Single method call to transfer data of a business
entity - Business Tier Code (Enterprise Bean) constructs
the Transfer Object, populate and pass it by
value to the client
18The Transfer Object Pattern
19The Transfer Object Pattern
20The Transfer Object Pattern
- Strategies
- Updateable Transfer Objects Strategy
- Multiple Transfer Object Strategy
- Entity Inherits Transfer Object Strategy (1-to-1)
- Transfer Object Factory Strategy
- Custom Transfer Object Strategy
- Multiple Entity, Single Transfer Object
Not in the J2EE Pattern Catalog Entry
21Typical Implementation
- Developers manually create light(er) weight
objects - Populate them in code, near where they access the
data - Session Façade, Entity Bean, DAO
- Usually support strategies outlined poorly
- Requires a great deal of coding
- Muddles the business tier with transformation
code - Equivalent to having O/R mapping code
22Problems with the Typical Implementation
- Parallel-Object Hierarchy
- Parallel Hierarchy is hard to maintain
- in XP terms this is smelly
- Sign that domain has been flattened
- Code Overhead/Maintenance
- DTO classes
- Transformation Code
23Transformation Layers in J2EE
- In a typical 3 tier application there are 2
transformation tiers - Between the business tier and the
data/integration tier - Between the business tier and the presentation
tier - O/R Mapping Tools deal with the first
- DTO Pattern Implementations deal with the second
- Problem is that everyone starts from scratch
24The DynaDTO Solution
- Provide a solution to the screen/UI object lt--gt
domain/data object transformation problem - Solution similar to what Hibernate is to O/R
Mapping - XML configuration
- Factory-based design
- Smart defaults
- Supports all the strategies for the DTO pattern
well - Enables you to test outside of the container
25A DynaDTO Sample Usage Scenario
- We have a single object, want a mirror DTO
- A Quote object is the domain object
- The interface IQuouteDTO represents the DTO
- Client only reads IQuoteDTO
26A DynaDTO Sample Usage Scenario
- Quote.java
- public class Quote
- // fields
- private String title
- private Date date
- private String author
- String text
- // getters and setters
27A DynaDTO Sample Usage Scenario
- IQuoteDTO.java
- public interface IQuoteDTO extends DTO
- String getTitle()
- Date getDate()
- String getAuthor()
- String getText()
28A DynaDTO Sample Usage Scenario
- IQuoteDTO.dto.xml
- lt?xml version"1.0"?gt
- ltdtosgt
- ltdto targetIQuoteDTO"gt
- ltsourcesgt
- ltsource typeQuote" automap"true" /gt
- lt/sourcesgt
- lt/dtogt
- lt/dtosgt
29A DynaDTO Sample Usage Scenario - 2
- We have an object graph with 3 objects
- A Conference object
- A Conference has a Venue object
- A Conference has Tracks
30A DynaDTO Sample Usage Scenario - 2
- We want a IConferenceSummaryDTO interface
- Read only
- Some info from the Conference object
- The address, broken in two lines from the Venue
object - A List with the names of the Tracks
31A DynaDTO Sample Usage Scenario - 2
- IConferenceSummaryDTO.dto.xml Shell
- lt?xml version"1.0"?gt
- ltdtosgt
- ltdto targetIConferenceSummaryDTO"gt
- ltsourcesgt
- ltsource typeConference" gt
- ...INTERESTING STUFF GOES HERE
- lt/sourcegt
- lt/sourcesgt
- lt/dtogt
- lt/dtosgt
32A DynaDTO Sample Usage Scenario - 2
- IConferenceSummaryDTO.dto.xml
- ltsource type"Conference"gt
- lt!-- aliases, used by the expressions --gt
- ltalias source"venue" name"venue" /gt
- lt!-- mappings --gt
- ltmapping from"name" to"conferenceTitle" /gt
- ltmapping from"venue.name" to"venueName" /gt
- ltmapping from"venue.address.streetAddress
- to"venueAddressLine1" /gt
- lt/sourcegt
33A DynaDTO Sample Usage Scenario
- IConferenceSummaryDTO.dto.xml
- ltexpressionsgt
- ltexpression target"VenueAddressLine2
- value"venue.getAddress().getCity() ','
util.space() - venue.getAddress().getState()
util.space() - venue.getAddress().getZipCode()
" /gt - lt/expressionsgt
34Steps to use DynaDTO
- Define your DTO Interfaces
- Create mappings from Domain Object to DTO
interfaces - Reference the mapping in the dynadto.cfg.xml
- At runtime, initialize DynaDTO by reading
dynadto.cfg.xml - To create a DTO, use BuilderFactory to get a
Builder for a given DTO interface or class - Gather your source (domain objects)
- Pass them to the build method of the builder
35DynaDTO Sample Configuration File
- dynadto.cfg.xml
- lt?xml version"1.0" encoding"utf-8"?gt
- ltdynadto-configurationgt
- lt!-- mappings --gt
- ltmapping resource"ConferenceSummary.dto.xml" /gt
- lt/dynadto-configurationgt
36Initializing DynaDTO
- dynadto.cfg.xml
- try
- ConfigurationLoader.loadConfiguration("dynadto
.cfg.xml") - catch (ConfigurationException ce)
- ce.printStackTrace()
-
37Using DynaDTO
- Conference conf ...
- // build the DTO
- Builder builder BuilderFactory
- .getInstance()
.getBuilder(IConferenceSummaryDTO.class) - IConferenceSummaryDTO myDTO
- (IConferenceSummaryDTO) builder.build(conf)
38Conclusions
- DynaDTO simplifies implementation of DTO Pattern
- Supports all known strategies
- Supports complex expressions
- Supports nested DTOs
- Supports Collection Flattening/Transformation
- Allows you to use POJIs or POJOs as DTOs
39THANK YOU
- Questions?
- Feel free to email me at bsbodden_at_integrallis.com