Skip to main content

Version 2.0.1 Released!

After a long period of happy exPOJO usage on a variety of different projects we saw some room for simplification and improvement and version 2.0.1 is the result of this.

We've made some excellent refinements and added some new features:

New ModelExposerFactory
The most notable refinement is that the PersistenceProviderFactory has been renamed to ModelExposerFactory which is extended within your application to become the single object reponsible for producing ModelExposer's when required.

Previously ModelExposers were produced by the ExpojoServletContextListener which unfortunately tied the use of exPOJO to web based applications. Now that ModelExposer's are produced by a separate ModelExposerFactory that is independent of the servlet API exPOJO can be used equally well in web app and traditional Java applications.

Even within web based development there is often the need for background threads running as part of the web app or running as services outside of a servlet container and this recent refinement of the exPOJO design makes it possible to implement exPOJO in both the web app and supporting services using common source code.

Simple Wrapping of method within a transaction context
The basic pattern for using the ModelExposer is as follows:


try
{
// Either find a ModelExposer associated with the current context or create
// a new one each time - the choice is yours. The context could be a HTTP
// session or anything you want it to be.
modelExposer = context.getModelExposer();

if ( modelExposer == null )
{
modelExposer = ModelExposerFactory().get().createModelExposer();
context.setModelExposer(modelExposer):
}

// Inject (attach) the ModelExposer to this thread so that within any
// methods executed by this thread the model exposer is instantly
// accessible without requiring direct injection of individual
// objects via ModelExposer.get()
modelExposer.attach();

// Get the PersistenceProvider and begin a transaction
PersistenceProvider pp = modelExposer.getPp();

pp.beginTx();


// Typically call a method to perform work like servicing a HTTP request or
// executing an iteration of a periodic background task
// do something with your model - load objects, navigate along
// relationships etc., populating UI components: pages, forms, fields etc.,
doWork();

// example of code that may be executed within a method that does the work:
// UI elements, model objects etc., all have access to the repositories and
// services and the methods of the PersistenceProvider by simply calling the
// convenience methods eg.,

// User user = UserRepository.get().findUser(username);

// Order order = OrderService.get().createOrder(customer);
...

// Commit the transaction
pp.commitTx();
}
catch (Exception e)
{
// rollback if required etc.,

}
finally
{
// Detach from this thread - as threads are pooled in a servlet container we must
// not leave the model exposer attached to any particular thread after it
// has finished servicing the current http request
modelExposer.detach();
}

In exPOJO 1.x what tended to happen was that this familiar pattern ended up being implemented in multiple places throughout the code. In 2.x a new method in ModelExposer makes it simple to wrap code in such a pattern without having to repeatedly write implementations of the whole pattern. The implementation within ModelExposer can simply be reused, wrapping any doWork() style method that you require. The doWork() style method in ModelExposer is actually called execute() and it supports the passing of parameters if required.

Comments

Popular posts from this blog

Welcome to exPOJO

This blog has been created as a meeting place for developers and users of exPOJO to communicate, share ideas, ask questions etc., Please send all questions and comments to this blog.

Expojo 2.2 is on its way!

Expojo 2.2 is almost here Expojo 2.2 has some very useful enhancements but the main changes are name changes to some key classes which make more sense. Don't worry, we haven't removed the old classes (yet) so your existing code should work fine. Some of the name changes are: ModelExposer > ExpojoContext This class is automatically bound to the thread processing a HTTP request by the ExpojoFilter. ExpojoContext is readily available anywhere in your code via ExpojoContext.get(). It contains all the Repository and Service components that are registered in ExpojoContextFactory's addComponents method. ModelExposerFactory > ExpojoContextFactory This factory can be overridden but most likely the existing implementations will provide what you need. ExpojoServletContextListener > ExpojoFoundation This class has not really been a ServletContextListener for a long time (the listening is all done by ExpojoFilter now) so it was time for a name change. ExpojoF...