Hi,
I am currently exploring the option of using tapestry for my next
project. However, I realized there is no direct support for using
mybatis directly with tapestry - I could use Guice or SPring and use
their mybatis support, but I dont really want to do that.
So I put my Christmas holidays to a good use and was able to write a
basic mybatis plugin that works natively inside Tapestry without
requiring any other IoC libraries. However, I am quite new to Tapestry
and would like to know the "right way" of doing it from all the Gurus here.
Basically, what I did was created a "MyBatis Module" which in turn loads
up a configuration service and a "MyBatisMapperManager" which is
responsible for providing mapper objects/DAO objects when requested .
The configuration service loads up all the defaults from the
mybatis-config.xml file and provides it other services that might need
it, such as the SqlSessionService or the SqlSessionManagerService.
I am, however, not sure about the best way to inject Mapper/DAO objects
in request classes. I came up with two approaches:
1. The first one was to inject the "MyBatisMapperManager" directly, but
then that would require a little more additional work on the
page/component developer and would require them to write something like :
..............
................
@Inject
MyBatisMapperManager mapperManager;
...............
...........
.
..................................
MyDao myDao = mapperManager.get("com.x.y.data.mapper.MyDao")
myDao.getByPrimaryKey(1);
2. The second approach was to register all the available mappers upfront
and contribute to the MasterObjectProvider service. It seems like
Tapestry invokes all the registered object providers registered with the
MasterObjectProviderService when it is looking for an object and one can
query the passed in arguments to find out what interface/implementation
is requested. So the implementation for the MyBatisMapperManager service
looks like:
public <T> T provide(Class<T> objectType,
AnnotationProvider annotationProvider, ObjectLocator locator) {
//Get All available mappers from config file.
if(mappers == null) {
MyBatisConfiguration config =
locator.getService(MyBatisConfiguration.class);
config.configure();
mappers = config.getAllMappers();
}
if (!mappers.contains(objectType.getCanonicalName())) {
System.out.println("Our mapper list does not contain the
above type. Returning");
return null;
}
//Other validation
....
return
SqlSessionManager.newInstance(sqlSessionFactory).getMapper(objectType);
}
and In the component or the page, the developer can simple ask for the
Dao to be injected directly
@Inject
com.x.y.data.mapper.MyDao myDao
//And use the Dao directly
myDao.getByPrimaryKey(22);
...
However, I am not sure if this is the "right" way to do it. First and
foremost, the provider method gets called every time Tapestry needs to
provide an object. I could work around that problem by specifying the
"last" qualifier, I guess but just wondering if I am indeed on the right
track.
Personally, I would like to use to static bind method, but could not
figure out a way to cleanly accomplish it. Think I need to read up a bit
more on that.
I would appreciate your comments and suggestions.
Best Regards,
Sanket
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org