This article shows a real scenario where dimple helped a lot.
Background
A legacy system uses a ConnectionManager class to manage connections:
Client code is required to call ConnectionManager.checkIn(connection) to release a connection.
To Integrate iBatis
Things all work fine until an architect tries to bring in iBatis to handle persistence.
The best way to integrate the legacy system with iBatis is to implement a javax.sql.DataSource and a com.ibatis.sqlmap.engine.datasource.DataSourceFactory.
In order to implement DataSource, the Connection object checked out should be wrapped so that the "close" method calls ConnectionManager.checkIn().
Here goes pseudo code that does not compile:
- LegacyConnection implements Connection.close() and Connection.isClosed() method to perform ConnectionManager.checkIn() as well as to make sure isClosed() return true when close() is called.
- LegacyDataSource calls ConnectionManager.checkOut() when getConnection() is called. The checked out connection is then wrapped up in a LegacyConnection to implement the close logic.
- LegacyDataSourceFactory implements iBatis DataSourceFactory and simply return a LegacyDataSource object.
The ibatis config file can then be configured using the "transactionManager" as:
However, obviously the pseudo code doesn't compile because the "All other methods" in Connection interface and DataSource interface need yet to be implemented.
One could use the IDE's code generation support to "Generate Delegate Methods" for LegacyConnection and to "Implement/Override Methods" for LegacyDataSource. This technique works but has some serious drawbacks:
- It has versioning problem. If you are generating code for jdbc 3.0, it probably won't compile for jdbc 4.0 because some new methods may have been added to Connection interface or DataSource interface or possibly both.
- Code generation in Eclipse may be easy, but the hundreds of lines of generated code may be annoying.
- You need to generate delegate method for deprecated methods, this generates compiler warning and worry (what if this method is later removed from the interface? The delegate method will not compile!)
Dimple Solution
Using dimple, we can be rid of versioning problem and deprecated method problem. The code will become:
- Comment out "implements Connection" and "implements DataSource".
- Use dimple Implementor to convert LegacyConnection to Connection and LegacyDataSource to DataSource.
Created by benyu
On Sun Jan 07 10:59:51 CST 2007
Using TimTam
