You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Kaspar Fischer <hb...@rapsak.com> on 2009/06/10 18:56:25 UTC

OT: Using persistence layer objects for service results?

I am looking for a good pattern/methodology to use when coding  
services in small Wicket webapps. I want to use the services from  
Wicket but it will also be exposed via RMI and SOAP/REST. For example,  
a service for Houses and Inhabitants might have methods like this:

   // Returns the houses matching the filter (but not yet the  
inhabitants)
   List<House> findHouses(House filter);

   // Returns the inhabitants of the given house
   List<Inhabitant> getInhabitants(int houseId);

The class House exposes an id (for use in the getInhabitants() method)  
and some data (address, etc.). It does not provide the list of  
inhabitants because this list might be huge, might not be needed by  
the caller at all, etc. The service returns lists because this comes  
in handy with Wicket components.

My question: Should the above service return objects (HouseImpl,  
InhabitantImpl) from the persistence layer? Or do you introduce  
another set of public (DTO) classes for exposure in the service?

Ideally, I want to keep things simple and manageable, with little code.

For instance, for the above service one could use a Hibernate mapping  
like

   public class HouseImpl
   {
     ...

     @OneToMany(mappedBy = "house", fetch = FetchType.LAZY)
     private final Collection<InhabitantImpl> inhabitants;
   }

and

   public class InhabitantImpl
   {
     ...

     @ManyToOne(fetch = FetchType.LAZY)
     @JoinColumn(nullable = false, updatable = false)
     private HouesImpl house;
   }

and the service can directly return instances of these classes. I have  
used this approach in a project and we have seen these problems:

- There's some code used to properly detach the objects and load all  
exposed data in order to avoid LazyInitializationException's. For  
instance, if House has a list of caretakers and you want them to be  
exposed in the service's House, you may need to tell Hibernate to load  
them first. This code is cumbersome to maintain.

- I am not sure whether this would work with RMI, for example. I  
suppose the Hibernate proxies that exist for some of the members would  
get transmitted and would have to exist at the client end, too...

Therefore: How do you design your services? Is anybody using some  
method in which the service objects are generated (semi-)automatically  
from the persistence layer objects?

Many thanks for your input!
Kaspar

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org