Background
We're making use of the Assembler pattern in a truly distributed architecture that requires the passing of DTOs to rich (Swing/SWT) clients.
What we'd like to have is a generic assembly method for each domain/entity to DTO and are considering implementing them on a use-case by use-case basis instead.
The reason is because we don't want to introduce a Hibernate dependency check for LazyInitializationExceptions inside the assemblers whenever a proxy or uninitialized collection is encountered because a particular use-case doesn't require it, and quiver at the thought of adding flags and structured logic indicating whether a field is or isn't to be loaded inside the entity and assemblers respectively.
Question
How are others in the community are dealing with these issue? Is the use-case by use-case assembler the only route since we cannot make use of filters/interceptors, etc. We don't want to move the assembler call inside the transaction because this will result in a different kind of N+1 select problem: initialization of unwanted members because they are unknowingly being loaded because the generic assembler is invoking the uninitialized associations as a result of trying to stuff them into DTOs.
Please note that we're currently using Session EJBs as the implementing business interface responsible for transaction demarcation using the ThreadLocalSession pattern, CMT and nested transactions inside DAOs. We are not employing Spring as a framework.
Hibernate version: 2.1.4
Code between sessionFactory.openSession() and session.close():
Keep in mind we're using Session Beans.
We'd like this type of generic assembly usage:
Code:
...
Session session = AcmeThreadLocalSession.currentSession();
List entityList = null;
try {
entityList = AcmeDAOFactory.getDAOFactory().getEntiyDAO().findAll();
session.flush();
}
catch (Exception e) {
getSessionContext().setRollbackOnly();
throw (AcmeException) AcmeExceptionConverter.convert(e);
}
finally {
AcmeThreadLocalSession.closeSession();
}
/* here's where we swizzle the entity into a DTO. If we reuse this
method in another use-case which may produce a deeper or
shallower entity graph in the list, this will result in a
LazyInitializationException.*/
return EntityAssembler.writeDTO(entityList);
...
Regards,
Roll