Friday, July 4, 2008

Is development of CRUD apps faster with Grails than with Swing?

To make my point clear immediately at the beginning. I am not claiming that creation of fat rich client is easier in some web framework than with Java or .NET. I am talking about simple CRUD application with three domain objects, and two features that cannot be directly connected with CRUD.

So problem is following. There are three domain objects that should be stored into local database. Domain objects are Calculation, Shop and Item. Relationship between domain is displayed on the following picture:

calculation

So each calculation is made for exactly one shop and can contain number of items. Pretty straightforward domain model. Additional requirements are that ordinal number of Calculation is increased per Shop instance and you are able to print report for each Calculation.

So what are the tools necessary to create simple CRUD application for mentioned domain model. I decided for Swing, Hibernate, HSQLDB, Jasper reports. So how the work goes:

In Java:

As first step you create domain classes and then using some examples on the Internet create hibernate mapping files and hibernate configuration file. Next step is to reuse some your local component that handles SessionFactory initialization or you just find example on the Internet and copy/paste that example. More or less even these simple steps already can bring some headache (e.g. if you mistype property name of your domain object in XML file). And yes I know, probably there is number of tools that can help me with this configuration and I heard about Hibernate annotations too :)

After your domain object is created and database is automatically generated for you real fun begins. You need to create Swing application. You need main frame, dialogs and panels (even reused) to CRUD Shop domain objects and the same thing for Calculation and Items that are part of Calculation. As you need tables to display list of domain objects you have to create corresponding table models, connect listeners to buttons and so on. Everybody that have some experience with Java Swing know that although this is more or less simple and straightforward process it does not mean it will work from the first try. And the most often it will not.

When set of your screens is ready you still need to wire it up with domain and Hibernate (DB). You have to cope with hibernate sessions, lazy loading...

When your application supports all CRUD operations the end is near. You just need to create Jasper report and it is easy with the help of iReport.

Now when you see all steps necessary what would be your estimation for such application. I would say 2-3 days (well I did it in my free time).

In Grails:

Now let us see how all these steps are handled in Grails. Thanks to the GORM, scaffolding and plugins (Jasper plugin particularly) we will see that it is significantly faster with the Grails.

To start first you need to create new Grails application. For this it is enough to type grails create-app.

When your application is created using grails create-domain-class you can generate necessary domain classes. When domain classes are generated you add code for properties and relationships between domain objects. Having this you don't need to worry about Hibernate configuration because Grails will do that automatically for you.

Now when domain objects are ready we need to create user interface of application.

Well creating web user interface can really take time. You need to type generate-all for each domain object or if you are lazy you can reuse UberGenerateAll script. And your user interface is mainly finished (actually you can keep it that way if you want and still you will have nice fully functional web application). If you want you can update generated pages (e.g. remove ids from tables, maybe copy/paste few buttons). More or less easy work. You need to add little bit of behavior to the controllers or domain classes but this work can be only faster with Groovy than it was with Java.

To create report you can reuse Jasper Reports plugin for Grails. This means even all necessary dependencies will be resolved for you :)

And actually simple CRUD application is finished.

Conclusion

While I worked on Java implementation of described application I was thinking why it cannot be easy as with Grails. Anyway I was just repeating steps that I did number of times. Domain classes, Hibernate, Swing, JTables, dialogs. When I finished Java application I generated new Grails application and had basic behavior done in less than hour. It is just great and unbelievable. I didn't do final touches to the Grails application because anyway I didn't need this application in Grails.

But I was thinking, if I use embedded Jetty, embedded browser in Java frame, wouldn't it be easier to finish this application :) Maybe next time I will try.

But another idea that came to my mind is what if we could reuse GORM in the standalone applications, why not to implement scaffolding for Swing front end...

More or less I believe that Grails is excellent web framework that boost your speed and agility. The main problem with Grails is that you are going so fast forward that very often you are forgetting to write tests :) Believe me, I have experienced this on grails tutorials.

1 comment:

p3t0r said...

you might want to have a look at the groovy solution for building swing apps:

http://groovy.codehaus.org/Swing+Builder