Wednesday, August 19, 2009

Grails and the Domain Model, or why I prefer IBatis

I have spent many hours playing with Grails, to see how much effort it would be to take one of our existing apps and port it to Grails, since, as i have written previously, i prefer not to have to deal too much with GUI, so if Grails gives me a lot automatically, then I would be very happy using it.

At the same time, i have been using Grails to write a customized version of Quicken for my own personal budgetary needs (unfortunately, it has not progressed as well as i would want due to lack of time).

I will start with the second project first. As advertised, Grails is an absolutely amazing framework to take an idea and make it functional really fast. Just set up your domain classes and you automatically get a whole web site up and running, from which you can customize the columns and edits. And in the case of my personal app, the domain model is a few tables without any complex joins.

However, when trying to set up a Grails application on an existing database, this creates a lot of problems. I will start by saying that I am not a big fan of Hibernate either for a similar reason. In most reasonably sized companies, you have a pretty complex database (or set of databases) and in my opinion, it is easier to write queries than to try to “teach” the entire schema to your application. There are solutions like creating views, but then inserts will still be a problem. With Hibernate though, i found it possible with some effort to get around most of the issues. But in Grails, if i want the advantages of the framework, i cannot veer too far from the default domain model. After a lot of frustration trying to make the domain model work with our database, i found the best way to work was to keep tweaking it using the HSQLDB and seeing the scripts generated until they matched. In the end, i opened two  issues that prevented us from moving forward, and both have been addressed, 3616, and 4158. So, I guess now i am ready to resume.

But in the end, i still believe that the model IBatis provides is more appropriate for a company application. It means that the developers wears two hats – one of the SQL developer, writing and tweaking queries until they are just what you want and then putting on the programmer hat to use those queries that are mapped within an XML file. I see that IBatis version 3 is out, though every time i have checked i did not see too much documentation about what is different. If you are new to IBatis, I can tell that in version 2, the documentation is great, and it definitely is easy to get up and running with it, especially if you use Spring together with IBatis. And there are many nice advanced features that can take you quite far.

Technorati Tags: ,