You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Leon Pennings <L....@kenict.org> on 2007/04/12 08:29:36 UTC

Integration of T5 with the domain model

Just wanted to share how I hooked up T5 with my domain model.
I'm using jpa for persistence, so the thing I needed was injection of
the EntityManager into the objects providing access to my top level
domain objects.
And for just that simple IoC, Spring is way too overkill. Guice on the
other hand was quite nice for it.

I integrated it like this:

The access object

public class BlaBlaJPAQuery implements BlaBlaFinder {
   @Inject //the guice inject annotation
   private EntityManager _entityManager;

   //Code...
}

the JPAUtil

public class JPAUtil implements Provider<EntityManager> {

   private static final EntityManagerFactory _entityManagerFactory;
   
   private static ThreadLocal<EntityManager> _entityManagerContainer;
   
   static {
      _entityManagerFactory =
Persistence.createEntityManagerFactory("POB");
	_entityManagerContainer = new ThreadLocal<EntityManager>();
   }
   
   public static void createEntityManager() {
 
_entityManagerContainer.set(_entityManagerFactory.createEntityManager())
;
   }
    
   public static void closeEntityManager() {
      EntityManager entityManager = _entityManagerContainer.get();
      entityManager.close();
   }

   public EntityManager get() {
      return _entityManagerContainer.get();
   }
   
}

Another filter before the T5 filter that does this:

   public void doFilter(ServletRequest request, ServletResponse
response, FilterChain filterChain) throws IOException, ServletException
{
     try {
        JPAUtil.createEntityManager();
        filterChain.doFilter(request, response);
     } finally {
        JPAUtil.closeEntityManager();
     }
   }

The Guice module:

public class POBGuiceModule extends AbstractModule {

   public void configure() {
      bind(EntityManager.class).toProvider(JPAUtil.class);
   }

}

And the T5 module:

public class POBTapestryModule {

   private static final Injector _injector;
  
   static {
      _injector = Guice.createInjector(new Module[] { new
POBGuiceModule() });
   }
   
   @Lifecycle(value="perthread")
   public static BlaBlaFinder buildBlaBlaFinder() {
      return _injector.getInstance(BlaBlaJPAQuery.class);
   }

}

And offcourse the page:

   @Inject //T5 inject annotation
   private BlaBlaFinder _blablaFinder;

Comments/Suggestions/missed design flaws?



DISCLAIMER:

Dit bericht (met bijlagen) is met grote zorgvuldigheid samengesteld. Voor mogelijke onjuistheid en/of onvolledigheid van de hierin verstrekte informatie kan Stichting Kennisnet Ict op school geen aansprakelijkheid aanvaarden, evenmin kunnen aan de inhoud van dit bericht (met bijlagen) rechten worden ontleend. De inhoud van dit bericht (met bijlagen) kan vertrouwelijke informatie bevatten en is uitsluitend bestemd voor de geadresseerde van dit bericht. Indien u niet de beoogde ontvanger van dit bericht bent, verzoekt Stichting Kennisnet Ict op school u dit bericht te verwijderen, eventuele bijlagen niet te openen en wijst Stichting Kennisnet Ict op school u op de onrechtmatigheid van het gebruiken, kopi�ren of verspreiden van de inhoud van dit bericht (met bijlagen).

This message (with attachments) is given in good faith. Stichting Kennisnet Ict op school cannot assume any responsibility for the accuracy or reliability of the information contained in this message (with attachments), nor shall the information be construed as constituting any obligation on the part of Stichting Kennisnet Ict op school. The information contained in this message (with attachments) may be confidential or privileged and is only intended for the use of the named addressee. If you are not the intended recipient, you are requested by Stichting Kennisnet Ict op school to delete this message (with attachments) without opening it and you are notified by Stichting Kennisnet Ict op school that any disclosure, copying or distribution of the information contained in this message (with attachments) is strictly prohibited and unlawful.


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