Tuesday, December 16, 2008

Dozer Can Help Me Manage Parallel Object Hierarchies!

Some time ago, I wrote a post called "Robust Architecture for Object-Relational Mapping", in which I discussed and diagrammed a separation of "Business/Domain" and "Persistence" objects into separate, parallel object hierarchies. The idea is to isolate lazy loading and other ORM features (as well as any database schema oddities) away from the actual objects that implement the business logic. The main drawback to this approach is that it requires you to implement potentially-tedious and error-prone logic to transfer data back and forth between the layers. Taking this further, I think it's perfectly reasonable for a modern enterprise application to have up to four parallel object hierarchies:
  1. Persistence objects, which can closely mirror the database schema if necessary and guarantee that "transparent" ORM stays that way.
  2. Business/Domain objects, which reflect the application's core entities and the business logic that manipulates them.
  3. View objects, which are data objects optimized to serve the View layer.
  4. Message objects, which are exposed to the outside world via services and need to remain stable in the face of changes to the business layer.

In anything other than a trivial application and given current technologies, it is very difficult to use a single object model to serve all of the needs described above. (Trust me, I've tried.) So in reality, we spend a lot of time in the transfer implementation, moving values back and forth between the different flavors of objects, either with tedious set/get code, a hand-rolled reflection-based framework, or (more likely) a combination of both.

Clearly this is annoying. So I was excited today when one of my friends told me about Dozer, which purports to be a "powerful, yet simple Java Bean to Java Bean mapper". I haven't tried it yet -- just spent a lot of time going through the documentation. But it appears that Dozer will allow me to wire up hierarchies like those I describe above in a minimal-code, declarative fashion. I'm looking forward to trying it out and reporting my experiences.

No comments: