Trails - like Rails, but with Tapestry, Spring and Hibernate
I've been thinking about Rails ever since I wrote a post about it on Monday. The main reason is because of Dion's comment:
Matt - You should follow the lead and do a video of setting up a simple app using AppFuse.
This might sounds like a good idea, but if I did it right now in AppFuse's current state, it'd be a disaster. The reason? Because you have to manually create a whole bunch of classes to do CRUD on a database table. Here's a list of new classes needed for adding a new "person" table.
- model.Person
- dao.PersonDAOTest
- dao.PersonDAO
- dao.hibernate.PersonDAOHibernate
- service.PersonManagerTest
- service.PersonManager
- service.impl.PersonManagerImpl
- webapp.action.PersonActionTest
- webapp.action.PersonAction
- web/pages/personList.jsp
- web/pages/personForm.jsp
The last two JSPs can be generated, but that's still a buttload of classes (9) just to CRUD (and test!) a database table. Not too mention all the files you need to edit for Spring and i18n.
- dao/hibernate/applicationContext-hibernate.xml
- service/applicationContext-service.xml
- test/web/web-tests.xml
- web/WEB-INF/classes/ApplicationResources_en.properties
- web/WEB-INF/menu-config.xml
Result: to CRUD a database table using AppFuse you have to create 11 new files and modify 5 existing files. 16 files. What a beotch, huh? If I made a video of this - it'd be 20 minutes long! While this might make AppFuse look silly, it's really more of a symptom of the patterns we have in J2EE and how we're supposed to architect our apps. 3 tiers, test-driven, loosely-coupled and internationalized. Of course, if I was focused on fast and efficient, I could do this all with 1 JSP and JSTL's SQL tags. Everyone would slap my hand for not following patterns, but I'm willing to bet it'd work just as well and be just as fast. But I digress.
There have been a fair amount of requests (and some patches submitted) to generate and modify all of the files listed above. For the most part, I've frowned upon adding such a feature because I think if folks can run "ant generate -Dmodel=Person" - they'll end up with a whole bunch of code that they know nothing about. Sure there's the tutorials, but folks will quit reading those. Instead, they'll create a whole slew of POJOs (maybe even using Middlegen) and run "ant generate" on all 50 of them. Poof - now they've got 550 new files to maintain. Talk about a maintenance nightmare. Even worse - a support nightmare for me.
Nevertheless, if I wanted to create a cool video for AppFuse, I'd spend a few days writing this code-generation engine. Then I could show how you could create the data, service and web layer (including UI) in a matter of seconds. It'd be cool and folks would dig it. I'm still considering it, but I'm also leary of the resulting support fiasco. Maybe I could just say "use at your own risk".
A while back, I saw Erik Hatcher suggest a better solution than code-generation. I can't remember what he called it, but it was something like "meta-data dynamic rendering". The idea is that your application reads the metadata of a table (or POJO) and renders the appropriate UI for it. I loved the idea as soon as I heard it. I've always wanted a way to dynamically render the UI rather than writing HTML. Of course, I still want the ability to edit the templates and HTML since I fancy that sort of stuff. I don't like writing HTML for each row in a form, but I do like tweaking the HTML and CSS to look good.
Earlier this week, I saw the concept in action with Rails and its demo. IMO, something like Rails would never fly in Java because it appears to be tightly coupled to the database and only MySQL, PostgreSQL, and SQLite are supported. The Java community always seems to pride itself on database abstraction, partly due to JDBC and its ability to connect to anything that has a JDBC Driver. Ruby will probably catch up someday, but right now it appears to be looking for something like JDBC.
Then along comes Trails, which made me smile earlier today when I first read about it on the tapestry-dev mailing list.
I've been working on a project called Trails that uses Tapestry quite heavily and I thought it time to start soliciting feedback. Trails is a domain driven development framework that uses Tapestry, Spring, and Hibernate. Trails is very much in it's infancy, but the current version is functional and should give people a rough idea where I am heading. It's my first real forray into Tapesty and I have really found Tapestry a joy to work with.
Trails is very much like Rails, except that it doesn't talk directly to a database table. Instead, it talks to your domain objects that you mark up with XDoclet/Hibernate tags. To test it out, I dropped a User.java file into the org.trails.demo package, marked it up with XDoclet and deployed. It didn't work at first because the .hbm.xml files are explicitly listed in Spring's applicationContext.xml. I changed the "sessionFactory" bean to use the following and wammo - success! I could list and CRUD the table that my User object was mapped to.
<property name="mappingDirectoryLocations"> <list> <value>classpath:</value> </list> </property>
Trails is very cool, and I'd love to incorporate it into AppFuse or Equinox. Does an LGPL license allow me to do this? The one problem I can see with adding it is that it's specific to Tapestry and Hibernate, which doesn't always suite folks. I think developers might be willing to change because this solution will vastly improve their development productivity, but who knows. I think the best solution would be to offer this option in AppFuse/Equinox, but also offer the current manual and code-generation options. The holy grail would be the ability to plug in iBATIS or JDO instead of Hibernate. In addition, using Struts, Spring, WebWork or JSF instead of Tapestry would have folks clammering to use this stuff.
Mad props to David Heinemeier Hansson and Chris Nelson - you guys are developing awesome software.
Posted by analogueboy on October 29, 2004 at 09:14 AM MDT #
Posted by Francisco Hernandez on October 29, 2004 at 09:29 AM MDT #
When you write "something like Rails would never fly in Java because it appears to be tightly coupled to the database" that's not really true. Active Record (the ORM part of Rails) has a database abstraction layer embedded called Abstract Adapter, which all the specific adapters are implementing transparently.
Hence, your Rails application is not tightly coupled to a specific database, but rather to the Abstract Adapter interface. Since concrete adapters are just ~100 lines of code, it's pretty easy to implement your own adapter if the current offering isn't sufficient.
The PostgreSQL and SQLite adapters were implemented one the same day by ONE person. There's a Microsoft SQL Server adapter just around the corner and I've heard of people working on Oracle and DB2 adapters too.
So the only tightly coupling going on is if you write database-specific SQL. On that account, I'm actually very sympathetic to Jeremy Zawodny's Database Abstraction Layers Must Die!. Chasing database abstraction without a specific business case asking for it is just something You're Not Gonna Need and it's certainly not doing The Simplest Thing Possible.
Posted by David Heinemeier Hansson on October 29, 2004 at 10:39 AM MDT #
Posted by bitnix on October 29, 2004 at 11:25 AM MDT #
Posted by bitnix on October 29, 2004 at 11:54 AM MDT #
Posted by bitnix on October 29, 2004 at 11:55 AM MDT #
Posted by Dave Noble on October 29, 2004 at 03:08 PM MDT #
Posted by Lance Lavandowska on October 29, 2004 at 04:10 PM MDT #
Posted by Matt Raible on October 29, 2004 at 04:13 PM MDT #
Posted by Lance on October 29, 2004 at 04:13 PM MDT #
Posted by Joey Gibson on October 30, 2004 at 02:14 AM MDT #
Posted by Keith on October 30, 2004 at 03:47 AM MDT #
Posted by Todd on October 30, 2004 at 05:11 AM MDT #
Posted by Matt Raible on October 30, 2004 at 06:48 AM MDT #
Posted by Keith on October 30, 2004 at 02:24 PM MDT #
Posted by Chris Nelson on October 30, 2004 at 03:08 PM MDT #
Posted by Todd on October 30, 2004 at 04:39 PM MDT #
Posted by Keith on October 30, 2004 at 05:47 PM MDT #
Posted by Gabriel K. on October 31, 2004 at 04:29 PM MST #
I'm assuming that it wouldn't necessarily force you to use one specific framework although I'm sure a good bit of the UI code could be written for you in advance, it would just depend on what your UI religion is... I'm an Echo user and would love to shrink the tedium of writing the CRUD code using an open source tool.
Posted by Mark on November 01, 2004 at 02:58 PM MST #
Posted by Mike Henderson on November 05, 2004 at 01:52 AM MST #
Posted by Bill Schneider on August 25, 2005 at 06:02 PM MDT #